From dd00b57be87e9c4e25267c5ab6cc53f6d714a97e Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:43:18 -0500 Subject: [PATCH 01/15] =?UTF-8?q?Update=200704.=E4=BA=8C=E5=88=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0704.二分查找.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 1e474f9a..a468cc44 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -218,19 +218,21 @@ class Solution: (版本二)左闭右开区间 -```python -class Solution: +```class Solution: def search(self, nums: List[int], target: int) -> int: - left,right =0, len(nums) - while left < right: - mid = (left + right) // 2 - if nums[mid] < target: - left = mid+1 - elif nums[mid] > target: - right = mid + if nums is None or len(nums)==0: + return -1 + l=0 + r=len(nums)-1 + while (l<=r): + m = round(l+(r-l)/2) + if nums[m] == target: + return m + elif nums[m] > target: + r=m-1 else: - return mid - return -1 + l=m+1 + return -1 ``` **Go:** From f49b2e4a75c6f98c082923e1098aedc5cb27574b Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:49:32 -0500 Subject: [PATCH 02/15] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the former code has not consider if nums is None or len(nums)==0 --- problems/0027.移除元素.md | 38 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 4b50d666..b239136c 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -173,28 +173,24 @@ class Solution { Python: -```python +```python3 class Solution: - """双指针法 - 时间复杂度:O(n) - 空间复杂度:O(1) - """ - - @classmethod - def removeElement(cls, nums: List[int], val: int) -> int: - fast = slow = 0 - - while fast < len(nums): - - if nums[fast] != val: - nums[slow] = nums[fast] - slow += 1 - - # 当 fast 指针遇到要删除的元素时停止赋值 - # slow 指针停止移动, fast 指针继续前进 - fast += 1 - - return slow + def removeElement(self, nums: List[int], val: int) -> int: + if nums is None or len(nums)==0: + return 0 + l=0 + r=len(nums)-1 + while l Date: Mon, 6 Jun 2022 22:11:16 -0500 Subject: [PATCH 03/15] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python3 version of brutal force --- problems/0977.有序数组的平方.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 0e79a3d6..d3da662f 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -39,6 +39,15 @@ public: } }; ``` +```python3 +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + res=[] + for num in nums: + res.append(num**2) + return sorted(res) +``` + 这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。 From b10f7edba037886e736e58bbe0488ab078efdaa2 Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Mon, 6 Jun 2022 22:17:15 -0500 Subject: [PATCH 04/15] =?UTF-8?q?Update=200209.=E9=95=BF=E5=BA=A6=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 滑动窗口 version of python3 code --- problems/0209.长度最小的子数组.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index fbef7692..160f93bb 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -162,8 +162,27 @@ class Solution: index += 1 return 0 if res==float("inf") else res ``` - - +```python3 +#滑动窗口 +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + if nums is None or len(nums)==0: + return 0 + lenf=len(nums)+1 + total=0 + i=j=0 + while (j=target): + lenf=min(lenf,j-i) + total=total-nums[i] + i+=1 + if lenf==len(nums)+1: + return 0 + else: + return lenf +``` Go: ```go func minSubArrayLen(target int, nums []int) int { From b61afe9aee786592c313afb42b07295d0c1f4e00 Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Tue, 7 Jun 2022 12:30:18 -0500 Subject: [PATCH 05/15] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit for loop version of python3 solution --- problems/0054.螺旋矩阵.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index ccf6f471..27899d51 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -171,6 +171,30 @@ class Solution: return res ``` - +```python3 +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + r=len(matrix) + if r == 0 or len(matrix[0])==0: + return [] + c=len(matrix[0]) + res=matrix[0] + + if r>1: + for i in range (1,r): + res.append(matrix[i][c-1]) + for j in range(c-2, -1, -1): + res.append(matrix[r-1][j]) + if c>1: + for i in range(r-2, 0, -1): + res.append(matrix[i][0]) + + M=[] + for k in range(1, r-1): + e=matrix[k][1:-1] + M.append(e) + + return res+self.spiralOrder(M) +``` -----------------------
From ba31161609d4131a5d039046435a8f21805e301d Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Wed, 8 Jun 2022 17:47:49 -0500 Subject: [PATCH 06/15] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.07?= =?UTF-8?q?.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题02.07.链表相交.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 0a38cc33..ba6631a4 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -160,6 +160,8 @@ class Solution: 那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个 位置相遇 """ + if headA is None or headB is None: + return None cur_a, cur_b = headA, headB # 用两个指针代替a和b From c9900267505297634675172ad670e86a2269cace Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Thu, 9 Jun 2022 21:11:08 -0500 Subject: [PATCH 07/15] =?UTF-8?q?Update=200234.=E5=9B=9E=E6=96=87=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For both solution of python3, there are shorter and more efficient ways to write it. For the #数组模拟, it can be solved more easily by convert the linked list to a list #反转后半部分链表, the original version define to function, isPalindrome, and reverseList. That's too complicated... No need. --- problems/0234.回文链表.md | 80 ++++++++++++++--------------------- 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index db910d4e..bbfe4e91 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -218,59 +218,41 @@ class Solution { ```python #数组模拟 class Solution: - def isPalindrome(self, head: ListNode) -> bool: - length = 0 - tmp = head - while tmp: #求链表长度 - length += 1 - tmp = tmp.next - - result = [0] * length - tmp = head - index = 0 - while tmp: #链表元素加入数组 - result[index] = tmp.val - index += 1 - tmp = tmp.next - - i, j = 0, length - 1 - while i < j: # 判断回文 - if result[i] != result[j]: + def isPalindrome(self, head: Optional[ListNode]) -> bool: + list=[] + while head: + list.append(head.val) + head=head.next + l,r=0, len(list)-1 + while l<=r: + if list[l]!=list[r]: return False - i += 1 - j -= 1 - return True - + l+=1 + r-=1 + return True + #反转后半部分链表 class Solution: - def isPalindrome(self, head: ListNode) -> bool: - if head == None or head.next == None: - return True - slow, fast = head, head - while fast and fast.next: - pre = slow - slow = slow.next - fast = fast.next.next - - pre.next = None # 分割链表 - cur1 = head # 前半部分 - cur2 = self.reverseList(slow) # 反转后半部分,总链表长度如果是奇数,cur2比cur1多一个节点 - while cur1: - if cur1.val != cur2.val: - return False - cur1 = cur1.next - cur2 = cur2.next - return True + def isPalindrome(self, head: Optional[ListNode]) -> bool: + fast = slow = head - def reverseList(self, head: ListNode) -> ListNode: - cur = head - pre = None - while(cur!=None): - temp = cur.next # 保存一下cur的下一个节点 - cur.next = pre # 反转 - pre = cur - cur = temp - return pre + # find mid point which including (first) mid point into the first half linked list + while fast and fast.next: + fast = fast.next.next + slow = slow.next + node = None + + # reverse second half linked list + while slow: + slow.next, slow, node = node, slow.next, slow + + # compare reversed and original half; must maintain reversed linked list is shorter than 1st half + while node: + if node.val != head.val: + return False + node = node.next + head = head.next + return True ``` ## Go From 882c19c3e263161a18d4349f53ee2226a09a3c6b Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 12 Jun 2022 10:17:49 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201005.K=E6=AC=A1?= =?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1005.K次取反后最大化的数组和.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 202534da..71fc628f 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -289,6 +289,28 @@ function largestSumAfterKNegations(nums: number[], k: number): number { }; ``` +### Scala + +```scala +object Solution { + def largestSumAfterKNegations(nums: Array[Int], k: Int): Int = { + var num = nums.sortWith(math.abs(_) > math.abs(_)) + + var kk = k // 因为k是不可变量,所以要赋值给一个可变量 + for (i <- num.indices) { + if (num(i) < 0 && kk > 0) { + num(i) *= -1 // 取反 + kk -= 1 + } + } + + // kk对2取余,结果为0则为偶数不需要取反,结果为1为奇数,只需要对最后的数字进行反转就可以 + if (kk % 2 == 1) num(num.size - 1) *= -1 + + num.sum // 最后返回数字的和 + } +} +``` From 1da68a332c35b178d62edf946fd7a4da4ac98d3c Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 12 Jun 2022 13:46:58 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index a88f677d..541be293 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -471,5 +471,73 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ } ``` +### Scala + +暴力解法: + +```scala +object Solution { + def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = { + for (i <- cost.indices) { + var rest = gas(i) - cost(i) + var index = (i + 1) % cost.length // index为i的下一个节点 + while (rest > 0 && i != index) { + rest += (gas(index) - cost(index)) + index = (index + 1) % cost.length + } + if (rest >= 0 && index == i) return i + } + -1 + } +} +``` + +贪心算法,方法一: + +```scala +object Solution { + def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = { + var curSum = 0 + var min = Int.MaxValue + for (i <- gas.indices) { + var rest = gas(i) - cost(i) + curSum += rest + min = math.min(min, curSum) + } + if (curSum < 0) return -1 // 情况1: gas的总和小于cost的总和,不可能到达终点 + if (min >= 0) return 0 // 情况2: 最小值>=0,从0号出发可以直接到达 + // 情况3: min为负值,从后向前看,能把min填平的节点就是出发节点 + for (i <- gas.length - 1 to 0 by -1) { + var rest = gas(i) - cost(i) + min += rest + if (min >= 0) return i + } + -1 + } +} +``` + +贪心算法,方法二: + +```scala +object Solution { + def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = { + var curSum = 0 + var totalSum = 0 + var start = 0 + for (i <- gas.indices) { + curSum += (gas(i) - cost(i)) + totalSum += (gas(i) - cost(i)) + if (curSum < 0) { + start = i + 1 // 起始位置更新 + curSum = 0 // curSum从0开始 + } + } + if (totalSum < 0) return -1 // 说明怎么走不可能跑一圈 + start + } +} +``` + -----------------------
From 998785bcab1346d652b8b3db9ccd166054194a83 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 12 Jun 2022 16:59:08 -0700 Subject: [PATCH 10/15] =?UTF-8?q?Update=200034.=E5=9C=A8=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加力扣原题链接 --- ...序数组中查找元素的第一个和最后一个位置.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 260462c2..b6e82262 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -7,6 +7,8 @@ # 34. 在排序数组中查找元素的第一个和最后一个位置 +[力扣链接](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/) + 给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。 如果数组中不存在目标值 target,返回 [-1, -1]。 From 0a3a2d4ae2213cad03d8f339f09f4e9de7420998 Mon Sep 17 00:00:00 2001 From: Rinko Taketsuki <33001553+RinkoTaketsuki@users.noreply.github.com> Date: Mon, 13 Jun 2022 09:27:00 +0800 Subject: [PATCH 11/15] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index a51c68ee..1d8a0e64 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -49,7 +49,7 @@ 数组长度为:len。 -如果len % (len - (next[len - 1] + 1)) == 0 ,则说明 (数组长度-最长相等前后缀的长度) 正好可以被 数组的长度整除,说明有该字符串有重复的子字符串。 +如果len % (len - (next[len - 1] + 1)) == 0 ,则说明数组的长度正好可以被 (数组长度-最长相等前后缀的长度) 整除 ,说明该字符串有重复的子字符串。 **数组长度减去最长相同前后缀的长度相当于是第一个周期的长度,也就是一个周期的长度,如果这个周期可以被整除,就说明整个数组就是这个周期的循环。** From 8f52696f2206f2361917aad3e1cc7f87698c5cc0 Mon Sep 17 00:00:00 2001 From: Rinko Taketsuki <33001553+RinkoTaketsuki@users.noreply.github.com> Date: Tue, 14 Jun 2022 09:24:58 +0800 Subject: [PATCH 12/15] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 1d8a0e64..ba2fa160 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -63,7 +63,7 @@ next[len - 1] = 7,next[len - 1] + 1 = 8,8就是此时字符串asdfasdfasdf的最长相同前后缀的长度。 -(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 4正好可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。 +(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 12 正好可以被 4 整除,所以说明有重复的子字符串(asdf)。 C++代码如下:(这里使用了前缀表统一减一的实现方式) From 0b9737d7541870e8f68d728fd0a0197d281b4c1d Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Thu, 16 Jun 2022 21:24:24 -0500 Subject: [PATCH 13/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A00028.=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?strStr=20python=E7=89=88=E6=9C=AC=E6=9A=B4=E5=8A=9B=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0028.实现strStr.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 1cdd5292..00997907 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -685,7 +685,21 @@ class Solution { ``` Python3: - +```python +//暴力解法: +class Solution(object): + def strStr(self, haystack, needle): + """ + :type haystack: str + :type needle: str + :rtype: int + """ + m,n=len(haystack),len(needle) + for i in range(m): + if haystack[i:i+n]==needle: + return i + return -1 +``` ```python // 方法一 class Solution: From cbaa9df25b535c7b1ff040bdc30631603e1cd3f7 Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Sat, 18 Jun 2022 16:58:58 -0500 Subject: [PATCH 14/15] =?UTF-8?q?=E4=BC=98=E5=8C=960925.=E9=95=BF=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E5=85=A5python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0925.长按键入.md | 38 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 0ef5a3d7..7aab71a2 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -129,29 +129,21 @@ class Solution { ``` ### Python ```python -class Solution: - def isLongPressedName(self, name: str, typed: str) -> bool: - i, j = 0, 0 - m, n = len(name) , len(typed) - while i< m and j < n: - if name[i] == typed[j]: # 相同时向后匹配 - i += 1 - j += 1 - else: # 不相同 - if j == 0: return False # 如果第一位不相同,直接返回false - # 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz" - while j < n - 1 and typed[j] == typed[j-1]: j += 1 - if name[i] == typed[j]: - i += 1 - j += 1 - else: return False - # 说明name没有匹配完 - if i < m: return False - # 说明type没有匹配完 - while j < n: - if typed[j] == typed[j-1]: j += 1 - else: return False - return True + i = j = 0 + while(i Date: Thu, 21 Jul 2022 10:33:02 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200046.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0046.全排列 Rust版本 --- problems/0046.全排列.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 06e1550a..ce07395a 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -359,6 +359,36 @@ function permute(nums: number[]): number[][] { }; ``` +### Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, used: &mut Vec) { + let len = nums.len(); + if path.len() == len { + result.push(path.clone()); + return; + } + for i in 0..len { + if used[i] == true { continue; } + used[i] = true; + path.push(nums[i]); + Self::backtracking(result, path, nums, used); + path.pop(); + used[i] = false; + } + } + + pub fn permute(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + let mut used = vec![false; nums.len()]; + Self::backtracking(&mut result, &mut path, &nums, &mut used); + result + } +} +``` + ### C ```c