Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a7569c9

Browse files
Merge pull request #16 from wuyudi/master
Update README.md
2 parents 6c0ad78 + 4fff0ea commit a7569c9

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

‎README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ class Solution:
497497
from itertools import permutations
498498
return list(permutations(nums))
499499
```
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+
[:] 才会修改原列表
500511
## [49. Group Anagrams 1行](https://leetcode.com/problems/group-anagrams/)
501512
```python
502513
class Solution:
@@ -989,7 +1000,7 @@ class Solution:
9891000
class Solution:
9901001
def singleNumber(self, nums: List[int]) -> int:
9911002
from functools import reduce
992-
return reduce(int.__xor__, nums)
1003+
return reduce(xor, nums)
9931004
```
9941005
- 这里用到了异或(xor),相同的数字异或后为0,0异或任何数都等于那个数,用reduce在列表所有元素之间使用异或^,那么留下的就是那个单独的数字了
9951006
## [138. Copy List with Random Pointer 1行](https://leetcode.com/problems/copy-list-with-random-pointer/)
@@ -1379,7 +1390,7 @@ class Solution:
13791390
return int(bin(n)[2:].zfill(32)[::-1], 2)
13801391
```
13811392
- 字符串操作
1382-
- [ziff用法](https://www.runoob.com/python/att-string-zfill.html)
1393+
- [zfill用法](https://www.runoob.com/python/att-string-zfill.html)
13831394
## [191. Number of 1 Bits 1行](https://leetcode.com/problems/number-of-1-bits/)
13841395
```python
13851396
class Solution(object):
@@ -1510,6 +1521,13 @@ class Solution:
15101521
return nums[0]
15111522
return f(r, k - len(l) - len(m))
15121523
```
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 , 然后取最后一个
15131531
## [217. Contains Duplicate 1行](https://leetcode.com/problems/contains-duplicate/)
15141532
```python
15151533
class Solution:
@@ -1765,6 +1783,19 @@ class Solution:
17651783
return False
17661784
```
17671785
- 从矩阵右上角开始,若值比 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 可以直接排序的特点
17681799
## [258. Add Digits 1行](https://leetcode.com/problems/add-digits/)
17691800
```python
17701801
class Solution:
@@ -2479,6 +2510,14 @@ class Solution:
24792510
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
24802511
return sorted(points, key=lambda x: x[0]**2 + x[1]**2)[:K]
24812512
```
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 函数返回第一个满足条件的值,不然就返回默认值的特点
24822521
## [1290. Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/)
24832522
```python
24842523
# Definition for singly-linked list.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /