From dbca9c6e163c34a5b40209cbf3b7aeff9c0fec49 Mon Sep 17 00:00:00 2001 From: wuyudi Date: 2020年11月18日 23:44:48 +0800 Subject: [PATCH 1/4] Update README.md --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 97ccdaa..7fe09b7 100644 --- a/README.md +++ b/README.md @@ -989,7 +989,7 @@ class Solution: class Solution: def singleNumber(self, nums: List[int]) -> int: from functools import reduce - return reduce(int.__xor__, nums) + return reduce(xor, nums) ``` - 这里用到了异或(xor),相同的数字异或后为0,0异或任何数都等于那个数,用reduce在列表所有元素之间使用异或^,那么留下的就是那个单独的数字了 ## [138. Copy List with Random Pointer 1行](https://leetcode.com/problems/copy-list-with-random-pointer/) @@ -1379,7 +1379,7 @@ class Solution: return int(bin(n)[2:].zfill(32)[::-1], 2) ``` - 字符串操作 -- [ziff用法](https://www.runoob.com/python/att-string-zfill.html) +- [zfill用法](https://www.runoob.com/python/att-string-zfill.html) ## [191. Number of 1 Bits 1行](https://leetcode.com/problems/number-of-1-bits/) ```python class Solution(object): @@ -1510,6 +1510,13 @@ class Solution: return nums[0] return f(r, k - len(l) - len(m)) ``` +```python +class Solution: + def findKthLargest(self, nums: List[int], k: int) -> int: + return nlargest(k,nums)[-1] +``` + +- 用了 heapq 的 nlargest 函数,返回一个 list , 然后取最后一个 ## [217. Contains Duplicate 1行](https://leetcode.com/problems/contains-duplicate/) ```python class Solution: From 6ed4f8e27efde58aca4db57b56adc3d9195883d0 Mon Sep 17 00:00:00 2001 From: wuyudi Date: 2020年11月22日 00:36:06 +0800 Subject: [PATCH 2/4] Update README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 7fe09b7..f89f17a 100644 --- a/README.md +++ b/README.md @@ -1772,6 +1772,19 @@ class Solution: return False ``` - 从矩阵右上角开始,若值比 target 大则证明这一列的值都比 target 大,继续搜索前面的列;若比 target 小说明 target 可能在后面的行中,进入下一行 +## [242. 有效的字母异位词 1行](https://leetcode.com/problems/valid-anagram/) +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return Counter(s) == Counter(t) +``` +- O(n) 思路等于建哈希表 +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return sorted(s) == sorted(t) +``` +- O(n log(n)) 排序后相等,原来就相等,利用 python 的 str 可以直接排序的特点 ## [258. Add Digits 1行](https://leetcode.com/problems/add-digits/) ```python class Solution: From 27ec881850b3e80b77462b7e43cc4145cf80560f Mon Sep 17 00:00:00 2001 From: wuyudi Date: 2020年11月29日 01:12:32 +0800 Subject: [PATCH 3/4] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index f89f17a..3dcc3ec 100644 --- a/README.md +++ b/README.md @@ -2499,6 +2499,14 @@ class Solution: def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]: return sorted(points, key=lambda x: x[0]**2 + x[1]**2)[:K] ``` +## [976. 三角形的最大周长 2行](https://leetcode.com/problems/largest-perimeter-triangle/) +```python +class Solution: + def largestPerimeter(self, A: List[int]) -> int: + A.sort(reverse=True) + return next((i+j+k for i,j,k in zip(A,A[1:],A[2:]) if j+k>i ),0) +``` +- 利用 next 函数返回第一个满足条件的值,不然就返回默认值的特点 ## [1290. Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) ```python # Definition for singly-linked list. From 4fff0ea2433fbbf542b9f3bed4076eb79b645f0c Mon Sep 17 00:00:00 2001 From: wuyudi Date: 2020年12月19日 00:43:44 +0800 Subject: [PATCH 4/4] add 48 --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 3dcc3ec..e53bcac 100644 --- a/README.md +++ b/README.md @@ -497,6 +497,17 @@ class Solution: from itertools import permutations return list(permutations(nums)) ``` +## [48. rotate-image 1行](https://leetcode.com/problems/rotate-image/) +先转置后镜像对称 +```python +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + matrix[:] = [i[::-1] for i in zip(*matrix)] +``` +加 [:] 才会修改原列表 ## [49. Group Anagrams 1行](https://leetcode.com/problems/group-anagrams/) ```python class Solution:

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