for (inti=2; i < n + 1; i++){ dp[i] = Math.min(dp[i-1] + cost[i - 1], dp[i - 2] + cost[i - 2]); }
return dp[n]; } }
62 不同路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintuniquePaths(int m, int n) { int[][] dp = newint[m][n]; for (inti=0; i < m; i++){ dp[i][0] = 1; } for (inti=0; i < n; i++){ dp[0][i] = 1; }
for (inti=1; i < m; i++){ for (intj=1; j < n; j++){ dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } }
for (inti=2; i < n + 1; i++){ dp[i] = Math.min(dp[i-1] + cost[i - 1], dp[i - 2] + cost[i - 2]); }
return dp[n]; } }
62 不同路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintuniquePaths(int m, int n) { int[][] dp = newint[m][n]; for (inti=0; i < m; i++){ dp[i][0] = 1; } for (inti=0; i < n; i++){ dp[0][i] = 1; }
for (inti=1; i < m; i++){ for (intj=1; j < n; j++){ dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } }
for (inti=2; i < n + 1; i++){ dp[i] = Math.min(dp[i - 1] +cost[i - 1], dp[i - 2] + cost[i - 2]); }
return dp[n]; } }
62 不同路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintuniquePaths(int m, int n) { int[][] dp = newint[m][n]; for (inti=0; i < m; i++){ dp[i][0] = 1; } for (inti=0; i < n; i++){ dp[0][i] = 1; }
for (inti=1; i < m; i++){ for (intj=1; j < n; j++){ dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } }
classSolution { publicbooleanlemonadeChange(int[] bills) { intfive=0; intten=0; for (int i : bills){ if (i == 5) five++; elseif (i == 10){ if (five < 1) returnfalse; five--; ten++; }
elseif (i == 20){ if (ten > 0 && five > 0){ ten--; five--; } elseif (five >= 3) { five = five - 3; } elsereturnfalse; } }
classSolution { public List<Integer> sequentialDigits(int low, int high) { List<Integer> result = newArrayList<>(); for (inti=1; i<= 9; i++){ intnum= i; intnextDigit= i + 1; while (num <= high && nextDigit <= 9){ num = num * 10 + nextDigit; if (low <= num && num <= high){ result.add(num); } nextDigit++; }
for (inti=0; i < nums.length; i++){ maxLen = Math.max(maxLen, i + nums[i]); if (maxLen >= nums.length - 1) return ++count; if (i == curLen){ count++; curLen = maxLen; } }
for (inti=0; i < nums.length; i++){ maxLen = Math.max(maxLen, i + nums[i]); if (maxLen >= nums.length - 1) return ++count; if (i == curLen){ count++; curLen = maxLen; } }
for (inti=0; i < nums.length; i++){ maxLen = Math.max(maxLen, i + nums[i]); if (maxLen >= nums.length - 1) returntrue; if (i == maxLen && nums[i] == 0) returnfalse; }