链表
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 # __author__ = '北方姆Q' 4 # __datetime__ = 2018/12/5 19:21 5 6 7 class Node(object): 8 """ 9 节点类 10 """ 11 __slots__ = ("__data", "__next") 12 13 def __init__(self, data=None, next=None): 14 """ 15 16 :param data: 原始数据 17 :param next: 下一步指针 18 """ 19 self.__data = data 20 self.__next = next 21 22 @property 23 def data(self): 24 return self.__data 25 26 @data.setter 27 def data(self, data): 28 self.__data = data 29 30 @property 31 def next(self): 32 return self.__next 33 34 @next.setter 35 def next(self, next): 36 self.__next = next 37 38 39 class LinkedList(object): 40 """ 41 单向链表 42 """ 43 __slots__ = ("__head", "__index") 44 45 def __init__(self): 46 """ 47 初始时头为空,下标初始为-1是为了与list数据类型取值下标保持一致 48 """ 49 self.__head = None 50 self.__index = -1 51 52 def clear(self): 53 """ 54 清空整个链表 55 :return: 56 """ 57 self.__init__() 58 59 def append(self, data): 60 """ 61 向链表的最后增加一条数据,如同list.append(x)的功能 62 :param data: 添加的新数据 63 :return: 64 """ 65 data = data if isinstance(data, Node) else Node(data) 66 67 if self.__head: 68 last_node = self.get_node_by_index(self.__index) 69 last_node.next = data 70 else: 71 self.__head = data 72 self.__index += 1 73 74 def delete_node_by_index(self, index): 75 """ 76 根据下标删除某数据,如同del list[xxx] 77 :param index: 要删除的下标 78 :return: 79 """ 80 if self.__index != -1 and 0 <= index <= self.__index: 81 if self.__index == index == 0: 82 self.clear() 83 elif index == 0: 84 self.__head = self.__head.next 85 self.__index -= 1 86 else: 87 before_node = self.get_node_by_index(index-1) 88 before_node.next = before_node.next.next 89 self.__index -= 1 90 else: 91 raise IndexError("linked list index out of range") 92 93 def insert_node_by_index(self, index, data): 94 """ 95 向下标插入一条数据,如同list.insert(x, x) 96 :param index: 要插入数据的下标 97 :param data: 要插入的数据 98 :return: 99 """ 100 data = data if isinstance(data, Node) else Node(data) 101 102 if self.__index == -1 or index > self.__index: 103 self.append(data) 104 elif index < 0: 105 self.insert_node_by_index(0, data) 106 elif index == 0: 107 data.next = self.__head 108 self.__head = data 109 self.__index += 1 110 else: 111 before_node = self.get_node_by_index(index-1) 112 data.next = before_node.next 113 before_node.next = data 114 self.__index += 1 115 116 def get_node_by_index(self, index): 117 """ 118 根据下标获取数据,如同list[x] 119 :param index: 要获取的下标 120 :return: Node数据类型 121 """ 122 if self.__index != -1 and 0 <= index <= self.__index: 123 i = 0 124 node = self.__head 125 while node and i < index: 126 node = node.next 127 i += 1 128 return node 129 else: 130 raise IndexError("linked list index out of range") 131 132 def __len__(self): 133 """ 134 整个链表的长度 135 :return: int类型 136 """ 137 return self.__index+1 138 139 def __str__(self): 140 """ 141 打印当前链表 142 :return: str类型 143 """ 144 r = [] 145 node = self.__head 146 while node: 147 r.append(node.data) 148 node = node.next 149 return str(r) 150 151 __repr__ = __str__
冒泡排序
1 def bubble_sort(data): 2 length = len(data) 3 4 for i in range(length): 5 is_change = False 6 for j in range(length-i-1): 7 if data[j] > data[j+1]: 8 data[j], data[j+1] = data[j+1], data[j] 9 is_change = True 10 11 if not is_change: 12 break 13 return data
插入排序
1 def insert_sort(data): 2 length = len(data) 3 4 for i in range(length): 5 tmp = data[i] 6 j = i - 1 7 while j >= 0 and data[j] > tmp: 8 data[j+1] = data[j] 9 j -= 1 10 data[j+1] = tmp 11 return data
选择排序
1 def select_sort(data): 2 length = len(data) 3 4 for i in range(length): 5 index = i 6 for j in range(i+1, length): 7 if data[j] < data[index]: 8 index = j 9 10 data[index], data[i] = data[i], data[index] 11 return data
归并排序
1 def merge_sort(data): 2 _merge_sort(data, 0, len(data)-1) 3 return data 4 5 6 def _merge_sort(data, start, end): 7 if start >= end: 8 return 9 10 mid = start + (end-start) // 2 11 _merge_sort(data, start, mid) 12 _merge_sort(data, mid+1, end) 13 14 __merge_sort(data, start, mid, end) 15 16 17 def __merge_sort(data, start, mid, end): 18 tmp = [] 19 i, j = start, mid+1 20 21 while i <= mid and j <= end: 22 if data[i] <= data[j]: 23 tmp.append(data[i]) 24 i += 1 25 else: 26 tmp.append(data[j]) 27 j += 1 28 29 x = i if i <= mid else j 30 y = mid if i <= mid else end 31 tmp.extend(data[x:y+1]) 32 data[start:end+1] = tmp
快速排序
1 def quick_sort(data): 2 _quick_sort(data, 0, len(data)-1) 3 return data 4 5 6 def _quick_sort(data, start, end): 7 if start >= end: 8 return 9 10 q = __quick_sort(data, start, end) 11 _quick_sort(data, start, q) 12 _quick_sort(data, q+1, end) 13 14 15 def __quick_sort(data, start, end): 16 i, tmp = start, data[start] 17 for j in range(start+1, end+1): 18 if data[j] <= tmp: 19 i += 1 20 data[i], data[j] = data[j], data[i] 21 22 data[start], data[i] = data[i], data[start] 23 return i
计数排序
1 def counting_sort(data): 2 length = len(data) 3 4 # 构建计数列表 5 count_list = [0] * (max(data)+1) 6 for i in data: 7 count_list[i] += 1 8 count_list = list(itertools.accumulate(count_list)) 9 10 data_sort_list = [0] * length 11 for i in reversed(data): 12 index = count_list[i] - 1 13 data_sort_list[index] = i 14 count_list[i] -= 1 15 16 return data_sort_list
二分查找
1 def bsearch(data, value): 2 return _bsearch(data, 0, len(data)-1, value) 3 4 5 def _bsearch(data, start, end, value): 6 """ 7 寻找某个数的下标 8 :param data: 列表 9 :param start: 起始点 10 :param end: 结束点 11 :param value: 要寻找的值 12 :return: 该值在列表中的下标 13 """ 14 mid = start + (end-start) // 2 15 if data[mid] == value: 16 return mid 17 elif data[mid] < value: 18 return _bsearch(data, mid+1, end, value) 19 else: 20 return _bsearch(data, start, mid-1, value) 21 22 23 def _bsearch(data, start, end, value): 24 """ 25 寻找某个数的首个下标 26 :param data: 列表 27 :param start: 起始点 28 :param end: 结束点 29 :param value: 要寻找的值 30 :return: 该值在列表中首个下标 31 """ 32 mid = start + (end-start) // 2 33 if data[mid] == value: 34 if mid == 0 or data[mid-1] != value: 35 return mid 36 else: 37 return _bsearch(data, start, mid, value) 38 elif data[mid] < value: 39 return _bsearch(data, mid+1, end, value) 40 else: 41 return _bsearch(data, start, mid-1, value) 42 43 44 def _bsearch(data, start, end, value): 45 """ 46 寻找某个数的最后下标 47 :param data: 列表 48 :param start: 起始点 49 :param end: 结束点 50 :param value: 要寻找的值 51 :return: 该值在列表中最后下标 52 """ 53 mid = start + (end-start) // 2 54 if data[mid] == value: 55 if mid == len(data)-1 or data[mid+1] != value: 56 return mid 57 else: 58 return _bsearch(data, mid+1, end, value) 59 elif data[mid] < value: 60 return _bsearch(data, mid+1, end, value) 61 else: 62 return _bsearch(data, start, mid-1, value) 63 64 65 def _bsearch(data, start, end, value): 66 """ 67 寻找第一个大于等于某个数的下标 68 :param data: 列表 69 :param start: 起始点 70 :param end: 结束点 71 :param value: 要寻找的值 72 :return: 第一个大于等于某个数的下标 73 """ 74 mid = start + (end-start) // 2 75 if data[mid] >= value: 76 if mid == 0 or mid == len(data)-1 or data[mid-1] < value: 77 return mid 78 else: 79 return _bsearch(data, start, mid, value) 80 else: 81 return _bsearch(data, mid+1, end, value) 82 83 84 def _bsearch(data, start, end, value): 85 """ 86 寻找第一个小于等于某个数的下标 87 :param data: 列表 88 :param start: 起始点 89 :param end: 结束点 90 :param value: 要寻找的值 91 :return: 第一个小于等于某个数的下标 92 """ 93 mid = start + (end-start) // 2 94 if data[mid] <= value: 95 if mid == 0 or mid == len(data)-1 or data[mid-1] > value: 96 return mid 97 else: 98 return _bsearch(data, start, mid, value) 99 else: 100 return _bsearch(data, start, mid-1, value)
暴力匹配
1 def bf(main, pattern): 2 """ 3 暴力查找 4 :param main: 主串 5 :param pattern: 模式串 6 :return: 匹配起标 7 """ 8 n = len(main) 9 m = len(pattern) 10 11 if n <= m: 12 return 0 if main == pattern else -1 13 14 for i in range(n-m+1): 15 for j in range(m): 16 if pattern[j] == main[i+j]: 17 if j == m-1: 18 return i 19 else: 20 continue 21 else: 22 break 23 return -1
rk匹配
1 def simple_hash(s, start, end): 2 assert start <= end 3 return sum(map(ord, s[start: end])) 4 5 6 def rk(main, pattern): 7 """ 8 rk查找 9 :param main: 主串 10 :param pattern: 模式串 11 :return: 匹配起标 12 """ 13 n = len(main) 14 m = len(pattern) 15 16 if n <= m: 17 return 0 if main == pattern else -1 18 19 hash_main = [None] * (n-m+1) 20 hash_main[0] = simple_hash(main, 0, m) 21 for i in range(1, n-m+1): 22 hash_main[i] = hash_main[i-1]-simple_hash(main, i-1, i)+simple_hash(main, i+m-1, i+m) 23 24 hash_pattern = simple_hash(pattern, 0, m) 25 for index, value in enumerate(hash_main): 26 if value == hash_pattern: 27 if pattern == main[index:index+m]: 28 return index 29 else: 30 continue 31 else: 32 continue 33 return -1
trie树
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 # __author__ = '北方姆Q' 4 # __datetime__ = 2019/1/15 15:08 5 6 7 SIZE = 26 8 9 10 class TrieNode(object): 11 def __init__(self, data): 12 self.data = data 13 self.is_ending = False 14 self.children = [None] * SIZE 15 16 17 class Trie(object): 18 def __init__(self): 19 self._root = TrieNode('/') 20 21 def insert(self, data): 22 node = self._root 23 24 for index, value in map(lambda x: (ord(x)-ord("a"), x), data): 25 if not node.children[index]: 26 node.children[index] = TrieNode(value) 27 node = node.children[index] 28 node.is_ending = True 29 30 def find(self, data): 31 node = self._root 32 33 for index, value in map(lambda x: (ord(x)-ord("a"), x), data): 34 if not node.children[index]: 35 return False 36 node = node.children[index] 37 return node.is_ending