问题:输入:accounts = [1,5,7,3,3,5]输出: 10
第二个客户是最富有的,财富为10. ** 是使用for-循环的解决方案。
public int maximumWealth(int[][] accounts) {
int total_count = 0;
for (int j = 0; j < accounts.length; j++) {
int temp_count = 0;
for (int i = 0; i < accounts[0].length; i++) {
temp_count = accounts[j][i] + temp_count;
System.out.println(accounts[j][i]);
System.out.println("value of temp_count" + temp_count);
}
if (temp_count > total_count) {
total_count = temp_count;
System.out.println("value of total_count" + total_count)
}
}
return total_count;
}下面的是带有增强的for循环的解决方案
class Solution {
public int maximumWealth(int[][] accounts) {
int total_count = 0;
for (int[] account: accounts) {
int temp_count = 0;
for (int item: account) {
temp_count = item + temp_count;
}
if (temp_count > total_count) {
total_count = temp_count;
}
}
return total_count;
}
}发布于 2020-12-03 06:46:54
这两种形式的for循环都具有相同的时间复杂度,即O(n*m)。引入增强型for循环是为了更简单地迭代集合的所有元素。它也可以用于数组,但这不是最初的目的。增强的循环是简单但不灵活的。“增强”一词并不意味着增强的for循环在时间复杂度方面得到了增强。和循环一样。
https://stackoverflow.com/questions/65120784
复制相似问题