@@ -1473,6 +1473,13 @@ class Solution:
1473
1473
def isIsomorphic (self , s : str , t : str ) -> bool :
1474
1474
return [* map (s.index, s)] == [* map (t.index, t)]
1475
1475
```
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
+
1476
1483
- 同构代表两个字符串中每个位置上字符在自身第一次出现的索引相同
1477
1484
## [ 206. Reverse Linked List 2行] ( https://leetcode.com/problems/reverse-linked-list/ )
1478
1485
``` python
@@ -2297,6 +2304,22 @@ class Solution:
2297
2304
return [x for x in d if d[x] == min (d.values())]
2298
2305
```
2299
2306
- 使用字典记录{共同喜欢的商店:索引和},返回索引和并列最小的商店名
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
+ - 前缀和
2300
2323
## [ 652. Find Duplicate Subtrees 8行] ( https://leetcode.com/problems/find-duplicate-subtrees/ )
2301
2324
``` python
2302
2325
# Definition for a binary tree node.
@@ -2404,6 +2427,13 @@ class Solution:
2404
2427
- 本题利用双指针,利用 i,j 双向遍历数组。
2405
2428
- l 记录当前索引左边所有数字之和,r 记录右边的和
2406
2429
- 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 行
2407
2437
## [ 733. Flood Fill 6行] ( https://leetcode.com/problems/flood-fill/ )
2408
2438
``` python
2409
2439
class Solution :
@@ -2522,6 +2552,12 @@ class Solution:
2522
2552
return next ((i+ j+ k for i,j,k in zip (A,A[1 :],A[2 :]) if j+ k> i ),0 )
2523
2553
```
2524
2554
- 利用 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
+ ```
2525
2561
## [ 1290. Convert Binary Number in a Linked List to Integer] ( https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ )
2526
2562
``` python
2527
2563
# Definition for singly-linked list.
0 commit comments