Skip to content

Commit 9e36e5c

Browse files
committed
update
1 parent 58183b4 commit 9e36e5c

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

P106. 寻找峰值.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
***峰值元素是指其值严格大于左右相邻值的元素。给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。***
2+
3+
```
4+
输入:nums = [1,2,3,1]
5+
输出:2
6+
解释:3 是峰值元素,你的函数应该返回其索引 2。
7+
```
8+
9+
```
10+
class Solution:
11+
def findPeakElement(self, nums: List[int]) -> int:
12+
i = 0
13+
j = len(nums)-1
14+
while i < j:
15+
mid = (i+j)//2
16+
if nums[mid] > nums[mid+1]:
17+
j = mid
18+
else:
19+
i = mid+1
20+
return i
21+
```

P107. 四数相加 II.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```
2+
给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
3+
4+
0 <= i, j, k, l < n
5+
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
6+
```
7+
8+
```
9+
class Solution:
10+
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
11+
cnt = 0
12+
countAB = collections.Counter([a+b for a in nums1 for b in nums2])
13+
for c in nums3:
14+
for d in nums4:
15+
countCD = c+d
16+
if -countCD in countAB:
17+
cnt += countAB[-countCD]
18+
return cnt
19+
```

P108. 奇偶链表.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
![algo45](./images/algo45.jpg)
2+
3+
```
4+
# Definition for singly-linked list.
5+
# class ListNode:
6+
# def __init__(self, val=0, next=None):
7+
# self.val = val
8+
# self.next = next
9+
class Solution:
10+
def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
11+
if not head:
12+
return None
13+
oddhead = head
14+
evenhead = head.next
15+
odd, even = oddhead, evenhead
16+
while even and even.next:
17+
odd.next = even.next
18+
odd = odd.next
19+
even.next = odd.next
20+
even = even.next
21+
odd.next = evenhead
22+
return oddhead
23+
```

images/algo45.jpg

30.4 KB
Loading

0 commit comments

Comments
 (0)