From 2119e411b73d818baaec8ca8140c7b619358bd54 Mon Sep 17 00:00:00 2001 From: xsduan98 Date: Fri, 30 Jul 2021 18:21:18 +0800 Subject: [PATCH 1/7] =?UTF-8?q?657.=20=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=83=BD?= =?UTF-8?q?=E5=90=A6=E8=BF=94=E5=9B=9E=E5=8E=9F=E7=82=B9=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0657.机器人能否返回原点.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index cd26836c..b1fb6f21 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -71,6 +71,21 @@ public: ## Java ```java +// 时间复杂度:O(n) +// 空间复杂度:如果采用 toCharArray,则是 O(n);如果使用 charAt,则是 O(1) +class Solution { + public boolean judgeCircle(String moves) { + int x = 0; + int y = 0; + for (char c : moves.toCharArray()) { + if (c == 'U') y++; + if (c == 'D') y--; + if (c == 'L') x++; + if (c == 'R') x--; + } + return x == 0 && y == 0; + } +} ``` ## Python From 58ed197ba94f17646969ddf0e45ed851f5b03961 Mon Sep 17 00:00:00 2001 From: xsduan98 Date: Fri, 30 Jul 2021 18:54:43 +0800 Subject: [PATCH 2/7] =?UTF-8?q?1365.=E6=9C=89=E5=A4=9A=E5=B0=91=E5=B0=8F?= =?UTF-8?q?=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E5=AD=97=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E5=AD=97=20=E6=B7=BB=E5=8A=A0Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...据数字二进制下1的数目排序.md | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index e464f716..a3d724fa 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -16,7 +16,7 @@ 换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。 以数组形式返回答案。 -  + 示例 1: 输入:nums = [8,1,2,2,3] @@ -35,7 +35,7 @@ 示例 3: 输入:nums = [7,7,7,7] 输出:[0,0,0,0] -  + 提示: * 2 <= nums.length <= 500 * 0 <= nums[i] <= 100 @@ -120,8 +120,51 @@ public: ## Java ```java +/** +* 解法一:暴力 +* 时间复杂度:O(n^2) +* 空间复杂度:O(n) +*/ +class Solution { + public int[] smallerNumbersThanCurrent(int[] nums) { + int[] res = new int[nums.length]; + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < nums.length; j++) { + if (nums[j] < nums[i] && j != i) { // 注意 j 不能和 i 重合 + res[i]++; + } + } + } + return res; + } +} ``` +```java +/** +* 优化:排序 + 哈希表 +* 时间复杂度:O(nlogn) +* 空间复杂度:O(n) +*/ +class Solution { + public int[] smallerNumbersThanCurrent(int[] nums) { + int[] res = Arrays.copyOf(nums, nums.length); + Arrays.sort(res); // 是对 res 排序,nums 中顺序还要保持 + int[] hash = new int[101]; // 使用哈希表,记录比当前元素小的元素个数 + for (int i = res.length - 1; i >= 0; i--) { // 注意:从后向前 + hash[res[i]] = i; // 排序后,当前下标即表示比当前元素小的元素个数 + } + // 此时 hash中保存的每一个元素数值 便是 小于这个数值的个数 + for (int i = 0; i < res.length; i++) { + res[i] = hash[nums[i]]; + } + return res; + } +} +``` + + + ## Python ```python @@ -143,4 +186,3 @@ public: * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
- From 4764d993b26a3a912646f810095c49142d497be8 Mon Sep 17 00:00:00 2001 From: posper Date: Sat, 31 Jul 2021 10:20:56 +0800 Subject: [PATCH 3/7] =?UTF-8?q?657.=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=83=BD?= =?UTF-8?q?=E5=90=A6=E8=BF=94=E5=9B=9E=E5=8E=9F=E7=82=B9=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0657.机器人能否返回原点.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index b1fb6f21..ba682a33 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -91,6 +91,20 @@ class Solution { ## Python ```python +class Solution: + def judgeCircle(self, moves: str) -> bool: + x = 0 # 记录当前位置 + y = 0 + for i in range(len(moves)): + if (moves[i] == 'U'): + y += 1 + if (moves[i] == 'D'): + y -= 1 + if (moves[i] == 'L'): + x += 1 + if (moves[i] == 'R'): + x -= 1 + return x == 0 and y == 0 ``` ## Go From 0e832d518357bb239d84e294e08a3ccecec669ea Mon Sep 17 00:00:00 2001 From: posper Date: Sat, 31 Jul 2021 10:28:14 +0800 Subject: [PATCH 4/7] =?UTF-8?q?657.=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=83=BD?= =?UTF-8?q?=E5=90=A6=E8=BF=94=E5=9B=9E=E5=8E=9F=E7=82=B9=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0657.机器人能否返回原点.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index ba682a33..805a81e6 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -91,6 +91,8 @@ class Solution { ## Python ```python +# 时间复杂度:O(n) +# 空间复杂度:O(1) class Solution: def judgeCircle(self, moves: str) -> bool: x = 0 # 记录当前位置 @@ -115,6 +117,19 @@ class Solution: ## JavaScript ```js +// 时间复杂度:O(n) +// 空间复杂度:O(1) +var judgeCircle = function(moves) { + var x = 0; // 记录当前位置 + var y = 0; + for (var i = 0; i < moves.length; i++) { + if (moves[i] == 'U') y++; + if (moves[i] == 'D') y--; + if (moves[i] == 'L') x++; + if (moves[i] == 'R') x--; + } + return x == 0 && y == 0; +}; ``` ----------------------- From 4851af73ab4ed58b71c1c6d1223c39d2483816ee Mon Sep 17 00:00:00 2001 From: posper Date: Sat, 31 Jul 2021 10:36:59 +0800 Subject: [PATCH 5/7] =?UTF-8?q?657.=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=83=BD?= =?UTF-8?q?=E5=90=A6=E8=BF=94=E5=9B=9E=E5=8E=9F=E7=82=B9=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0657.机器人能否返回原点.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index 805a81e6..9b43ea6c 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -112,6 +112,25 @@ class Solution: ## Go ```go +func judgeCircle(moves string) bool { + x := 0 + y := 0 + for i := 0; i < len(moves); i++ { + if moves[i] == 'U' { + y++ + } + if moves[i] == 'D' { + y-- + } + if moves[i] == 'L' { + x++ + } + if moves[i] == 'R' { + x-- + } + } + return x == 0 && y == 0; +} ``` ## JavaScript From 5d1c0883079d6e791804e086e928882a7c091831 Mon Sep 17 00:00:00 2001 From: posper Date: Sun, 1 Aug 2021 11:16:22 +0800 Subject: [PATCH 6/7] =?UTF-8?q?232.=E7=94=A8=E6=A0=88=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E9=98=9F=E5=88=97=EF=BC=8C=E4=B9=8B=E5=89=8D=E7=9A=84js?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=94=BE=E5=88=B0=E4=BA=86go=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 6890fc2b..32476d6f 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -384,7 +384,7 @@ func (this *MyQueue) Peek() int { func (this *MyQueue) Empty() bool { return len(this.stack) == 0 && len(this.back) == 0 } - +``` javaScript: @@ -442,8 +442,6 @@ MyQueue.prototype.empty = function() { ``` - - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 7757a60d0ebbe729443be57adccfe3716837dfc9 Mon Sep 17 00:00:00 2001 From: posper Date: Sun, 1 Aug 2021 11:20:13 +0800 Subject: [PATCH 7/7] =?UTF-8?q?34.=E5=9C=A8=E6=8E=92=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E4=BD=8D=E7=BD=AE=EF=BC=8CJava=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=A7=A3=E6=B3=952=E6=B3=A8=E9=87=8A=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序数组中查找元素的第一个和最后一个位置.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 97b9a9b6..f6b01dae 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -223,7 +223,7 @@ class Solution { // 解法2 // 1、首先,在 nums 数组中二分查找 target; // 2、如果二分查找失败,则 binarySearch 返回 -1,表明 nums 中没有 target。此时,searchRange 直接返回 {-1, -1}; -// 3、如果二分查找失败,则 binarySearch 返回 nums 中 为 target 的一个下标。然后,通过左右滑动指针,来找到符合题意的区间 +// 3、如果二分查找成功,则 binarySearch 返回 nums 中值为 target 的一个下标。然后,通过左右滑动指针,来找到符合题意的区间 class Solution { public int[] searchRange(int[] nums, int target) {