Skip to content

Commit cd29927

Browse files
committed
修正代码 tab 占位
1 parent ea29f5a commit cd29927

10 files changed

Lines changed: 451 additions & 451 deletions

File tree

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
class Solution:
2-
# 调整为小顶堆
3-
def heapify(self, nums: [int], index: int, end: int):
4-
left = index * 2 + 1
5-
right = left + 1
6-
while left <= end:
7-
# 当前节点为非叶子结点
8-
min_index = index
9-
if nums[left] < nums[min_index]:
10-
min_index = left
11-
if right <= end and nums[right] < nums[min_index]:
12-
min_index = right
13-
if index == min_index:
14-
# 如果不用交换,则说明已经交换结束
15-
break
16-
nums[index], nums[min_index] = nums[min_index], nums[index]
17-
# 继续调整子树
18-
index = min_index
19-
left = index * 2 + 1
20-
right = left + 1
21-
22-
23-
# 初始化小顶堆
24-
def buildMinHeap(self, nums: [int]):
25-
size = len(nums)
26-
# (size - 2) // 2 是最后一个非叶节点,叶节点不用调整
27-
for i in range((size - 2) // 2, -1, -1):
28-
self.heapify(nums, i, size - 1)
29-
return nums
30-
31-
32-
# 升序堆排序,思路如下:
33-
# 1. 先建立小顶堆
34-
# 2. 让堆顶最小元素与最后一个交换,然后调整第一个元素到倒数第二个元素,这一步获取最小值
35-
# 3. 再交换堆顶元素与倒数第二个元素,然后调整第一个元素到倒数第三个元素,这一步获取第二小值
36-
# 4. 以此类推,直到最后一个元素交换之后完毕。
37-
def minHeapSort(self, nums: [int]):
38-
self.buildMinHeap(nums)
39-
size = len(nums)
40-
for i in range(size):
41-
nums[0], nums[size - i - 1] = nums[size - i - 1], nums[0]
42-
self.heapify(nums, 0, size - i - 2)
43-
return nums
44-
45-
# 升序堆排序,思路如下:
46-
# 1. 先建立大顶堆
47-
# 2. 让堆顶最大元素与最后一个交换,然后调整第一个元素到倒数第二个元素,这一步获取最大值
48-
# 3. 再交换堆顶元素与倒数第二个元素,然后调整第一个元素到倒数第三个元素,这一步获取第二大值
49-
# 4. 以此类推,直到最后一个元素交换之后完毕。
50-
def maxHeapSort(self, arr: [int]):
51-
self.buildMaxHeap(arr)
52-
size = len(arr)
53-
for i in range(size):
54-
arr[0], arr[size - i - 1] = arr[size - i - 1], arr[0]
55-
self.heapify(arr, 0, size - i - 2)
56-
return arr
57-
58-
def sortArray(self, nums: List[int]) -> List[int]:
59-
return self.maxHeapSort(nums)
2+
# 调整为小顶堆
3+
def heapify(self, nums: [int], index: int, end: int):
4+
left = index * 2 + 1
5+
right = left + 1
6+
while left <= end:
7+
# 当前节点为非叶子结点
8+
min_index = index
9+
if nums[left] < nums[min_index]:
10+
min_index = left
11+
if right <= end and nums[right] < nums[min_index]:
12+
min_index = right
13+
if index == min_index:
14+
# 如果不用交换,则说明已经交换结束
15+
break
16+
nums[index], nums[min_index] = nums[min_index], nums[index]
17+
# 继续调整子树
18+
index = min_index
19+
left = index * 2 + 1
20+
right = left + 1
21+
22+
23+
# 初始化小顶堆
24+
def buildMinHeap(self, nums: [int]):
25+
size = len(nums)
26+
# (size - 2) // 2 是最后一个非叶节点,叶节点不用调整
27+
for i in range((size - 2) // 2, -1, -1):
28+
self.heapify(nums, i, size - 1)
29+
return nums
30+
31+
32+
# 升序堆排序,思路如下:
33+
# 1. 先建立小顶堆
34+
# 2. 让堆顶最小元素与最后一个交换,然后调整第一个元素到倒数第二个元素,这一步获取最小值
35+
# 3. 再交换堆顶元素与倒数第二个元素,然后调整第一个元素到倒数第三个元素,这一步获取第二小值
36+
# 4. 以此类推,直到最后一个元素交换之后完毕。
37+
def minHeapSort(self, nums: [int]):
38+
self.buildMinHeap(nums)
39+
size = len(nums)
40+
for i in range(size):
41+
nums[0], nums[size - i - 1] = nums[size - i - 1], nums[0]
42+
self.heapify(nums, 0, size - i - 2)
43+
return nums
44+
45+
# 升序堆排序,思路如下:
46+
# 1. 先建立大顶堆
47+
# 2. 让堆顶最大元素与最后一个交换,然后调整第一个元素到倒数第二个元素,这一步获取最大值
48+
# 3. 再交换堆顶元素与倒数第二个元素,然后调整第一个元素到倒数第三个元素,这一步获取第二大值
49+
# 4. 以此类推,直到最后一个元素交换之后完毕。
50+
def maxHeapSort(self, arr: [int]):
51+
self.buildMaxHeap(arr)
52+
size = len(arr)
53+
for i in range(size):
54+
arr[0], arr[size - i - 1] = arr[size - i - 1], arr[0]
55+
self.heapify(arr, 0, size - i - 2)
56+
return arr
57+
58+
def sortArray(self, nums: List[int]) -> List[int]:
59+
return self.maxHeapSort(nums)
Lines changed: 105 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,109 @@
11
class ListNode:
2-
def __init__(self, val=0, next=None):
3-
self.val = val
4-
self.next = next
2+
def __init__(self, val=0, next=None):
3+
self.val = val
4+
self.next = next
55

66
class LinkedList:
7-
def __init__(self):
8-
self.head = None
9-
10-
# 根据 data 初始化一个新链表
11-
def create(self, data):
12-
self.head = ListNode(0)
13-
cur = self.head
14-
for i in range(len(data)):
15-
node = ListNode(data[i])
16-
cur.next = node
17-
cur = cur.next
18-
19-
# 获取链表长度
20-
def length(self):
21-
count = 0
22-
cur = self.head
23-
while cur:
24-
count += 1
25-
cur = cur.next
26-
return count
27-
28-
# 查找元素
29-
def find(self, val):
30-
cur = self.head
31-
while cur:
32-
if val == cur.val:
33-
return cur
34-
cur = cur.next
35-
36-
return None
37-
38-
# 头部插入元素
39-
def insertFront(self, val):
40-
node = ListNode(val)
41-
node.next = self.head
42-
self.head = node
43-
44-
# 尾部插入元素
45-
def insertRear(self, val):
46-
node = ListNode(val)
47-
cur = self.head
48-
while cur.next:
49-
cur = cur.next
50-
cur.next = node
7+
def __init__(self):
8+
self.head = None
9+
10+
# 根据 data 初始化一个新链表
11+
def create(self, data):
12+
self.head = ListNode(0)
13+
cur = self.head
14+
for i in range(len(data)):
15+
node = ListNode(data[i])
16+
cur.next = node
17+
cur = cur.next
18+
19+
# 获取链表长度
20+
def length(self):
21+
count = 0
22+
cur = self.head
23+
while cur:
24+
count += 1
25+
cur = cur.next
26+
return count
27+
28+
# 查找元素
29+
def find(self, val):
30+
cur = self.head
31+
while cur:
32+
if val == cur.val:
33+
return cur
34+
cur = cur.next
35+
36+
return None
37+
38+
# 头部插入元素
39+
def insertFront(self, val):
40+
node = ListNode(val)
41+
node.next = self.head
42+
self.head = node
43+
44+
# 尾部插入元素
45+
def insertRear(self, val):
46+
node = ListNode(val)
47+
cur = self.head
48+
while cur.next:
49+
cur = cur.next
50+
cur.next = node
5151

52-
53-
# 中间插入元素
54-
def insertInside(self, index, val):
55-
count = 0
56-
cur = self.head
57-
while cur and count < index - 1:
58-
count += 1
59-
cur = cur.next
60-
61-
if not cur:
62-
return 'Error'
63-
64-
node = ListNode(val)
65-
node.next = cur.next
66-
cur.next = node
67-
68-
# 改变元素
69-
def change(self, index, val):
70-
count = 0
71-
cur = self.head
72-
while cur and count < index:
73-
count += 1
74-
cur = cur.next
75-
76-
if not cur:
77-
return 'Error'
78-
79-
cur.val = val
80-
81-
# 移除链表头部元素
82-
def removeFront(self):
83-
if self.head:
84-
self.head = self.head.next
85-
86-
# 移除链表尾部元素
87-
def removeRear(self):
88-
if not self.head.next:
89-
return 'Error'
90-
91-
cur = self.head
92-
while cur.next.next:
93-
cur = cur.next
94-
cur.next = None
95-
96-
# 移除链表中间元素
97-
def removeInside(self, index):
98-
count = 0
99-
cur = self.head
100-
101-
while cur.next and count < index - 1:
102-
count += 1
103-
cur = cur.next
104-
105-
if not cur:
106-
return 'Error'
107-
108-
del_node = cur.next
109-
cur.next = del_node.next
52+
53+
# 中间插入元素
54+
def insertInside(self, index, val):
55+
count = 0
56+
cur = self.head
57+
while cur and count < index - 1:
58+
count += 1
59+
cur = cur.next
60+
61+
if not cur:
62+
return 'Error'
63+
64+
node = ListNode(val)
65+
node.next = cur.next
66+
cur.next = node
67+
68+
# 改变元素
69+
def change(self, index, val):
70+
count = 0
71+
cur = self.head
72+
while cur and count < index:
73+
count += 1
74+
cur = cur.next
75+
76+
if not cur:
77+
return 'Error'
78+
79+
cur.val = val
80+
81+
# 移除链表头部元素
82+
def removeFront(self):
83+
if self.head:
84+
self.head = self.head.next
85+
86+
# 移除链表尾部元素
87+
def removeRear(self):
88+
if not self.head.next:
89+
return 'Error'
90+
91+
cur = self.head
92+
while cur.next.next:
93+
cur = cur.next
94+
cur.next = None
95+
96+
# 移除链表中间元素
97+
def removeInside(self, index):
98+
count = 0
99+
cur = self.head
100+
101+
while cur.next and count < index - 1:
102+
count += 1
103+
cur = cur.next
104+
105+
if not cur:
106+
return 'Error'
107+
108+
del_node = cur.next
109+
cur.next = del_node.next

0 commit comments

Comments
 (0)