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 1/9] =?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 2/9] =?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 3/9] =?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 4/9] =?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 5/9] =?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 6/9] =?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 7/9] =?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 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 8/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A00028.=E5=AE=9E=E7=8E=B0st?= =?UTF-8?q?rStr=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 9/9] =?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