diff --git a/codes/cpp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cpp b/codes/cpp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cpp index ca3210261..bee5d4a4e 100644 --- a/codes/cpp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cpp +++ b/codes/cpp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cpp @@ -9,7 +9,7 @@ /* 带约束爬楼梯:动态规划 */ int climbingStairsConstraintDP(int n) { if (n == 1 || n == 2) { - return n; + return 1; } // 初始化 dp 表,用于存储子问题的解 vector> dp(n + 1, vector(3, 0)); diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs index 099efc106..c1f035b0f 100644 --- a/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs @@ -10,7 +10,7 @@ public class climbing_stairs_constraint_dp { /* 带约束爬楼梯:动态规划 */ public int climbingStairsConstraintDP(int n) { if (n == 1 || n == 2) { - return n; + return 1; } // 初始化 dp 表,用于存储子问题的解 int[,] dp = new int[n + 1, 3]; diff --git a/codes/dart/chapter_dynamic_programming/climbing_stairs_constraint_dp.dart b/codes/dart/chapter_dynamic_programming/climbing_stairs_constraint_dp.dart index 1740b36a3..0d07c00be 100644 --- a/codes/dart/chapter_dynamic_programming/climbing_stairs_constraint_dp.dart +++ b/codes/dart/chapter_dynamic_programming/climbing_stairs_constraint_dp.dart @@ -7,7 +7,7 @@ /* 带约束爬楼梯:动态规划 */ int climbingStairsConstraintDP(int n) { if (n == 1 || n == 2) { - return n; + return 1; } // 初始化 dp 表,用于存储子问题的解 List> dp = List.generate(n + 1, (index) => List.filled(3, 0)); diff --git a/codes/go/chapter_dynamic_programming/climbing_stairs_constraint_dp.go b/codes/go/chapter_dynamic_programming/climbing_stairs_constraint_dp.go index 2621b3939..1653d6939 100644 --- a/codes/go/chapter_dynamic_programming/climbing_stairs_constraint_dp.go +++ b/codes/go/chapter_dynamic_programming/climbing_stairs_constraint_dp.go @@ -7,7 +7,7 @@ package chapter_dynamic_programming /* 带约束爬楼梯:动态规划 */ func climbingStairsConstraintDP(n int) int { if n == 1 || n == 2 { - return n + return 1 } // 初始化 dp 表,用于存储子问题的解 dp := make([][3]int, n+1) diff --git a/codes/java/chapter_dynamic_programming/climbing_stairs_constraint_dp.java b/codes/java/chapter_dynamic_programming/climbing_stairs_constraint_dp.java index eac9166bf..5f717cd67 100644 --- a/codes/java/chapter_dynamic_programming/climbing_stairs_constraint_dp.java +++ b/codes/java/chapter_dynamic_programming/climbing_stairs_constraint_dp.java @@ -10,7 +10,7 @@ public class climbing_stairs_constraint_dp { /* 带约束爬楼梯:动态规划 */ static int climbingStairsConstraintDP(int n) { if (n == 1 || n == 2) { - return n; + return 1; } // 初始化 dp 表,用于存储子问题的解 int[][] dp = new int[n + 1][3]; diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_constraint_dp.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_constraint_dp.js index 67f024f14..54a3b930f 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_constraint_dp.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_constraint_dp.js @@ -7,7 +7,7 @@ /* 带约束爬楼梯:动态规划 */ function climbingStairsConstraintDP(n) { if (n === 1 || n === 2) { - return n; + return 1; } // 初始化 dp 表,用于存储子问题的解 const dp = Array.from(new Array(n + 1), () => new Array(3)); diff --git a/codes/python/chapter_dynamic_programming/climbing_stairs_constraint_dp.py b/codes/python/chapter_dynamic_programming/climbing_stairs_constraint_dp.py index 261ec230e..4c20d7982 100644 --- a/codes/python/chapter_dynamic_programming/climbing_stairs_constraint_dp.py +++ b/codes/python/chapter_dynamic_programming/climbing_stairs_constraint_dp.py @@ -8,7 +8,7 @@ Author: Krahets (krahets@163.com) def climbing_stairs_constraint_dp(n: int) -> int: """带约束爬楼梯:动态规划""" if n == 1 or n == 2: - return n + return 1 # 初始化 dp 表,用于存储子问题的解 dp = [[0] * 3 for _ in range(n + 1)] # 初始状态:预设最小子问题的解 diff --git a/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs b/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs index 675e050c8..e7cce8108 100644 --- a/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs +++ b/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs @@ -6,7 +6,7 @@ /* 带约束爬楼梯:动态规划 */ 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 表,用于存储子问题的解 let mut dp = vec![vec![-1; 3]; n + 1]; // 初始状态:预设最小子问题的解 diff --git a/codes/swift/chapter_dynamic_programming/climbing_stairs_constraint_dp.swift b/codes/swift/chapter_dynamic_programming/climbing_stairs_constraint_dp.swift index c4a879f4b..a40425211 100644 --- a/codes/swift/chapter_dynamic_programming/climbing_stairs_constraint_dp.swift +++ b/codes/swift/chapter_dynamic_programming/climbing_stairs_constraint_dp.swift @@ -7,7 +7,7 @@ /* 带约束爬楼梯:动态规划 */ func climbingStairsConstraintDP(n: Int) -> Int { if n == 1 || n == 2 { - return n + return 1 } // 初始化 dp 表,用于存储子问题的解 var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1) diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts index 2f3b21186..df38263ea 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts @@ -7,7 +7,7 @@ /* 带约束爬楼梯:动态规划 */ function climbingStairsConstraintDP(n: number): number { if (n === 1 || n === 2) { - return n; + return 1; } // 初始化 dp 表,用于存储子问题的解 const dp = Array.from( diff --git a/codes/zig/chapter_dynamic_programming/climbing_stairs_constraint_dp.zig b/codes/zig/chapter_dynamic_programming/climbing_stairs_constraint_dp.zig index ce2619c31..dbe2085c2 100644 --- a/codes/zig/chapter_dynamic_programming/climbing_stairs_constraint_dp.zig +++ b/codes/zig/chapter_dynamic_programming/climbing_stairs_constraint_dp.zig @@ -7,7 +7,7 @@ const std = @import("std"); // 带约束爬楼梯:动态规划 fn climbingStairsConstraintDP(comptime n: usize) i32 { if (n == 1 or n == 2) { - return @intCast(n); + return 1; } // 初始化 dp 表,用于存储子问题的解 var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);