From be40b09991fed2d2f2612c24c8cb5115c2fcb7b0 Mon Sep 17 00:00:00 2001 From: yqq Date: Mon, 14 Jun 2021 16:34:46 +0800 Subject: [PATCH 1/2] adddddd --- problems/0035.搜索插入位置.md | 4 ++-- problems/0070.爬楼梯完全背包版本.md | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index e891e3c5..a8da3045 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -76,7 +76,7 @@ public: ``` 时间复杂度:O(n) -时间复杂度:O(1) +空间复杂度:O(1) 效率如下: @@ -158,7 +158,7 @@ public: **大家要仔细看注释,思考为什么要写while (left < right), 为什么要写right = middle**。 -``` +```cpp class Solution { public: int searchInsert(vector& nums, int target) { diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index d6b12450..f92e6716 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -88,7 +88,8 @@ 以上分析完毕,C++代码如下: -``` + +```cpp class Solution { public: int climbStairs(int n) { From 34444b3a4d7c5fb5eb9d9bc61bc300c47e08a39d Mon Sep 17 00:00:00 2001 From: yqq Date: Sat, 21 Aug 2021 18:00:54 +0800 Subject: [PATCH 2/2] fix 0494 --- problems/0494.目标和.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 77b3c76d..5d89f7dc 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -19,15 +19,15 @@ 示例: -输入:nums: [1, 1, 1, 1, 1], S: 3 -输出:5 +输入:nums: [1, 1, 1, 1, 1], S: 3 +输出:5 -解释: --1+1+1+1+1 = 3 -+1-1+1+1+1 = 3 -+1+1-1+1+1 = 3 -+1+1+1-1+1 = 3 -+1+1+1+1-1 = 3 +解释: +-1+1+1+1+1 = 3 ++1-1+1+1+1 = 3 ++1+1-1+1+1 = 3 ++1+1+1-1+1 = 3 ++1+1+1+1-1 = 3 一共有5种方法让最终目标和为3。 @@ -202,6 +202,7 @@ public: for (int i = 0; i < nums.size(); i++) sum += nums[i]; if (S > sum) return 0; // 此时没有方案 if ((S + sum) % 2 == 1) return 0; // 此时没有方案 + if (S + sum < 0) return 0; // 以确保bagSize为正数 int bagSize = (S + sum) / 2; vector dp(bagSize + 1, 0); dp[0] = 1; @@ -311,7 +312,7 @@ Javascript: const findTargetSumWays = (nums, target) => { const sum = nums.reduce((a, b) => a+b); - + if(target > sum) { return 0; }