""" Module Description: 字符串相关 Problem: Solution: Date: 2020/2/18 Author: Wang P """
classReverseString: """ 字符数组反转 """ defreverse_string(self, x, reverse=False): """ :param x: 字符数组 :param reverse: 是否之用内置库 :return: """ if reverse: x.reverse() return x else: ifnot x: return beg, end = 0, len(x)-1 while beg < end: x[beg], x[end] = x[end], x[beg] beg += 1 end -= 1 return x
classPalindrome: """ 判断字符串是否是回文 """ defis_palindrome(self, x): ifisinstance(x, int) and x < 0: returnFalse xx = str(x) beg, end = 0, len(xx)-1 while beg < end: if xx[beg] == xx[end]: beg += 1 end -= 1 else: returnFalse returnTrue
if __name__ == "__main__": r = ReverseString() print(r.reverse_string(['d', 'h', 's', 'j', 'f'], False))
defget(self, key): if key in self._od: val = self._od[key] self._od.move_to_end(key) return val else: return -1
defpush(self, key, value): if key in self._od: del self._od[key] self._od[key] = value else: # insert self._od[key] = value iflen(self._od) > self._capacity: self._od.popitem(last=False)
defmerge_sorted_list(self, list_a, list_b): """ 合并两个有序序列 :param list_a: :param list_b: :return: """ length_a = len(list_a) length_b = len(list_b) a = b = 0 new_list = [] while a < length_a and b < length_b: if list_a[a] < list_b[b]: new_list.append(list_a[a]) a += 1 else: new_list.append(list_b[b]) b += 1 if a < length_a: new_list.extend(list_a[a:]) if b < length_b: new_list.extend(list_b[b:]) return new_list
if __name__ == "__main__": import random ms = MergeSort() ll = list(range(10)) random.shuffle(ll) print(ll) ll = ms.merge_sort(ll) print(ll)
""" Module Description: 快速排序算法-分治法 Problem: Solution:Partition:选择基准分割数组为两个字数组,小于基准和大于基准 对两个字数组分别快排 合并结果 Date: 2020/2/15 Author: Wang P """ import random import pysnooper # 调试神器
classQuickSort:
@pysnooper.snoop() defquick_sort(self, arr): iflen(arr) < 2: return arr pivot_index = 0# 第一个数作为pivot pivot = arr[pivot_index] less_part = [i for i in arr[pivot_index+1:] if i < pivot] great_part = [i for i in arr[pivot_index+1:] if i >= pivot] return self.quick_sort(less_part) + [pivot] + self.quick_sort(great_part)
deftraversal(self): cur = self._head link_list = [] while cur: link_list.append(cur.val) cur = cur.next print(link_list)
classMergeLinkedList: """ 合并两个有序链表 思路:新建一个链表root,将list_one, list_two遍历,按大小放入root """ def__init__(self, list_one=None, list_two=None): if list_one andisinstance(list_one, list) and list_two andisinstance(list_two, list): self.list_one = ListNode(list_one[0]) cur_index = self.list_one for i inrange(1, len(list_one)): cur_index.next = ListNode(list_one[i]) cur_index = cur_index.next self.list_two = ListNode(list_two[0]) cur_index = self.list_two for i inrange(1, len(list_two)): cur_index.next = ListNode(list_two[i]) cur_index = cur_index.next else: self.list_one = None self.list_two = None self._head = None
defmerge_linked_list(self): one_cur_val = self.list_one two_cur_val = self.list_two self._head = ListNode(None) cur = self._head while one_cur_val and two_cur_val: if one_cur_val.val < two_cur_val.val: node = ListNode(one_cur_val.val) one_cur_val = one_cur_val.next else: node = ListNode(two_cur_val.val) two_cur_val = two_cur_val.next cur.next = node cur = node cur.next = one_cur_val or two_cur_val
deftraversal(self): cur = self._head.next link_list = [] while cur: link_list.append(cur.val) cur = cur.next print(link_list)
classReverseLinkList: """ 单链表反转 """ def__init__(self, head=None): """链表的头部""" self._head = head
defadd(self, val: int): """ 给链表添加元素 :param val: 传过来的数字 :return: """ # 创建一个节点 node = ListNode(val) if self._head isNone: self._head = node else: cur = self._head while cur.nextisnotNone: cur = cur.next# 移动游标 cur.next = node # 如果 next 后面没了证明以及到最后一个节点了
deftraversal(self): ifnot self._head: return cur = self._head link_list = [] while cur: link_list.append(cur.val) cur = cur.next print(link_list)
defsize(self): """ 获取链表的大小 :return: """ count = 0 if self._head isNone: return count else: cur = self._head while cur isnotNone: count += 1 cur = cur.next return count
defreverse_link(self): """ 单链表反转 思路: 让 cur.next 先断开即指向 none,指向设定 pre 游标指向断开的元素,然后 cur.next 指向断开的元素,再把开始 self._head 再最后一个元素的时候. :return: """ if self._head isNoneor self.size() == 1: return else: pre = None cur = self._head while cur isnotNone: post = cur.next cur.next = pre pre = cur cur = post self._head = pre # 逆向后的头节点
defpop(self): """ 出栈 :rtype: int """ count = self.sq1.qsize() if count == 0: returnFalse while count > 1: x = self.sq1.get() self.sq1.put(x) count -= 1 return self.sq1.get()
deftop(self): """ Get the top element. :rtype: int """ count = self.sq1.qsize() if count == 0: returnFalse while count: x = self.sq1.get() self.sq1.put(x) count -= 1 return x
defempty(self): """ Returns whether the stack is empty. :rtype: bool """ return self.sq1.empty()
deflevel_order_travel(self, root): """ 层次遍历 :param root: :return: list[list[int]] """ ifnot root: print([]) return cur_nodes = [root] next_nodes = [] print([i.val for i in cur_nodes]) while cur_nodes or next_nodes: for node in cur_nodes: if node.left: next_nodes.append(node.left) if node.right: next_nodes.append(node.right) if next_nodes: print([i.val for i in next_nodes]) cur_nodes = next_nodes next_nodes = []
递归函数:Recursion is a process for solving problems by subdividing a larger problem into smaller cases of the problem itself and then solving the smaller, more trivial parts.
Properties of Recursion: 使用stack解决的问题都能用递归解决
A recursive solution must contain a base case; 递归出口,代表最小子问题(n == 0退出打印)
A recursive solution must contain a recursive case; 可以分解的子问题
A recursive solution must make progress toward the base case. 递减n使得n像递归出口靠近
Tail Recursion: occurs when a function includes a single recursive call as the last statement of the function. In this case, a stack is not needed to store values to te used upon the return of the recursive call and thus a solution can be implemented using a iterative loop instead.