classSolution: defhIndex(self, citations: List[int]) -> int: citations.sort(reverse = True) size = len(citations) res = -1 for i inrange(0,size): if i+1 >=citations[i]: res = max(res, citations[i]) else: res = max(res, i+1) return res if res!=-1else0
1 2 3 4 5 6 7 8
classSolution: defhIndex(self, citations: List[int]) -> int: citations = sorted(citations, reverse = True) size = len(citations) i, h = 0,0 while i < n and citations[i] > h: i, h = i+1, h+1 return h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defhIndex(self, citations: List[int]) -> int: n = len(citations) cnt = [0] * (n+1) for i inrange(0, n): if citations[i] > n: cnt[n] = cnt[n] + 1 else: cnt[citations[i]] += 1 tot = 0 for i inrange(n, -1, -1): tot += counter[i] if tot >= i: return i return0
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defhIndex(self, citations: List[int]) -> int: left, right = 0, len(citations) while left < right: mid = (left + right +1) >>1 cnt = 0 for v in citations: if v >= mid: cnt+=1 if cnt >= mid: left = mid else: right = mid-1 return left
classSolution: deflongestMountain(self, arr: List[int]) -> int: s = [] n = len(arr) inc = [0] * n dec = [0] * n inc[0] = 1 for i inrange(1, n): if arr[i] > arr[i-1]: inc[i] = inc[i-1]+1 else: inc[i] = 1 dec[n-1] = 1 for i inrange(n-2, -1, -1): if arr[i] > arr[i+1]: dec[i] = dec[i+1] +1 else: dec[i] = 1 res = 0 for i inrange(0,n): res = max(res, inc[i]+dec[i]-1if inc[i]!=1and dec[i]!=1else0) return res if res!=1else0
```python classSolution: defrearrangeBarcodes(self, barcodes: List[int]) -> List[int]: n = len(barcodes) if n == 0or n == 1: return barcodes barcodes.sort() res = [0] * n if n%2==0: mid = n // 2 for i inrange(0,n,2): res[i], res[i+1] = barcodes[i//2], barcodes[i//2+mid] else: mid = n // 2 +1 for i inrange(0,n-1,2): res[i], res[i+1] = barcodes[i//2], barcodes[i//2+mid] res[n-1] = barcodes[mid-1] return res
classSolution: defintersection(self, nums1: List[int], nums2: List[int]) -> List[int]: s = set() res = [] for num in nums1: s.add(num) for num in nums2: if num in s and num notin res: res.append(num) return res
classSolution: defsingleNumber(self, nums: List[int]) -> int: maxn = max(nums) size = math.log(nums,2)+2 d = [0]* size n = len(nums) for num in nums: i = 0 while num: if num%2 == 1: d[i] += 1 i += 1 num = num//2 res = 0 p = 1 for i inrange(0,size): d[i] = d[i]%3 res += d[i] * p p = p*2 return res
1 2 3 4 5 6 7 8 9 10
classSolution: defsingleNumber(self, nums: List[int]) -> int: ans = 0 for i inrange(31): # cnt 表示的是1的数量? cnt1 = sum(x >> i & 1for x in nums ) ans |= cnt1%3 << i # 这个是对符号位进行处理 cnt1 = sum (x >> 31 & 1for x in nums) return ans - (cnt1 % 3 << 31)
1 2 3
总结
1 2
for i inrange(31): cnt1 = sum(x >> i & 1for x in nums )
这里的含义是对应第i位的1的个数,同时这里有一个比较细节的点
特点
sum( .. for .. in ..)
sum( [.. for .. in ..] )
内存使用
按需生成元素,节省内存
先生成完整列表,内存开销较大
执行效率
较高,尤其是数据量大时更优
稍低,因为先生成整个列表
我们基本选择第一种,第二种一般是先生成还需其他操作的时候用
202. 快乐数
思路 集合判断,出现过了就是进入死循环了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defisHappy(self, n: int) -> bool: s = set() s.add(n) while n!=1: next_n = 0 while n: k = n%10 next_n += k*k n = n/10 if next_n in s: returnFalse n = next_n returnTrue