House RobberI 高频题 其它公司也喜欢考
Page 1 of 1
House RobberI 高频题 其它公司也喜欢考
public static int rob1(int[] num) {
if (num.length == 0) return 0;
int[] dp = new int[num.length + 1];
dp[0] = 0;
dp[1] = num[0];
for (int i = 2; i <= num.length; i++)dp[i] = Math.max(dp[i - 1], dp[i - 2] + num[i - 1]);
return dp[num.length];
}
public static int rob(int[] num){
if (num.length == 0) return 0;
int prepre = 0;
int pre = num[0];
for (int i = 1; i < num.length; i++) {
int cur = Math.max(prepre + num[i], pre);
prepre = pre;
pre = cur;
}
return pre;
}
if (num.length == 0) return 0;
int[] dp = new int[num.length + 1];
dp[0] = 0;
dp[1] = num[0];
for (int i = 2; i <= num.length; i++)dp[i] = Math.max(dp[i - 1], dp[i - 2] + num[i - 1]);
return dp[num.length];
}
public static int rob(int[] num){
if (num.length == 0) return 0;
int prepre = 0;
int pre = num[0];
for (int i = 1; i < num.length; i++) {
int cur = Math.max(prepre + num[i], pre);
prepre = pre;
pre = cur;
}
return pre;
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|