mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 00:06:28 +08:00
fix climbing_stairs_constraint_dp code
This commit is contained in:
parent
628d8a516b
commit
a47a974a6a
11 changed files with 11 additions and 11 deletions
|
@ -9,7 +9,7 @@
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
int climbingStairsConstraintDP(int n) {
|
int climbingStairsConstraintDP(int n) {
|
||||||
if (n == 1 || n == 2) {
|
if (n == 1 || n == 2) {
|
||||||
return n;
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
vector<vector<int>> dp(n + 1, vector<int>(3, 0));
|
vector<vector<int>> dp(n + 1, vector<int>(3, 0));
|
||||||
|
|
|
@ -10,7 +10,7 @@ public class climbing_stairs_constraint_dp {
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
public int climbingStairsConstraintDP(int n) {
|
public int climbingStairsConstraintDP(int n) {
|
||||||
if (n == 1 || n == 2) {
|
if (n == 1 || n == 2) {
|
||||||
return n;
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
int[,] dp = new int[n + 1, 3];
|
int[,] dp = new int[n + 1, 3];
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
int climbingStairsConstraintDP(int n) {
|
int climbingStairsConstraintDP(int n) {
|
||||||
if (n == 1 || n == 2) {
|
if (n == 1 || n == 2) {
|
||||||
return n;
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));
|
List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));
|
||||||
|
|
|
@ -7,7 +7,7 @@ package chapter_dynamic_programming
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
func climbingStairsConstraintDP(n int) int {
|
func climbingStairsConstraintDP(n int) int {
|
||||||
if n == 1 || n == 2 {
|
if n == 1 || n == 2 {
|
||||||
return n
|
return 1
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
dp := make([][3]int, n+1)
|
dp := make([][3]int, n+1)
|
||||||
|
|
|
@ -10,7 +10,7 @@ public class climbing_stairs_constraint_dp {
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
static int climbingStairsConstraintDP(int n) {
|
static int climbingStairsConstraintDP(int n) {
|
||||||
if (n == 1 || n == 2) {
|
if (n == 1 || n == 2) {
|
||||||
return n;
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
int[][] dp = new int[n + 1][3];
|
int[][] dp = new int[n + 1][3];
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
function climbingStairsConstraintDP(n) {
|
function climbingStairsConstraintDP(n) {
|
||||||
if (n === 1 || n === 2) {
|
if (n === 1 || n === 2) {
|
||||||
return n;
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
const dp = Array.from(new Array(n + 1), () => new Array(3));
|
const dp = Array.from(new Array(n + 1), () => new Array(3));
|
||||||
|
|
|
@ -8,7 +8,7 @@ Author: Krahets (krahets@163.com)
|
||||||
def climbing_stairs_constraint_dp(n: int) -> int:
|
def climbing_stairs_constraint_dp(n: int) -> int:
|
||||||
"""带约束爬楼梯:动态规划"""
|
"""带约束爬楼梯:动态规划"""
|
||||||
if n == 1 or n == 2:
|
if n == 1 or n == 2:
|
||||||
return n
|
return 1
|
||||||
# 初始化 dp 表,用于存储子问题的解
|
# 初始化 dp 表,用于存储子问题的解
|
||||||
dp = [[0] * 3 for _ in range(n + 1)]
|
dp = [[0] * 3 for _ in range(n + 1)]
|
||||||
# 初始状态:预设最小子问题的解
|
# 初始状态:预设最小子问题的解
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
fn climbing_stairs_constraint_dp(n: usize) -> i32 {
|
fn climbing_stairs_constraint_dp(n: usize) -> i32 {
|
||||||
if n == 1 || n == 2 { return n as i32 };
|
if n == 1 || n == 2 { return 1 };
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
let mut dp = vec![vec![-1; 3]; n + 1];
|
let mut dp = vec![vec![-1; 3]; n + 1];
|
||||||
// 初始状态:预设最小子问题的解
|
// 初始状态:预设最小子问题的解
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
func climbingStairsConstraintDP(n: Int) -> Int {
|
func climbingStairsConstraintDP(n: Int) -> Int {
|
||||||
if n == 1 || n == 2 {
|
if n == 1 || n == 2 {
|
||||||
return n
|
return 1
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)
|
var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
/* 带约束爬楼梯:动态规划 */
|
/* 带约束爬楼梯:动态规划 */
|
||||||
function climbingStairsConstraintDP(n: number): number {
|
function climbingStairsConstraintDP(n: number): number {
|
||||||
if (n === 1 || n === 2) {
|
if (n === 1 || n === 2) {
|
||||||
return n;
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
const dp = Array.from(
|
const dp = Array.from(
|
||||||
|
|
|
@ -7,7 +7,7 @@ const std = @import("std");
|
||||||
// 带约束爬楼梯:动态规划
|
// 带约束爬楼梯:动态规划
|
||||||
fn climbingStairsConstraintDP(comptime n: usize) i32 {
|
fn climbingStairsConstraintDP(comptime n: usize) i32 {
|
||||||
if (n == 1 or n == 2) {
|
if (n == 1 or n == 2) {
|
||||||
return @intCast(n);
|
return 1;
|
||||||
}
|
}
|
||||||
// 初始化 dp 表,用于存储子问题的解
|
// 初始化 dp 表,用于存储子问题的解
|
||||||
var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);
|
var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);
|
||||||
|
|
Loading…
Reference in a new issue