Skip to content

Commit a54f348

Browse files
authored
Merge pull request cy69855522#17 from wuyudi/patch-1
add solution
2 parents 8e18b3c + 65406f8 commit a54f348

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,13 @@ class Solution:
14731473
def isIsomorphic(self, s: str, t: str) -> bool:
14741474
return [*map(s.index, s)] == [*map(t.index, t)]
14751475
```
1476+
1477+
```python
1478+
class Solution:
1479+
def isIsomorphic(self, s: str, t: str) -> bool:
1480+
return all(s.index(i) == t.index(j) for i,j in zip(s,t))
1481+
```
1482+
14761483
- 同构代表两个字符串中每个位置上字符在自身第一次出现的索引相同
14771484
## [206. Reverse Linked List 2行](https://leetcode.com/problems/reverse-linked-list/)
14781485
```python
@@ -2297,6 +2304,22 @@ class Solution:
22972304
return [x for x in d if d[x] == min(d.values())]
22982305
```
22992306
- 使用字典记录{共同喜欢的商店:索引和},返回索引和并列最小的商店名
2307+
## [605. Can-place-flowers 2行](https://leetcode.com/problems/can-place-flowers/)
2308+
```python
2309+
class Solution:
2310+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
2311+
s = "".join(str(i) for i in [0, *flowerbed, 0]).split("1")
2312+
return n <= sum((len(i) - 1) // 2 for i in s)
2313+
```
2314+
- 两边都加 0, 然后按 1 分割
2315+
## [643. 子数组最大平均数 I 2行](https://leetcode.com/problems/maximum-average-subarray-i/)
2316+
```python
2317+
class Solution:
2318+
def findMaxAverage(self, nums: List[int], k: int) -> float:
2319+
presum = [0, *accumulate(nums, add)]
2320+
return max(presum[i + 1] - presum[i + 1 - k] for i in range(k - 1, len(nums))) / float(k)
2321+
```
2322+
- 前缀和
23002323
## [652. Find Duplicate Subtrees 8行](https://leetcode.com/problems/find-duplicate-subtrees/)
23012324
```python
23022325
# Definition for a binary tree node.
@@ -2404,6 +2427,13 @@ class Solution:
24042427
- 本题利用双指针,利用 i,j 双向遍历数组。
24052428
- l 记录当前索引左边所有数字之和,r 记录右边的和
24062429
- diff 记录当前索引左边所有数字之和 - 右边所有数字之和,中心索引左右和相等,diff[中心索引] 为 0
2430+
```python
2431+
class Solution:
2432+
def pivotIndex(self, nums: List[int]) -> int:
2433+
前缀和 = [0, *list(accumulate(nums, add))]
2434+
return next((i for i in range(len(nums)) if 前缀和[i] == 前缀和[-1] - 前缀和[i + 1]), -1)
2435+
```
2436+
- 前缀和,利用 next 的默认值返回 -1,2 行
24072437
## [733. Flood Fill 6行](https://leetcode.com/problems/flood-fill/)
24082438
```python
24092439
class Solution:
@@ -2522,6 +2552,12 @@ class Solution:
25222552
return next((i+j+k for i,j,k in zip(A,A[1:],A[2:]) if j+k>i ),0)
25232553
```
25242554
- 利用 next 函数返回第一个满足条件的值,不然就返回默认值的特点
2555+
## [989. 数组形式的整数加法 1行](https://leetcode.com/problems/add-to-array-form-of-integer/)
2556+
```python
2557+
class Solution:
2558+
def addToArrayForm(self, A: List[int], K: int) -> List[int]:
2559+
return map(int,str(int(''.join(map(str,A)))+K))
2560+
```
25252561
## [1290. Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/)
25262562
```python
25272563
# Definition for singly-linked list.

0 commit comments

Comments
 (0)