题目链接:
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数组
网友评论