-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path剑指offer.py
1725 lines (1597 loc) · 45.4 KB
/
剑指offer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: 剑指offer
Description :
Author : amilyxy
date: 2020/1/23
-------------------------------------------------
"""
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
'''
1.数组中重复的数字
'''
# 方法一
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
for i in range(len(nums)):
while i!=nums[i]:
if nums[nums[i]] == nums[i]:
return nums[i]
nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
# 方法二:
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
setnum = set()
for i in nums:
if i not in setnum:
setnum.add(i)
else:
return i
'''
2.二维数组中的查找
# 原始方案:暴力查找 时间复杂度O(n^2)
'''
class Solution:
def Find(self, target, array):
m = len(array)
n = len(array[0])
for i in range(m):
for j in range(n):
if array[i][j] == target:
return True
return False
# 优化方案一 : 左上寻找 逐步减少查询范围 时间复杂度最多能O(n^2),好像并没有很优化多少
class Solution:
def Find(self, target, array):
m = len(array)
n = len(array[0])
temp = 0
while temp<m and temp<n:
for i in range(temp, m):
if array[i][temp] == target:
return True
if array[i][temp] > target:
m = i
break
for j in range(temp, n):
if array[temp][j] == target:
return True
if array[temp][j] > target:
n = j
break
temp+=1
return False
# 优化方案二 右上查询 时间复杂度O(m+n)
class Solution:
def Find(self, target, array):
if not array: return False
n = len(array[0])
row, col = 0, n-1
while row<len(array) and col>-1:
if array[row][col] == target:
return True
if array[row][col]<target:
row+=1
else:
col-=1
return False
'''
3. 替换空格
'''
class Solution:
def replaceSpace(self, s):
s = list(s)
for i in range(len(s)-1, -1, -1):
if s[i] == ' ':
s[i] = '%20'
return ''.join(s)
'''
4. 从尾到头打印链表
'''
# 方案一 顺序遍历逆序输出
class Solution:
def printListFromTailToHead(self, listNode):
res = []
while listNode:
res.append(listNode.val)
listNode = listNode.next
return res[::-1]
# 方案二 回溯
class Solution:
def printListFromTailToHead(self, listNode):
# write code
res = []
def back(node):
if node.next:
back(node.next)
res.append(node.val)
if listNode:
back(listNode)
return res
'''
5. 重建二叉树
'''
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, preorder, inorder):
if not preorder:
return None
loc = inorder.index(preorder[0])
root = TreeNode(preorder[0])
root.left = self.reConstructBinaryTree(preorder[1: loc+1], inorder[0:loc])
root.right = self.reConstructBinaryTree(preorder[loc+1:], inorder[loc+1:])
return root
'''
6.二叉树的下一个节点
'''
class Solution:
def GetNext(self, pNode):
# write code here
# 如果该节点有右子树
if pNode.right:
pNode = pNode.right
while pNode.left:
pNode = pNode.left
return pNode
else:
if pNode.next:
if pNode == pNode.next.left:
return pNode.next
else:
while pNode.next.left != pNode:
pNode = pNode.next
if not pNode.next:
return None
return pNode.next
else:
return None
'''
7.用两个栈实现队列
'''
from collections import deque
# 栈先进后出 队列先进先出
class Solution:
def __init__(self):
self.stack1 = deque()
self.stack2 = deque()
def push(self, node):
# write code here
self.stack1.append(node)
def pop(self):
# return xx
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop())
if self.stack2:
return self.stack2.pop()
return -1
'''
8.菲波那切数列
牛客网上跳台阶、变态跳台阶、覆盖格子都是斐波那契的应用
'''
class Solution:
def Fibonacci(self, n):
# write code here
if n == 0: return 0
if n == 1: return 1
n1, n2 = 0, 1
for i in range(2, n + 1):
n1, n2 = n2, n1 + n2
return n2
'''
9.旋转排序数组中的最小值
'''
class Solution:
def minNumberInRotateArray(self, rotateArray):
l, r = 0, len(rotateArray)-1
while l<r:
mid = l+(r-l)//2
if rotateArray[r]<rotateArray[mid]:
l=mid+1
else:
r=mid
return rotateArray[l]
# 带重复数字
class Solution:
def minArray(self, numbers):
l, r = 0, len(numbers)-1
while l<r:
if numbers[l] == numbers[r]:
l += 1
else:
mid = l+(r-l)//2
if numbers[mid]<=numbers[r]:
r = mid
else:
l = mid+1
return numbers[l]
'''
10.矩阵中的路径
力扣中相似题目有8、90、77、39
'''
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
if not word: return True
if not board: return False
m, n = len(board), len(board[0])
dire = [[-1, 0], [0, -1], [1, 0], [0, 1]]
def helper(i, j, word, marked):
if not word: return True
marked[i][j] = 1
for k in dire:
new_i, new_j = i + k[0], j + k[1]
if 0 <= new_i < m and 0 <= new_j < n and board[new_i][new_j] == word[0] and not marked[new_i][new_j]:
if helper(new_i, new_j, word[1:], marked): return True
marked[i][j] = 0
marked = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
if board[i][j] == word[0]:
if helper(i, j, word[1:], marked):
return True
return False
'''
11. 机器人的运动范围
'''
class Solution:
def movingCount(self, m: int, n: int, k: int) -> int:
dire = [[-1, 0], [0, 1], [1, 0], [0, 1]]
marked = [[0 for _ in range(n)] for _ in range(m)]
def bitsum(a):
sum = 0
while a > 0:
sum += a % 10
a = a // 10
return sum
def helper(row, col):
marked[row][col] = 1
for i in dire:
newi, newj = row + i[0], col + i[1]
if 0 <= newi < m and 0 <= newj < n and marked[newi][newj] == 0:
if bitsum(newi) + bitsum(newj) <= k:
helper(newi, newj)
if k >= 0: helper(0, 0)
return sum([sum(i) for i in marked])
'''
12.剪绳子
当n=10000时,测量一下题解一报错超过循环次数,题解二105s
'''
'''
解题一:递归方法(会存在许多重复步骤)31ms
'''
class Solution:
def cutRope(self, number):
# write code here
def helper(num):
res = []
if num == 1:
return 1
for i in range(1, (num+1)//2+1):
res.append(max(helper(i), i)*max(helper(num-i), num-i))
return max(res)
return helper(number)
'''
解题二:动态规划方法(避免重复) 28ms
'''
class Solution:
def cutRope(self, number):
# write code here
res = [0, 1]
# write code here
for i in range(2, number+1):
tmp = []
for j in range(1, (i+1)//2+1):
tmp.append(max(res[j],j) * max(res[i - j], (i-j)))
res.append(max(tmp))
return res[-1]
'''
13.二进制中1的个数
'''
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
if n < 0:
n = n & 0xffffffff
while n:
count += 1
# 补码中最右边有几个1,就循环几次
n = (n - 1) & n
return count
'''
14.数值的整数次方
'''
class Solution:
def Power(self, base, exponent):
# write code here
if base == 0 and exponent<0:
return "the input is invalid"
else:
return pow(base, exponent) # base**exponent
# 不使用库函数
class Solution:
def myPow(self, x: float, n: int) -> float:
if x == 0: return 0
res = 1
if n < 0: x, n = 1 / x, -n
while n:
if n & 1: res *= x
x *= x
n >>= 1
return res
'''
15.打印从1到最大的n位数
'''
# 一般做法
class Solution:
def printNumbers(self, n: int) -> List[int]:
res = []
for i in range(1, pow(10, n)):
res.append(i)
return res
# 大数问题 用排列方法解决
class Solution:
def printNumbers(self, n: int) -> List[int]:
base = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
res = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
tmp = res
for i in range(1, n):
tmp1 = []
for k in tmp:
for j in base:
tmp1.append(k+j)
res.extend(tmp1)
tmp = tmp1
return [int(i) for i in res]
'''
16.删除链表的节点
'''
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val == val: return head.next
pre, cur = head, head.next
while cur and cur.val != val:
pre, cur = cur, cur.next
if cur:
pre.next = cur.next
return head
'''
17.调整数组顺序使得奇数位于偶数前面
① 容易想到的方法
② 原地算法
'''
# 方法一
class Solution:
def reOrderArray(self, array):
# write code here
a, b = [],[]
for i in array:
a.append(i) if i&0x01 else b.append(i)
return a+b
# 方法二 算是原地算法吧???
class Solution:
def reOrderArray(self, array):
# write code here
n = len(array)
pre, cur = 0, 0
while cur<n:
if array[cur]&0x01:
tmp = array[cur]
for i in range(cur, pre-1, -1):
array[i] = array[i-1]
array[pre] = tmp
pre+=1
cur+=1
return array
# 方法三 推荐 双指针方法
class Solution:
def exchange(self, nums: List[int]) -> List[int]:
i, j = 0, len(nums) - 1
while i < j:
while i < j and nums[i] & 0x01:
i += 1
while i < j and not nums[j] & 0x01:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
return nums
'''
18.链表中倒数第k个结点
!!注意不是输出第k个结点的值而是第k个结点!!
'''
# 可以用快慢指针去做
class Solution:
def FindKthToTail(self, head, k):
# write code here
pre, cur = head, head
while k:
cur = cur.next
k-=1
while cur:
pre, cur = pre.next, cur.next
return pre
'''
19.反转链表
'''
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if not head: return head
pre, cur = None, head
while cur:
tmp = cur.next
cur.next = pre
pre, cur = cur, tmp
return pre
'''
20.合并两个排序的链表
'''
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 and l2:
if l1.val>l2.val:
l1, l2 = l2, l1
l1.next = self.mergeTwoLists(l1.next, l2)
return l1 or l2
'''
21.链表中环的入口节点
'''
class Solution:
def EntryNodeOfLoop(self, pHead):
pre, cur = pHead, pHead
flag = False
while cur and pre:
pre = pre.next
if cur.next:
cur = cur.next.next
if pre == cur:
flag = True
pre = pre.next
n = 1
while pre != cur:
pre = pre.next
n += 1
break
else:
cur = cur.next
if not flag:
return
pre, cur = pHead, pHead
while n:
cur = cur.next
n -= 1
while pre != cur:
pre = pre.next
cur = cur.next
return pre
'''
22.树的子结构
'''
class Solution:
def HasSubtree(self, pRoot1, pRoot2):
# write code here
if not pRoot1 or not pRoot2:
return False
else:
# 匹配到与pRoot2相同的根节点
if pRoot1.val == pRoot2.val:
# 每次重新遍历是不是有点耗时
if self.equal(pRoot1, pRoot2): return True
if self.HasSubtree(pRoot1.left, pRoot2): return True
if self.HasSubtree(pRoot1.right, pRoot2): return True
# 根据pRoot2要么是左树的子树,要么是右树的子树,前面代码也可以这么写:
'''
if not pRoot1 or not pRoot2:
return False
else:
# 匹配到与pRoot2相同的根节点
return self.equal(pRoot1, pRoot2)|self.equal(pRoot1.left, pRoot2)|self.equal(pRoot1.right, pRoot2)
'''
def equal(self, node1, node2):
if not node2:
return True
if not node1:
return False
if node1.val != node2.val:
return False
return self.equal(node1.left, node2.left) and self.equal(node1.right, node2.right)
'''
23.二叉树的镜像
代码为递归做法,如果要求不能用递归,可以用广度遍历去做(题解的思路
'''
# 递归实现
class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
# write code here
if not root:
return
root.left, root.right = root.right, root.left
self.Mirror(root.left)
self.Mirror(root.right)
return root
# 循环实现
class Solution:
# 返回镜像树的根节点
def Mirror(self, pRoot):
if not pRoot:
return True
else:
tmp = [pRoot]
tmp1 = []
while tmp:
node = tmp.pop()
node.left, node.right = node.right, node.left
if node.left:
tmp1.append(node.left)
if node.right:
tmp1.append(node.right)
if not tmp:
tmp = tmp1
tmp1 = []
return pRoot
'''
24.对称的二叉树
'''
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
def helper(root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
if root1.val != root2.val:
return False
return helper(root1.left, root2.right) and helper(root1.right, root2.left)
return helper(root.left, root.right) if root else True
'''
25.顺时针打印矩阵
'''
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
dire = [[0, 1], [1, 0], [0, -1], [-1, 0]]
if not matrix: return res
m = len(matrix)
n = len(matrix[0])
marked = [[0]*n for _ in range(m)]
i, j, k = 0, 0, 0
while 0<=i<m and 0<=j<n and not marked[i][j]:
res.append(matrix[i][j])
marked[i][j] = 1
while 0<=i+dire[k][0]<m and 0<=j+dire[k][1]<n and not marked[i+dire[k][0]][j+dire[k][1]]:
i, j = i+dire[k][0], j+dire[k][1]
res.append(matrix[i][j])
matrix[i][j] = 1
k = (k+1)%4
i, j = i+dire[k][0], j+dire[k][1]
return res
'''
26.包含min函数的栈
'''
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.helper = []
self.minnum = None
def push(self, x: int) -> None:
self.stack.append(x)
if self.minnum == None or x<self.minnum:
self.minnum = x
self.helper.append(self.minnum)
def pop(self) -> None:
if self.stack:
self.stack.pop()
self.helper.pop()
else:
return
if self.stack:
self.minnum = self.helper[-1]
else:
self.minnum = None
def top(self) -> int:
return self.stack[-1]
def min(self) -> int:
return self.minnum
'''
27.栈的压入、弹出序列
'''
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
res = []
idx = 0
for i in pushed:
while res and res[-1] == popped[idx]:
idx+=1
res.pop()
res.append(i)
# print(res, popped)
if res == popped[idx:][::-1]:
return True
else:
return False
'''
28.从上到下打印二叉树
'''
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
if not root: return []
res, tmp = [], []
stack = [root]
while stack:
node = stack.pop(0)
res.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return res
'''
29.二叉搜索树的后序遍历
'''
class Solution:
def verifyPostorder(self, postorder: List[int]) -> bool:
if not postorder:
return True
else:
tmp = []
for i in range(len(postorder)-1):
if postorder[i] < postorder[-1]:
tmp.append(postorder[i])
else:
break
for j in range(len(tmp), len(postorder)-1):
if postorder[j]<postorder[-1]:
return False
return self.verifyPostorder(tmp) and self.verifyPostorder(postorder[len(tmp): len(postorder)-1])
'''
30.二叉树中和为某一值的路径
'''
class Solution:
def pathSum(self, root: TreeNode, sumr: int) -> List[List[int]]:
res = []
def dfs(node, tmp):
if sum(tmp)+node.val == sumr and not node.left and not node.right:
res.append(tmp+[node.val])
else:
if node.left:
dfs(node.left, tmp+[node.val])
if node.right:
dfs(node.right, tmp+[node.val])
if root:
dfs(root, [])
return res
'''
31.复杂链表的复制
'''
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
lookup = {}
def helper(node):
if not node:
return None
if node in lookup: return lookup[node]
copynode = Node(node.val)
lookup[node] = copynode
copynode.next, copynode.random = helper(node.next), helper(node.random)
return copynode
return helper(head)
#方法二
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return None
node = head
while node:
node.next, node.next.next = Node(node.val), node.next
node = node.next.next
node = head
while node:
if node.random:
node.next.random = node.random.next
node = node.next.next
# 拆分
copy_head = head.next
copy_pre = head
copy_post = head.next
while copy_pre:
# pre
copy_pre.next = copy_pre.next.next
copy_pre = copy_pre.next
# post
if copy_pre:
copy_post.next = copy_pre.next
copy_post = copy_post.next
return copy_head
'''
32.二叉树与双向链表
'''
# 方法一 按照中序遍历保存节点然后连线
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def treeToDoublyList(self, root: 'Node') -> 'Node':
if not root: return None
array = []
self.inorder(root, array)
for i in range(len(array)):
array[i].left = array[i-1]
array[i].right = array[(i+1)%(len(array))]
return array[0]
def inorder(self, node, array):
if not node:
return
self.inorder(node.left, array)
array.append(node)
self.inorder(node.right, array)
# 递归方法
class Solution:
def __init__(self):
self.pre = None
self.head = None
# self.tail = None
def treeToDoublyList(self, pRootOfTree: 'Node') -> 'Node':
if not pRootOfTree:
return None
self.inorder(pRootOfTree)
self.head.left = self.pre
self.pre.right = self.head
return self.head
def inorder(self, node):
if not node:
return
self.inorder(node.left)
if not self.pre:
self.head = node
else:
self.pre.right = node
node.left = self.pre
self.pre = node
# self.tail = node
self.inorder(node.right)
'''
33.字符串的排列
'''
class Solution:
def permutation(self, s: str) -> List[str]:
res = []
s = sorted(s)
def helper(s, tmp):
if not s:
res.append(''.join(tmp))
else:
for i in range(len(s)):
if i>0 and s[i-1] == s[i]:
continue
helper(s[:i]+s[i+1:], tmp+[s[i]])
helper(s,[])
return res
# 来个内置函数的用法
import itertools
class Solution:
def permutation(self, s: str) -> List[str]:
res = itertools.permutations(s, len(s))
return set([''.join(x) for x in res])
# 不用递归 交换元素
class Solution:
def Permutation(self, s):
# write code here
if not s: return []
s = list(s)
res = [s]
start = 0
n = len(s)
while start < n:
tmp = []
for i in res:
for j in range(start+1, n):
if i[start] != i[j]:
tmp1 = i[:]
tmp1[start], tmp1[j] = tmp1[j], tmp1[start]
tmp.append(tmp1)
res += tmp
start += 1
return sorted(list(set([''.join(x) for x in res])))
'''
34.数组中出现次数超过一半的数字
主要方法:哈希、摩尔投票、排序
'''
# 摩尔投票
class Solution:
def majorityElement(self, nums: List[int]) -> int:
cand = None
count = 0
for i in nums:
if count == 0:
cand = i
cand += (1 if cand == i else -1)
return cand
# 拓展 数组中超过三分之一的元素 leecode229
'''
35.最小的k个数
'''
# 方法一:快排
class Solution:
def getLeastNumbers(self, arr: List[int], k: int) -> List[int]:
left, right = 0, len(arr)-1
def helper(arr, left, right):
if left<right:
mid = self.quicksort(arr, left, right)
if mid>k:
helper(arr, left, mid-1)
if mid<k:
helper(arr, mid+1, right)
helper(arr, left, right)
return arr[:k]
def quicksort(self, arr, left, right):
base = left
i, idx = left+1, left+1
while i<=right:
if arr[i]<arr[base]:
arr[idx], arr[i] = arr[i], arr[idx]
idx+=1
i+=1
arr[base], arr[idx-1] = arr[idx-1], arr[base]
return idx-1
# 堆排
class Solution:
def getLeastNumbers(self, nums: List[int], k: int) -> List[int]:
res = []
n = len(nums)
if n == 1: return nums
for i in range(n//2-1, -1, -1): #构建小顶堆
self.heapify(nums, i, n-1)
for i in range(n-1, n-k-1, -1):
res.append(nums[0])
nums[i], nums[0] = nums[0], nums[i] #将小顶堆堆顶放到最后
self.heapify(nums, 0, i-1) #调整剩下的数为小顶堆
return res
def heapify(self, arr, par, end):
temp = arr[par] # 父节点值
son = 2*par+1 #左子结点
while son<=end:
if son<end and arr[son]>arr[son+1]: #选择左子结点和右子结点较小的一个
son+=1
if temp<=arr[son]:
break
arr[par]=arr[son] #若大于父节点 上浮
par=son
son=2*son+1
arr[par]=temp
'''
36.连续子数组的最大和
修改了一下可以输出区间,left,right为最大和的左右区间
'''
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxsum = nums[0]
cursum = nums[0]
# left, right, i_tmp = 0, 0, 0
for i in range(1, len(nums)):
if cursum <= 0:
cursum = nums[i]
# i_tmp = i
else:
cursum += nums[i]
if cursum > maxsum:
maxsum = cursum
# left = i_tmp
# right = i
# 获取区间
# print(left, right)
return maxsum
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# 动态规划,原地修改数组
maxnum = nums[0]
for i in range(1,len(nums)):
if nums[i - 1] > 0:
nums[i] += nums[i - 1]
maxnum = max(maxnum,nums[i])
return maxnum
# 简洁版
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
cur_sum = max_sum = nums[0]
for i in range(1, len(nums)):
cur_sum = max(nums[i], cur_sum+nums[i])
max_sum = max(max_sum, cur_sum)
return max_sum
'''
37.数字1的个数
讲道理我觉得这是数学题目,我也就看懂了一种方法,
'''
# 按照编程之美上的解释写出来的
class Solution:
def countDigitOne(self, n: int) -> int:
if n<1:
return 0
res = 0
strn = str(n)