【发布时间】:2021-03-15 03:53:26
【问题描述】:
问题: 输入: 帐户 = [[1,5],[7,3],[3,5]] 输出:10
- 说明:
- 第一个客户拥有财富 = 6
- 第二位客户拥有财富 = 10
- 第三位客户拥有财富 = 8
第二位客户是最富有的,拥有 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;
}
}
【问题讨论】:
-
为什么这两个会有不同的时间复杂度?这是相同的操作 - 二维数组上的两个嵌套循环。他们都是
O(n*m)
标签: java foreach time-complexity