2020-11-30-简单-1672. 最富有客户的资产总量

题目链接
https://leetcode-cn.com/problems/richest-customer-wealth/
我的答案

 class Solution {
  public int maximumWealth(int[][] accounts) {
      int n=accounts.length;
      int m=accounts[0].length; 
      int wealth[]=new int[n] ;
      int max=0;
      for (int i = 0; i < n; i++) { 
          for (int j = 0; j < m; j++) { 
              wealth[i]+=accounts[i][j];
          }
      }
      for (int k = 0; k< wealth.length;k++) {
          System.out.println(wealth[k]);
          if(k==0) {
              max=wealth[k];
          }else if(wealth[k]>=max) {
              max=wealth[k];
          }
      }
      return max;
  }
}

题解

class Solution {
        public int maximumWealth(int[][] accounts) {
            if (accounts == null) return 0;
            int ans = 0;
            for (int i = 0; i < accounts.length; i++) {
                int max = 0;
                for (int j = 0; j < accounts[i].length; j++) {
                    max += accounts[i][j];
                }
                ans = Math.max(ans, max);
            }
            return ans;
        }
    }

总结:
1.只用一次不需要保存,直接一直用最大值记录即可,不需要保存到wealth数组

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容