@@ -497,6 +497,17 @@ class Solution:
497
497
from itertools import permutations
498
498
return list(permutations(nums))
499
499
```
500
+ ## [ 48. rotate-image 1行] ( https://leetcode.com/problems/rotate-image/ )
501
+ 先转置后镜像对称
502
+ ``` python
503
+ class Solution :
504
+ def rotate (self , matrix : List[List[int ]]) -> None :
505
+ """
506
+ Do not return anything, modify matrix in-place instead.
507
+ """
508
+ matrix[:] = [i[::- 1 ] for i in zip (* matrix)]
509
+ ```
510
+ 加 [ :] 才会修改原列表
500
511
## [ 49. Group Anagrams 1行] ( https://leetcode.com/problems/group-anagrams/ )
501
512
``` python
502
513
class Solution :
@@ -989,7 +1000,7 @@ class Solution:
989
1000
class Solution :
990
1001
def singleNumber (self , nums : List[int ]) -> int :
991
1002
from functools import reduce
992
- return reduce (int . __xor__ , nums)
1003
+ return reduce (xor , nums)
993
1004
```
994
1005
- 这里用到了异或(xor),相同的数字异或后为0,0异或任何数都等于那个数,用reduce在列表所有元素之间使用异或^,那么留下的就是那个单独的数字了
995
1006
## [ 138. Copy List with Random Pointer 1行] ( https://leetcode.com/problems/copy-list-with-random-pointer/ )
@@ -1379,7 +1390,7 @@ class Solution:
1379
1390
return int (bin (n)[2 :].zfill(32 )[::- 1 ], 2 )
1380
1391
```
1381
1392
- 字符串操作
1382
- - [ ziff用法 ] ( https://www.runoob.com/python/att-string-zfill.html )
1393
+ - [ zfill用法 ] ( https://www.runoob.com/python/att-string-zfill.html )
1383
1394
## [ 191. Number of 1 Bits 1行] ( https://leetcode.com/problems/number-of-1-bits/ )
1384
1395
``` python
1385
1396
class Solution (object ):
@@ -1510,6 +1521,13 @@ class Solution:
1510
1521
return nums[0]
1511
1522
return f(r, k - len(l) - len(m))
1512
1523
```
1524
+ ``` python
1525
+ class Solution :
1526
+ def findKthLargest (self , nums : List[int ], k : int ) -> int :
1527
+ return nlargest(k,nums)[- 1 ]
1528
+ ```
1529
+
1530
+ - 用了 heapq 的 nlargest 函数,返回一个 list , 然后取最后一个
1513
1531
## [ 217. Contains Duplicate 1行] ( https://leetcode.com/problems/contains-duplicate/ )
1514
1532
``` python
1515
1533
class Solution :
@@ -1765,6 +1783,19 @@ class Solution:
1765
1783
return False
1766
1784
```
1767
1785
- 从矩阵右上角开始,若值比 target 大则证明这一列的值都比 target 大,继续搜索前面的列;若比 target 小说明 target 可能在后面的行中,进入下一行
1786
+ ## [ 242. 有效的字母异位词 1行] ( https://leetcode.com/problems/valid-anagram/ )
1787
+ ``` python
1788
+ class Solution :
1789
+ def isAnagram (self , s : str , t : str ) -> bool :
1790
+ return Counter(s) == Counter(t)
1791
+ ```
1792
+ - O(n) 思路等于建哈希表
1793
+ ``` python
1794
+ class Solution :
1795
+ def isAnagram (self , s : str , t : str ) -> bool :
1796
+ return sorted (s) == sorted (t)
1797
+ ```
1798
+ - O(n log(n)) 排序后相等,原来就相等,利用 python 的 str 可以直接排序的特点
1768
1799
## [ 258. Add Digits 1行] ( https://leetcode.com/problems/add-digits/ )
1769
1800
``` python
1770
1801
class Solution :
@@ -2479,6 +2510,14 @@ class Solution:
2479
2510
def kClosest (self , points : List[List[int ]], K : int ) -> List[List[int ]]:
2480
2511
return sorted (points, key = lambda x : x[0 ]** 2 + x[1 ]** 2 )[:K]
2481
2512
```
2513
+ ## [ 976. 三角形的最大周长 2行] ( https://leetcode.com/problems/largest-perimeter-triangle/ )
2514
+ ``` python
2515
+ class Solution :
2516
+ def largestPerimeter (self , A : List[int ]) -> int :
2517
+ A.sort(reverse = True )
2518
+ return next ((i+ j+ k for i,j,k in zip (A,A[1 :],A[2 :]) if j+ k> i ),0 )
2519
+ ```
2520
+ - 利用 next 函数返回第一个满足条件的值,不然就返回默认值的特点
2482
2521
## [ 1290. Convert Binary Number in a Linked List to Integer] ( https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ )
2483
2522
``` python
2484
2523
# Definition for singly-linked list.
0 commit comments