mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(0377.组合总和Ⅳ.md):增加typescript版本
This commit is contained in:
@ -221,7 +221,27 @@ const combinationSum4 = (nums, target) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function combinationSum4(nums: number[], target: number): number {
|
||||
const dp: number[] = new Array(target + 1).fill(0);
|
||||
dp[0] = 1;
|
||||
// 遍历背包
|
||||
for (let i = 1; i <= target; i++) {
|
||||
// 遍历物品
|
||||
for (let j = 0, length = nums.length; j < length; j++) {
|
||||
if (i >= nums[j]) {
|
||||
dp[i] += dp[i - nums[j]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[target];
|
||||
};
|
||||
```
|
||||
|
||||
Rust
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
pub fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
|
||||
|
Reference in New Issue
Block a user