mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #610 from Steve0x2a/master
添加0724寻找数组的中心索引 Python3版本等
This commit is contained in:
@ -165,7 +165,32 @@ public:
|
||||
Java:
|
||||
|
||||
Python:
|
||||
```python3
|
||||
class Solution:
|
||||
def sumNumbers(self, root: TreeNode) -> int:
|
||||
res = 0
|
||||
path = []
|
||||
def backtrace(root):
|
||||
nonlocal res
|
||||
if not root: return # 节点空则返回
|
||||
path.append(root.val)
|
||||
if not root.left and not root.right: # 遇到了叶子节点
|
||||
res += get_sum(path)
|
||||
if root.left: # 左子树不空
|
||||
backtrace(root.left)
|
||||
if root.right: # 右子树不空
|
||||
backtrace(root.right)
|
||||
path.pop()
|
||||
|
||||
def get_sum(arr):
|
||||
s = 0
|
||||
for i in range(len(arr)):
|
||||
s = s * 10 + arr[i]
|
||||
return s
|
||||
|
||||
backtrace(root)
|
||||
return res
|
||||
```
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
@ -222,7 +222,61 @@ public class ReorderList {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
# 方法二 双向队列
|
||||
class Solution:
|
||||
def reorderList(self, head: ListNode) -> None:
|
||||
"""
|
||||
Do not return anything, modify head in-place instead.
|
||||
"""
|
||||
d = collections.deque()
|
||||
tmp = head
|
||||
while tmp.next: # 链表除了首元素全部加入双向队列
|
||||
d.append(tmp.next)
|
||||
tmp = tmp.next
|
||||
tmp = head
|
||||
while len(d): # 一后一前加入链表
|
||||
tmp.next = d.pop()
|
||||
tmp = tmp.next
|
||||
if len(d):
|
||||
tmp.next = d.popleft()
|
||||
tmp = tmp.next
|
||||
tmp.next = None # 尾部置空
|
||||
|
||||
# 方法三 反转链表
|
||||
class Solution:
|
||||
def reorderList(self, head: ListNode) -> None:
|
||||
if head == None or head.next == None:
|
||||
return True
|
||||
slow, fast = head, head
|
||||
while fast and fast.next:
|
||||
slow = slow.next
|
||||
fast = fast.next.next
|
||||
right = slow.next # 分割右半边
|
||||
slow.next = None # 切断
|
||||
right = self.reverseList(right) #反转右半边
|
||||
left = head
|
||||
# 左半边一定比右半边长, 因此判断右半边即可
|
||||
while right:
|
||||
curLeft = left.next
|
||||
left.next = right
|
||||
left = curLeft
|
||||
|
||||
curRight = right.next
|
||||
right.next = left
|
||||
right = curRight
|
||||
|
||||
|
||||
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
|
||||
```
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
@ -148,7 +148,62 @@ public:
|
||||
|
||||
## Python
|
||||
|
||||
```python
|
||||
```python3
|
||||
#数组模拟
|
||||
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]:
|
||||
return False
|
||||
i += 1
|
||||
j -= 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 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
|
||||
```
|
||||
|
||||
## Go
|
||||
|
@ -89,7 +89,16 @@ class Solution {
|
||||
|
||||
## Python
|
||||
|
||||
```python
|
||||
```python3
|
||||
class Solution:
|
||||
def pivotIndex(self, nums: List[int]) -> int:
|
||||
numSum = sum(nums) #数组总和
|
||||
leftSum = 0
|
||||
for i in range(len(nums)):
|
||||
if numSum - leftSum -nums[i] == leftSum: #左右和相等
|
||||
return i
|
||||
leftSum += nums[i]
|
||||
return -1
|
||||
```
|
||||
|
||||
## Go
|
||||
|
@ -149,7 +149,32 @@ class Solution {
|
||||
|
||||
## Python
|
||||
|
||||
```python
|
||||
```python3
|
||||
#方法2
|
||||
class Solution:
|
||||
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
|
||||
result = [0]*len(nums)
|
||||
evenIndex = 0
|
||||
oddIndex = 1
|
||||
for i in range(len(nums)):
|
||||
if nums[i] % 2: #奇数
|
||||
result[oddIndex] = nums[i]
|
||||
oddIndex += 2
|
||||
else: #偶数
|
||||
result[evenIndex] = nums[i]
|
||||
evenIndex += 2
|
||||
return result
|
||||
|
||||
#方法3
|
||||
class Solution:
|
||||
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
|
||||
oddIndex = 1
|
||||
for i in range(0,len(nums),2): #步长为2
|
||||
if nums[i] % 2: #偶数位遇到奇数
|
||||
while nums[oddIndex] % 2: #奇数位找偶数
|
||||
oddIndex += 2
|
||||
nums[i], nums[oddIndex] = nums[oddIndex], nums[i]
|
||||
return nums
|
||||
```
|
||||
|
||||
## Go
|
||||
|
Reference in New Issue
Block a user