mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 20:12:07 +08:00
Merge branch 'master' into master
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: array.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
@ -31,7 +31,7 @@ def extend(nums, enlarge):
|
||||
""" 在数组的索引 index 处插入元素 num """
|
||||
def insert(nums, num, index):
|
||||
# 把索引 index 以及之后的所有元素向后移动一位
|
||||
for i in range(len(nums) - 1, index - 1, -1):
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
nums[i] = nums[i - 1]
|
||||
# 将 num 赋给 index 处元素
|
||||
nums[index] = num
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linked_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: my_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
@ -55,11 +55,14 @@ class MyList:
|
||||
""" 删除元素 """
|
||||
def remove(self, index):
|
||||
assert index < self.__size, "索引越界"
|
||||
num = self.nums[index]
|
||||
# 索引 i 之后的元素都向前移动一位
|
||||
for j in range(index, self.__size - 1):
|
||||
self.__nums[j] = self.__nums[j + 1]
|
||||
# 更新元素数量
|
||||
self.__size -= 1
|
||||
# 返回被删除元素
|
||||
return num
|
||||
|
||||
""" 列表扩容 """
|
||||
def extend_capacity(self):
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: leetcode_two_sum.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: space_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: time_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: worst_best_time_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_search.py
|
||||
Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: hashing_search.py
|
||||
Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linear_search.py
|
||||
Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: bubble_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: insertion_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: merge_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
@ -24,15 +24,15 @@ def merge(nums, left, mid, right):
|
||||
i, j = left_start, right_start
|
||||
# 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for k in range(left, right + 1):
|
||||
# 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
# 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if i > left_end:
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
# 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
# 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
elif j > right_end or tmp[i] <= tmp[j]:
|
||||
nums[k] = tmp[i]
|
||||
i += 1
|
||||
# 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
# 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else:
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: quick_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: array_queue.py
|
||||
Created Time: 2022-12-01
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
@ -42,7 +42,6 @@ class ArrayQueue:
|
||||
|
||||
""" 出队 """
|
||||
def poll(self):
|
||||
# 删除头结点
|
||||
num = self.peek()
|
||||
# 队头指针向后移动一位,若越过尾部则返回到数组头部
|
||||
self.__front = (self.__front + 1) % self.capacity()
|
||||
@ -50,19 +49,11 @@ class ArrayQueue:
|
||||
|
||||
""" 访问队首元素 """
|
||||
def peek(self):
|
||||
# 删除头结点
|
||||
if self.is_empty():
|
||||
print("队列为空")
|
||||
return False
|
||||
return self.__nums[self.__front]
|
||||
|
||||
""" 访问指定位置元素 """
|
||||
def get(self, index):
|
||||
if index >= self.size():
|
||||
print("索引越界")
|
||||
return False
|
||||
return self.__nums[(self.__front + index) % self.capacity()]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
res = [0] * self.size()
|
||||
@ -90,10 +81,6 @@ if __name__ == "__main__":
|
||||
peek = queue.peek()
|
||||
print("队首元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = queue.get(3)
|
||||
print("队列索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出队 """
|
||||
poll = queue.poll()
|
||||
print("出队元素 poll =", poll)
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: array_stack.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
@ -27,15 +27,13 @@ class ArrayStack:
|
||||
|
||||
""" 出栈 """
|
||||
def pop(self):
|
||||
assert not self.is_empty(), "栈为空"
|
||||
return self.__stack.pop()
|
||||
|
||||
""" 访问栈顶元素 """
|
||||
def peek(self):
|
||||
assert not self.is_empty(), "栈为空"
|
||||
return self.__stack[-1]
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
def get(self, index):
|
||||
return self.__stack[index]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
@ -59,10 +57,6 @@ if __name__ == "__main__":
|
||||
peek = stack.peek()
|
||||
print("栈顶元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = stack.get(3)
|
||||
print("栈索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出栈 """
|
||||
pop = stack.pop()
|
||||
print("出栈元素 pop =", pop)
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: deque.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linkedlist_queue.py
|
||||
Created Time: 2022-12-01
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linkedlist_stack.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: queue.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: stack.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_tree.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import collections
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linked_list.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
class ListNode:
|
||||
"""Definition for a singly-linked list node
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: print_util.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import copy
|
||||
import queue
|
||||
|
||||
Reference in New Issue
Block a user