diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..39ee28517 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,39 @@ +# Default ignored files +/workspace.xml +# Project exclude paths +/. + + + + + + + + + + + + + +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. +# Project exclude paths +/. \ No newline at end of file diff --git a/.idea/algorithm010.iml b/.idea/algorithm010.iml new file mode 100644 index 000000000..b107a2dd8 --- /dev/null +++ b/.idea/algorithm010.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/google-java-format.xml b/.idea/google-java-format.xml new file mode 100644 index 000000000..2aa056da3 --- /dev/null +++ b/.idea/google-java-format.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 000000000..6560a9898 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/leetcode/editor.xml b/.idea/leetcode/editor.xml new file mode 100644 index 000000000..3b3f45c4c --- /dev/null +++ b/.idea/leetcode/editor.xml @@ -0,0 +1,177 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..6c07f54a3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..39a5bed6d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/production/algorithm010/Common/BuildBinaryTree.class b/.idea/production/algorithm010/Common/BuildBinaryTree.class new file mode 100644 index 000000000..31ec6f8b5 Binary files /dev/null and b/.idea/production/algorithm010/Common/BuildBinaryTree.class differ diff --git a/.idea/production/algorithm010/Common/TreeNode.class b/.idea/production/algorithm010/Common/TreeNode.class new file mode 100644 index 000000000..c423450c1 Binary files /dev/null and b/.idea/production/algorithm010/Common/TreeNode.class differ diff --git a/.idea/production/algorithm010/README.md b/.idea/production/algorithm010/README.md new file mode 100644 index 000000000..c2f794952 --- /dev/null +++ b/.idea/production/algorithm010/README.md @@ -0,0 +1,28 @@ +# 极客大学「算法训练营-第10期」作业提交仓库 + + +## 讲师课件下载地址 + +请大家通过该链接查看讲师课件并进行下载,链接:https://pan.baidu.com/s/1GOuXJfnlERQs8bI8HNwPrQ 密码:zlua + + +## 仓库目录结构说明 + +1. `week01/` 代表第一周作业提交目录,以此类推。 +2. 请在对应周的目录下新建或修改自己的代码作业。 +2. 每周均有一个 `REDAME.md` 文档,你可以将自己当周的学习心得以及做题过程中的思考记录在该文档中。 + +## 作业提交规则 + +1. 先将本仓库 Fork 到自己 GitHub 账号下。 +2. 将 Fork 后的仓库 Clone 到本地,然后在本地仓库中对应周的目录下新建或修改自己的代码作业,当周的学习总结写在对应周的README.md文件里。 +3. 在本地仓库完成作业后,push 到自己的 GitHub 远程仓库。 +4. 最后将远程仓库中当周的作业链接,按格式贴到班级仓库对应学习周的issue下面。 +5. 提交issue请务必按照规定格式进行提交,否则作业统计工具将抓取不到你的作业提交记录。 + +详细的作业提交流程可以查阅:https://shimo.im/docs/m5rtM8K8rNsjw5jk/ + + +## 注意事项 + + 如果对 Git 和 GitHub 不太了解,请参考 [Git 官方文档](https://git-scm.com/book/zh/v2) 或者极客时间的[《玩转 Git 三剑客》](https://time.geekbang.org/course/intro/145)视频课程。 diff --git a/.idea/production/algorithm010/Week01/LC189_rotate-array.md b/.idea/production/algorithm010/Week01/LC189_rotate-array.md new file mode 100644 index 000000000..ec7ca573f --- /dev/null +++ b/.idea/production/algorithm010/Week01/LC189_rotate-array.md @@ -0,0 +1,17 @@ + +class Solution { + + //Brute-force + //move k times for all elements - O(k^n) + + public void rotate(int[] nums, int k) { + int length = nums.length; + for(int i=0;i0;j--){ + nums[j] = nums[j-1]; + } + nums[0] = tmp; + } + } +} \ No newline at end of file diff --git a/.idea/production/algorithm010/Week01/LC206_ReverseLinkedList.md b/.idea/production/algorithm010/Week01/LC206_ReverseLinkedList.md new file mode 100644 index 000000000..cc4945ef8 --- /dev/null +++ b/.idea/production/algorithm010/Week01/LC206_ReverseLinkedList.md @@ -0,0 +1,104 @@ +/* +第一次写道这样就试着运行,链表和Recursive不清楚 :-) +*/ +class Solution { + + ListNode res = new ListNode(); + + public ListNode reverseList(ListNode head) { + + if (head == null){ + return null; + } + + res.next = head; + res = res.next; + + return reverseList(head.next); + } + +} + +//因为要达到末尾时才能够进行连接,所以要在出栈的时候(recursive 调用后) +class Solution { + + ListNode res = new ListNode(); + + public ListNode reverseList(ListNode head) { + helper(head); + return res; + } + private ListNode helper(ListNode node){ + if(node == null || node.next == null){ + res = node; + return node; + } + ListNode temp = helper(node.next); + temp.next = node; + node.next = null; + return node; + } +} + +/*看完题解后: +1. 迭代法: 用 prev, curr, head 反转指针 +prev = head; +curr = head.next; +curr.next = prev; //反转指针 +head = curr; + +定义两个指针: pre 和 cur ;pre 在前 cur 在后。 +每次让 pre 的 next 指向 cur ,实现一次局部反转 +局部反转完成之后,pre 和 cur 同时往前移动一个位置 +循环上述过程,直至 pre 到达链表尾部 + +*/ +class Solution { + public ListNode reverseList(ListNode head) { + ListNode cur = null, pre=head; + while(pre!=null){ + ListNode tmp = pre.next; + pre.next = cur; + cur = pre; + pre = tmp; + } + return cur; + } + //时间复杂度:O(n)O(n) + //空间复杂度:O(1)O(1) +} + +/* 2.递归 +递归就是压栈,在递归调用前的code就是压栈前要执行的,在递归调用后的code就是出栈后执行的。 +有没有一种方法/公式,知道是这段代码是放在递归前还是递归后 + */ +class Solution { + public ListNode reverseList(ListNode head) { + //terminator + if(head == null || head.next == null){ + return head; + } + ListNode node = reverseList(head.next); //这里会一直迭代直到触发terminator(走到链表最后),然后才执行下面语句 + head.next.next = head; + head.next = null; //这里是为了断开连接 + return node; + } +} + +/* 递归 2 + return helper(nextNode, curr); //用value互换来达到指针反转 + */ +class Solution { + public ListNode reverseList(ListNode head) { + return helper(head, null); + } + + private ListNode helper(ListNode curr, ListNode newList){ + if (curr == null){ + return newList; + } + ListNode nextNode = curr.next; + curr.next = newList; + return helper(nextNode, curr); //用参数互换来达到指针反转 + } +} \ No newline at end of file diff --git a/.idea/production/algorithm010/Week01/LC21_merge-two-sorted-lists.md b/.idea/production/algorithm010/Week01/LC21_merge-two-sorted-lists.md new file mode 100644 index 000000000..a84347b6d --- /dev/null +++ b/.idea/production/algorithm010/Week01/LC21_merge-two-sorted-lists.md @@ -0,0 +1,30 @@ + +class Solution { + + /* + 思路 + + 终止条件:两条链表分别名为 l1 和 l2,当 l1 为空或 l2 为空时结束 + 返回值:每一层调用都返回排序好的链表头 + 本级递归内容:如果 l1 的 val 值更小,则将 l1.next 与排序好的链表头相接,l2 同理 + O(m+n)O(m+n),mm 为 l1的长度,nn 为 l2 的长度 + */ + + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + //terminator: + if(l1 == null){ + return l2; + } + if(l2 == null){ + return l1; + } + if (l1.val < l2.val){ + l1.next = mergeTwoLists(l1.next, l2); + return l1; + } + else { + l2.next = mergeTwoLists(l1, l2.next); + return l2; + } + } +} \ No newline at end of file diff --git a/.idea/production/algorithm010/Week01/LC26_remove-duplicates-from-sorted-array.md b/.idea/production/algorithm010/Week01/LC26_remove-duplicates-from-sorted-array.md new file mode 100644 index 000000000..7c440b78b --- /dev/null +++ b/.idea/production/algorithm010/Week01/LC26_remove-duplicates-from-sorted-array.md @@ -0,0 +1,21 @@ + +class Solution { + + + //代替法,用j保存修改数组的位置 + + public int removeDuplicates(int[] nums) { + if (nums == null || nums.length==0){ + return 0; + } + int j =0; + for(int i=0;i=0) { + // when nums1 reach the head, p1 will become -1 + if (p1 == -1 || nums1[p1] <= nums2[p2]) { + nums1[p] = nums2[p2]; + p2--; + } + else{ + nums1[p] = nums1[p1]; + p1--; + } + p--; + + nums1[p--] = (p1 == -1 || nums1[p1] <= nums2[p2]) ? nums2[p2--] : nums1[p1--]; + } + } +} + +//可以简化成四行 +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int p1 = m - 1, p2 = n - 1, p = m + n - 1; + while (p2 >= 0) { + nums1[p--] = (p1 == -1 || nums1[p1] <= nums2[p2]) ? nums2[p2--] : nums1[p1--]; + } + } +} \ No newline at end of file diff --git a/.idea/production/algorithm010/Week01/LC_283_MoveZeroes.md b/.idea/production/algorithm010/Week01/LC_283_MoveZeroes.md new file mode 100644 index 000000000..500ef6dbb --- /dev/null +++ b/.idea/production/algorithm010/Week01/LC_283_MoveZeroes.md @@ -0,0 +1,47 @@ +class Solution { + + + public void moveZeroes(int[] nums) { + //把0用后面的数代替,并统计0的个数记录在j上 + int j =0; + for(int i=0;i=0;i--){ + digits[i]++; + digits[i] = digits[i] % 10; + //e.g. 1,9,9; when it becomes 200. it will end th loop + if (digits[i] != 0) return digits; + } + // reach here, only when all digits are 9. e.g. 999 + // in java, int[] initia with '0' + int[] newDigits = new int[digits.length +1]; + // System.arraycopy(digits,0,newDigits,1,digits.length); + newDigits[0] = 1; + return newDigits; + + } +} \ No newline at end of file diff --git a/.idea/production/algorithm010/Week01/LeetCode_11.md b/.idea/production/algorithm010/Week01/LeetCode_11.md new file mode 100644 index 000000000..96824c7e6 --- /dev/null +++ b/.idea/production/algorithm010/Week01/LeetCode_11.md @@ -0,0 +1,27 @@ +class Solution { + + + //11. 盛最多水的容器 + + public int maxArea(int[] height) { + + int len = height.length; + int i=0, j=len-1,h=0; + int maxArea =0; + + while (i> threeSum(int[] nums) { + List> result = new ArrayList<>(); + int len = nums.length; + if (nums == null || len<3) { + return result; + } + //sort是关键,排序完就很容易去重了 + Arrays.sort(nums); + + for (int i=0;i0 && nums[i] == nums[i-1]) continue; + //用while一样效果,不用回到for去判断 + while (i>0 && i0) break; + + int left = i+1; + int right = len-1; + while (left stack = new LinkedList(); + stack.addFirst("a"); + stack.addFirst("b"); + stack.addFirst("c"); + System.out.println(stack); + + String str = stack.peek(); + System.out.println(str); + System.out.println(stack); + while (stack.size()>0){ + System.out.println(stack.removeFirst()); + } + System.out.println(stack); + diff --git a/.idea/production/algorithm010/Week02/BinanryTree_Traversal$TreeNode.class b/.idea/production/algorithm010/Week02/BinanryTree_Traversal$TreeNode.class new file mode 100644 index 000000000..1508b2f1e Binary files /dev/null and b/.idea/production/algorithm010/Week02/BinanryTree_Traversal$TreeNode.class differ diff --git a/.idea/production/algorithm010/Week02/BinanryTree_Traversal.class b/.idea/production/algorithm010/Week02/BinanryTree_Traversal.class new file mode 100644 index 000000000..30fd3db6a Binary files /dev/null and b/.idea/production/algorithm010/Week02/BinanryTree_Traversal.class differ diff --git a/.idea/production/algorithm010/Week02/BinaryHeap.class b/.idea/production/algorithm010/Week02/BinaryHeap.class new file mode 100644 index 000000000..fa9e64fc0 Binary files /dev/null and b/.idea/production/algorithm010/Week02/BinaryHeap.class differ diff --git "a/.idea/production/algorithm010/Week02/HashMap\346\200\273\347\273\223.md" "b/.idea/production/algorithm010/Week02/HashMap\346\200\273\347\273\223.md" new file mode 100644 index 000000000..465cb6d27 --- /dev/null +++ "b/.idea/production/algorithm010/Week02/HashMap\346\200\273\347\273\223.md" @@ -0,0 +1,161 @@ +一,HashMap的内部存储结构 + +Java中数据存储方式最底层的两种结构,一种是数组,另一种就是链表。
+ +数组的特点:连续空间,寻址迅速,但是在删除或者添加元素的时候需要有较大幅度的移动,所以查询速度快,增删较慢。
+链表的特点:由于空间不连续,寻址困难,增删元素只需修改指针,所以查询慢、增删快。
+综合这两者的优点,摒弃缺点,哈希表就诞生了,既满足了数据查找方面的特点,占用的空间也不大。
+ +二、底层原理 + +HashMap的实现主要用到了哈希表的链地址法。即使用数组+链表的方式实现。
+hashMap实现了Map接口,继承了AbstractMap类。其中Map接口定义了键映射到值的规则,AbstractMap类提供了Map接口的骨干实现。
+hashMap底层有一个entry数组(entry是hashMap的内部类,它包含了键key、值value、下一个节点next,以及hash值,
+正是由于Entry才构成了table数组的项为链表),数组上每一项都有一个链表,如果链表长度超过阀值( TREEIFY THRESHOLD==8),就把链表转成红黑树,链表长度低于6,就把红黑树转回链表 + +JDK 1.7 有源码分析
+三、HashMap提供的三个构造函数: + +1、HashMap():
+构造一个具有默认初始容量 (16) 和默认加载因子 (0.75) 的空 HashMap。 + +2、HashMap(int initialCapacity):
+构造一个带指定初始容量和默认加载因子 (0.75) 的空 HashMap。 + +3、HashMap(int initialCapacity, float loadFactor):
+构造一个带指定初始容量和加载因子的空 HashMap。
+ +常量
+static final int DEFAULT_INITIAL_CAPACITY = 16; +初始容量:16 +static final int MAXIMUM_CAPACITY = 1 << 30; +最大容量:2的30次方 => 1073741824 +static final float DEFAULT_LOAD_FACTOR = 0.75f; +负载因子:75% + +** put(Object key,Object value)方法: + +作用是存储一个键-值对 + + public V put(K key, V value) { + if (key == null) + return putForNullKey(value); + int hash = hash(key.hashCode()); + int i = indexFor(hash, table.length); + for (Entry e = table[i]; e != null; e = e.next) { + Object k; + if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { + V oldValue = e.value; + e.value = value; + e.recordAccess(this); + return oldValue; + } + } + + modCount++; + addEntry(hash, key, value, i); + return null; + } + + 处理步骤如下:
+ (1)判断key是否为null,若为null,调用putForNullKey(value)处理。这个方法代码如下:
+ + /** + * Offloaded version of put for null keys + */ + private V putForNullKey(V value) { + for (Entry e = table[0]; e != null; e = e.next) { + if (e.key == null) { + V oldValue = e.value; + e.value = value; + e.recordAccess(this); + return oldValue; + } + } + modCount++; + addEntry(0, null, value, 0); + return null; + } + + +从代码可以看出,如果key为null的值,默认就存储到table[0]开头的链表了。然后遍历table[0]的链表的每个节点Entry, +如果发现其中存在节点Entry的key为null,就替换新的value,然后返回旧的value, +如果没发现key等于null的节点Entry,就增加新的节点。
+ +(2)先计算key的hashcode,在使用计算的结果二次hash,使用indexFor(hash, table.length)方法找到Entry数组的索引i的位置。
+ +(3)接着遍历以table[i]为头结点的链表,如果发现已经存在节点的hash、key值与条件相同时, +将该节点的value值替换为新的value值,然后返回旧的value值。
+ +(4)如果未找到hash、key值均相同的节点,则调用addEntry方法增加新的节点(头插法)。代码如下:
+ + void addEntry(int hash, K key, V value, int bucketIndex) { + Entry e = table[bucketIndex]; + table[bucketIndex] = new Entry(hash, key, value, e); + if (size++ >= threshold) + resize(2 * table.length); + } + + +**get(Object key)方法: + +作用是根据键来获取值
+ + public V get(Object key) { + if (key == null) + return getForNullKey(); + int hash = hash(key.hashCode()); + for (Entry e = table[indexFor(hash, table.length)]; + e != null; + e = e.next) { + Object k; + if (e.hash == hash && ((k = e.key) == key || key.equals(k)))//-------------------1---------------- + return e.value; + } + return null; + } + + +处理步骤如下:
+(1)当key为null时,调用getForNullKey(),它的源码如下:
+ + private V getForNullKey() { + for (Entry e = table[0]; e != null; e = e.next) { + if (e.key == null) + return e.value; + } + return null; + } + +返回table[0]开头的链表的键为null的节点的值
+ +(2)当键不为null时,依然计算hash值,然后找到具体在哪个table[indexFor(hash, table.length)] +节点开头的链表中,遍历此链表查找是否存在搜索条件中的key值,返回其value。若没有符合条件的key值,返回null。
+ + +***HashMap常考问题总结
+ +最后补充一些面试时候常问到的一些问题总结。 + +(1)HashMap和HashTable的区别?
+ +HashMap是非线程安全的,HashTable是线程安全的
+HashMap的键和值都允许有null值存在,而HashTable则不行
+因为线程安全的问题,HashMap效率比HashTable的要高
+哈希值的使用不同,HashMap要根据hashCode二次计算得到hash值,而HashTable直接使用对象的hashCode
+继承的父类不同,HashMap继承自AbstractMap,而HashTable继承自Dictionary
+ +(2)HashMap中的键可以是任何对象或数据类型吗?
+ +可以为null,但是不能为可变对象。如果为可变对象的话,对象中的属性改变则对象的hashCode也进行了相应的改变,导致下次无法查找到已存在Map中的数据。 +如果可变对象在HashMap中被当做键,那么就要小心在它的属性改变时,不要改变它的hashCode。只要保证成员变量的改变不会相应改变其hashCode即可。 +
+(3)HashTable如何实现线程安全?
+ +实现原理是在对应的方法上添加了synchronized关键字进行修饰,由于在执行此方法时需要获得对象锁, +因此执行起来比较慢。如果想实现线程安全的HashMap的话,推荐使用ConcurrentHashMap。
+ + +参考资料: +链接:https://www.jianshu.com/p/2f50d21dbfdc + diff --git a/.idea/production/algorithm010/Week02/LC_200_number_of_islands.class b/.idea/production/algorithm010/Week02/LC_200_number_of_islands.class new file mode 100644 index 000000000..bc026d83e Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_200_number_of_islands.class differ diff --git a/.idea/production/algorithm010/Week02/LC_22_generate_parentheses$Node.class b/.idea/production/algorithm010/Week02/LC_22_generate_parentheses$Node.class new file mode 100644 index 000000000..9d8411f6f Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_22_generate_parentheses$Node.class differ diff --git a/.idea/production/algorithm010/Week02/LC_22_generate_parentheses.class b/.idea/production/algorithm010/Week02/LC_22_generate_parentheses.class new file mode 100644 index 000000000..b3e7969e8 Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_22_generate_parentheses.class differ diff --git a/.idea/production/algorithm010/Week02/LC_239_sliding_window_maximum.class b/.idea/production/algorithm010/Week02/LC_239_sliding_window_maximum.class new file mode 100644 index 000000000..99251d039 Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_239_sliding_window_maximum.class differ diff --git a/.idea/production/algorithm010/Week02/LC_242_valid_anagram.class b/.idea/production/algorithm010/Week02/LC_242_valid_anagram.class new file mode 100644 index 000000000..1842b497b Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_242_valid_anagram.class differ diff --git a/.idea/production/algorithm010/Week02/LC_264_ugly_number_ii.class b/.idea/production/algorithm010/Week02/LC_264_ugly_number_ii.class new file mode 100644 index 000000000..a375b104c Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_264_ugly_number_ii.class differ diff --git a/.idea/production/algorithm010/Week02/LC_347_top_k_frequent_elements$1.class b/.idea/production/algorithm010/Week02/LC_347_top_k_frequent_elements$1.class new file mode 100644 index 000000000..4a9be3536 Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_347_top_k_frequent_elements$1.class differ diff --git a/.idea/production/algorithm010/Week02/LC_347_top_k_frequent_elements.class b/.idea/production/algorithm010/Week02/LC_347_top_k_frequent_elements.class new file mode 100644 index 000000000..1c0b1f4c1 Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_347_top_k_frequent_elements.class differ diff --git a/.idea/production/algorithm010/Week02/LC_49_Group_anagrams.class b/.idea/production/algorithm010/Week02/LC_49_Group_anagrams.class new file mode 100644 index 000000000..220223e1c Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_49_Group_anagrams.class differ diff --git a/.idea/production/algorithm010/Week02/LC_m40_small_k_digits.class b/.idea/production/algorithm010/Week02/LC_m40_small_k_digits.class new file mode 100644 index 000000000..d4f7ca5df Binary files /dev/null and b/.idea/production/algorithm010/Week02/LC_m40_small_k_digits.class differ diff --git a/.idea/production/algorithm010/Week02/NOTE.md b/.idea/production/algorithm010/Week02/NOTE.md new file mode 100644 index 000000000..d6635abf7 --- /dev/null +++ b/.idea/production/algorithm010/Week02/NOTE.md @@ -0,0 +1,94 @@ +学习笔记 +ArrayList / List 初始化: +List list = Arrays.asList(1,2); + +Array 初始化: +int[]{map.get(target-nums[i]),nums[i]}; + +HashMap 源码: +为啥要这么多中间变量呢? tab, first, e; n,k 感觉诚心让我们不容易看懂源码:-) +这些中间变量的作用是存储取出的值? tab = table 有啥作用,为啥不能直接在table上操作? + +putVal时,如果hash key 相同的值超过7个时变为TreeNode(红黑树) + if (binCount >= 7) { this.treeifyBin(tab, hash); } +--------------------------------------------- + final Node getNode(int hash, Object key) { + Node[] tab; Node first, e; int n; K k; + if ((tab = table) != null && (n = tab.length) > 0 && + (first = tab[(n - 1) & hash]) != null) { + if (first.hash == hash && // always check first node + ((k = first.key) == key || (key != null && key.equals(k)))) + return first; + if ((e = first.next) != null) { + if (first instanceof TreeNode) + return ((TreeNode)first).getTreeNode(hash, key); + do { + if (e.hash == hash && + ((k = e.key) == key || (key != null && key.equals(k)))) + return e; + } while ((e = e.next) != null); + } + } + return null; + } + +------------------------ + + final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { + HashMap.Node[] tab; + int n; + if ((tab = this.table) == null || (n = tab.length) == 0) { + n = (tab = this.resize()).length; + } + + Object p; + int i; + if ((p = tab[i = n - 1 & hash]) == null) { + tab[i] = this.newNode(hash, key, value, (HashMap.Node)null); + } else { + Object e; + Object k; + if (((HashMap.Node)p).hash == hash && ((k = ((HashMap.Node)p).key) == key || key != null && key.equals(k))) { + e = p; + } else if (p instanceof HashMap.TreeNode) { + e = ((HashMap.TreeNode)p).putTreeVal(this, tab, hash, key, value); + } else { + int binCount = 0; + + while(true) { + if ((e = ((HashMap.Node)p).next) == null) { + ((HashMap.Node)p).next = this.newNode(hash, key, value, (HashMap.Node)null); + if (binCount >= 7) { + this.treeifyBin(tab, hash); + } + break; + } + + if (((HashMap.Node)e).hash == hash && ((k = ((HashMap.Node)e).key) == key || key != null && key.equals(k))) { + break; + } + + p = e; + ++binCount; + } + } + + if (e != null) { + V oldValue = ((HashMap.Node)e).value; + if (!onlyIfAbsent || oldValue == null) { + ((HashMap.Node)e).value = value; + } + + this.afterNodeAccess((HashMap.Node)e); + return oldValue; + } + } + + ++this.modCount; + if (++this.size > this.threshold) { + this.resize(); + } + + this.afterNodeInsertion(evict); + return null; + } diff --git a/.idea/production/algorithm010/Week02/test.class b/.idea/production/algorithm010/Week02/test.class new file mode 100644 index 000000000..e427b2b9e Binary files /dev/null and b/.idea/production/algorithm010/Week02/test.class differ diff --git "a/.idea/production/algorithm010/Week03/.NOTE_images/\345\233\236\346\234\224\347\256\227\346\263\225.png" "b/.idea/production/algorithm010/Week03/.NOTE_images/\345\233\236\346\234\224\347\256\227\346\263\225.png" new file mode 100644 index 000000000..1cbd75499 Binary files /dev/null and "b/.idea/production/algorithm010/Week03/.NOTE_images/\345\233\236\346\234\224\347\256\227\346\263\225.png" differ diff --git a/.idea/production/algorithm010/Week03/LC105_construct_binary_tree_from_preorder$TreeNode.class b/.idea/production/algorithm010/Week03/LC105_construct_binary_tree_from_preorder$TreeNode.class new file mode 100644 index 000000000..ca0cb93c2 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC105_construct_binary_tree_from_preorder$TreeNode.class differ diff --git a/.idea/production/algorithm010/Week03/LC105_construct_binary_tree_from_preorder.class b/.idea/production/algorithm010/Week03/LC105_construct_binary_tree_from_preorder.class new file mode 100644 index 000000000..c08d42731 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC105_construct_binary_tree_from_preorder.class differ diff --git a/.idea/production/algorithm010/Week03/LC111_min_depth_b_tree.class b/.idea/production/algorithm010/Week03/LC111_min_depth_b_tree.class new file mode 100644 index 000000000..1123f66f5 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC111_min_depth_b_tree.class differ diff --git a/.idea/production/algorithm010/Week03/LC236_lowest_common_ancestor$TreeNode.class b/.idea/production/algorithm010/Week03/LC236_lowest_common_ancestor$TreeNode.class new file mode 100644 index 000000000..20dac47ff Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC236_lowest_common_ancestor$TreeNode.class differ diff --git a/.idea/production/algorithm010/Week03/LC236_lowest_common_ancestor.class b/.idea/production/algorithm010/Week03/LC236_lowest_common_ancestor.class new file mode 100644 index 000000000..8382e98c7 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC236_lowest_common_ancestor.class differ diff --git a/.idea/production/algorithm010/Week03/LC46_permutations.class b/.idea/production/algorithm010/Week03/LC46_permutations.class new file mode 100644 index 000000000..6be9c2f9e Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC46_permutations.class differ diff --git a/.idea/production/algorithm010/Week03/LC47_permutations_ii.class b/.idea/production/algorithm010/Week03/LC47_permutations_ii.class new file mode 100644 index 000000000..10398ffc5 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC47_permutations_ii.class differ diff --git a/.idea/production/algorithm010/Week03/LC51_n_queens.class b/.idea/production/algorithm010/Week03/LC51_n_queens.class new file mode 100644 index 000000000..58132298a Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC51_n_queens.class differ diff --git a/.idea/production/algorithm010/Week03/LC_17_Letter_combinations_of_phone$1.class b/.idea/production/algorithm010/Week03/LC_17_Letter_combinations_of_phone$1.class new file mode 100644 index 000000000..63847311e Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC_17_Letter_combinations_of_phone$1.class differ diff --git a/.idea/production/algorithm010/Week03/LC_17_Letter_combinations_of_phone.class b/.idea/production/algorithm010/Week03/LC_17_Letter_combinations_of_phone.class new file mode 100644 index 000000000..28f9a1dc2 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC_17_Letter_combinations_of_phone.class differ diff --git a/.idea/production/algorithm010/Week03/LC_50_powx_n.class b/.idea/production/algorithm010/Week03/LC_50_powx_n.class new file mode 100644 index 000000000..b3ad63236 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC_50_powx_n.class differ diff --git a/.idea/production/algorithm010/Week03/LC_77_combinations.class b/.idea/production/algorithm010/Week03/LC_77_combinations.class new file mode 100644 index 000000000..e671d1266 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC_77_combinations.class differ diff --git a/.idea/production/algorithm010/Week03/LC_78_subsets.class b/.idea/production/algorithm010/Week03/LC_78_subsets.class new file mode 100644 index 000000000..5dcbe1486 Binary files /dev/null and b/.idea/production/algorithm010/Week03/LC_78_subsets.class differ diff --git a/.idea/production/algorithm010/Week03/Lc_69_sqrtx.class b/.idea/production/algorithm010/Week03/Lc_69_sqrtx.class new file mode 100644 index 000000000..8cda4f70d Binary files /dev/null and b/.idea/production/algorithm010/Week03/Lc_69_sqrtx.class differ diff --git a/.idea/production/algorithm010/Week03/NOTE.md b/.idea/production/algorithm010/Week03/NOTE.md new file mode 100644 index 000000000..3227948df --- /dev/null +++ b/.idea/production/algorithm010/Week03/NOTE.md @@ -0,0 +1,116 @@ +#学习笔记 +---- +###Java 递归模板 +• 递归终结条件 +• 处理当前层逻辑 +• 下探到下一层 +• 清理当前层 +``` Java + public void recur(int level, int param) { + //terminator + if (level > MAX_LEVEL) { + //process result + return; + } + + //process current logic + process(level, param); + + //drill down + recur(level: level+1,newParam); + + //restore current status + + } +``` +###递归思维要点 +• 不要人肉递归(最大误区)
+• 找到最近最简方法,将其拆解成可以重复解决的问题(重复子问题)
+• 数学归纳法
+ +分治的递归模板 Didide & Conquer template: + + //1. terminator + //2. process (split your big problem) + //3. drill down (sub-problems) + //4. merge(sub-result) + //5. reverse states + + # Python + def divide_conquer(problem, param1, param2, ...): + # recursion terminator + if problem is None: + print_result + return + # prepare data + data = prepare_data(problem) + subproblems = split_problem(problem, data) + # conquer subproblems + subresult1 = self.divide_conquer(subproblems[0], p1, ...) + subresult2 = self.divide_conquer(subproblems[1], p1, ...) + subresult3 = self.divide_conquer(subproblems[2], p1, ...) + … + # process and generate the final result + result = process_result(subresult1, subresult2, subresult3, …) + + # revert the current level states + + +###回溯 + 每一层不断尝试,以获取可能的解 + 只需要思考 3 个问题: + 1、路径:也就是已经做出的选择。 + 2、选择列表:也就是你当前可以做的选择。 + 3、结束条件:也就是到达决策树底层,无法再做选择的条件。 + + 回溯模板: + + ``` + result = []; + public void backtrack(路径, 选择列表){ + if 满足结束条件: + result.add(路径); + return; + + for 选择 in 选择列表: + 做选择; + backtrack(路径, 选择列表); + 撤销选择; + } + ``` + 其核心就是 for 循环里面的递归,在递归调用之前「做选择」,在递归调用之后「撤销选择」 + + 列题:反转链表: + 这个BFS的讨论想和大家再重申一下 + 1. 初始化一个队列 + 2. 队列中先把第一个节点放进去,作为启动节点,不然后面没法while启动起来 + 3. 队列中出一个节点,处理,然后把它的所有子节点拿出来,依次放入队列中 + ### + 总结 + 1. 初始节点在循环外放进去 + 2. 处理节点逻辑和生成子节点逻辑放到循环内 + +我做题的时候,第 1 步都是先画图,画图是非常重要的,只有画图才能帮助我们想清楚递归结构,想清楚如何剪枝。就拿题目中的示例,想一想人手动操作是怎么做的,一般这样下来,这棵递归树都不难画出。 +即在画图的过程中思考清楚: + +1、分支如何产生; +2、题目需要的解在哪里?是在叶子结点、还是在非叶子结点、还是在从跟结点到叶子结点的路径? +3、哪些搜索是会产生不需要的解的?例如:产生重复是什么原因,如果在浅层就知道这个分支不能产生需要的结果,应该提前剪枝,剪枝的条件是什么,代码怎么写? +![](.NOTE_images/回朔算法.png) + +题目 提示 +47. 全排列 II 思考一下,为什么造成了重复,如何在搜索之前就判断这一支会产生重复,从而“剪枝”。 +17 .电话号码的字母组合 +22. 括号生成 这是字符串问题,没有显式回溯的过程。这道题广度优先遍历也很好写,可以通过这个问题理解一下为什么回溯算法都是深度优先遍历,并且都用递归来写。 +39. 组合总和 使用题目给的示例,画图分析。 +40. 组合总和 II +51. N皇后 其实就是全排列问题,注意设计清楚状态变量。 +60. 第k个排列 利用了剪枝的思想,减去了大量枝叶,直接来到需要的叶子结点。 +77. 组合 组合问题按顺序找,就不会重复。并且举一个中等规模的例子,找到如何剪枝,这道题思想不难,难在编码。 +78. 子集 为数不多的,解不在叶子结点上的回溯搜索问题。解法比较多,注意对比。 +90. 子集 II 剪枝技巧同 47 题、39 题、40 题。 +93. 复原IP地址 +784. 字母大小写全排列 + +作者:liweiwei1419 +链接:https://leetcode-cn.com/problems/permutations/solution/hui-su-suan-fa-python-dai-ma-java-dai-ma-by-liweiw/ \ No newline at end of file diff --git a/.idea/production/algorithm010/Week03/Solution.class b/.idea/production/algorithm010/Week03/Solution.class new file mode 100644 index 000000000..4ed7fae6f Binary files /dev/null and b/.idea/production/algorithm010/Week03/Solution.class differ diff --git a/.idea/production/algorithm010/Week03/Test.class b/.idea/production/algorithm010/Week03/Test.class new file mode 100644 index 000000000..1bb330bc8 Binary files /dev/null and b/.idea/production/algorithm010/Week03/Test.class differ diff --git a/.idea/production/algorithm010/Week04/.NOTE_images/bfs-dfs.png b/.idea/production/algorithm010/Week04/.NOTE_images/bfs-dfs.png new file mode 100644 index 000000000..ef4faed9a Binary files /dev/null and b/.idea/production/algorithm010/Week04/.NOTE_images/bfs-dfs.png differ diff --git a/.idea/production/algorithm010/Week04/Amazon_Interview.class b/.idea/production/algorithm010/Week04/Amazon_Interview.class new file mode 100644 index 000000000..3f816fc94 Binary files /dev/null and b/.idea/production/algorithm010/Week04/Amazon_Interview.class differ diff --git a/.idea/production/algorithm010/Week04/LC102_binary_tree_level_order_traversal.class b/.idea/production/algorithm010/Week04/LC102_binary_tree_level_order_traversal.class new file mode 100644 index 000000000..2802e3637 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC102_binary_tree_level_order_traversal.class differ diff --git a/.idea/production/algorithm010/Week04/LC122_best_time_to_buy_sell_stock_ii$Solution.class b/.idea/production/algorithm010/Week04/LC122_best_time_to_buy_sell_stock_ii$Solution.class new file mode 100644 index 000000000..5599791e9 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC122_best_time_to_buy_sell_stock_ii$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC122_best_time_to_buy_sell_stock_ii.class b/.idea/production/algorithm010/Week04/LC122_best_time_to_buy_sell_stock_ii.class new file mode 100644 index 000000000..76150d0c7 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC122_best_time_to_buy_sell_stock_ii.class differ diff --git a/.idea/production/algorithm010/Week04/LC126_word_ladder_ii$Solution.class b/.idea/production/algorithm010/Week04/LC126_word_ladder_ii$Solution.class new file mode 100644 index 000000000..2d12a3ed4 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC126_word_ladder_ii$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC126_word_ladder_ii.class b/.idea/production/algorithm010/Week04/LC126_word_ladder_ii.class new file mode 100644 index 000000000..8923a1a4b Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC126_word_ladder_ii.class differ diff --git a/.idea/production/algorithm010/Week04/LC127_word_ladder$Solution.class b/.idea/production/algorithm010/Week04/LC127_word_ladder$Solution.class new file mode 100644 index 000000000..ca6875cfe Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC127_word_ladder$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC127_word_ladder$Solution2.class b/.idea/production/algorithm010/Week04/LC127_word_ladder$Solution2.class new file mode 100644 index 000000000..9e2f4b957 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC127_word_ladder$Solution2.class differ diff --git a/.idea/production/algorithm010/Week04/LC127_word_ladder.class b/.idea/production/algorithm010/Week04/LC127_word_ladder.class new file mode 100644 index 000000000..8aba8532f Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC127_word_ladder.class differ diff --git a/.idea/production/algorithm010/Week04/LC153_find_minimum_in_rotated_sorted_array$Solution.class b/.idea/production/algorithm010/Week04/LC153_find_minimum_in_rotated_sorted_array$Solution.class new file mode 100644 index 000000000..2a508983d Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC153_find_minimum_in_rotated_sorted_array$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC153_find_minimum_in_rotated_sorted_array.class b/.idea/production/algorithm010/Week04/LC153_find_minimum_in_rotated_sorted_array.class new file mode 100644 index 000000000..d20fffcd9 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC153_find_minimum_in_rotated_sorted_array.class differ diff --git a/.idea/production/algorithm010/Week04/LC33_search_in_rotated_sorted_array.class b/.idea/production/algorithm010/Week04/LC33_search_in_rotated_sorted_array.class new file mode 100644 index 000000000..7fe1a7f25 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC33_search_in_rotated_sorted_array.class differ diff --git a/.idea/production/algorithm010/Week04/LC367_valid_perfect_square$Solution.class b/.idea/production/algorithm010/Week04/LC367_valid_perfect_square$Solution.class new file mode 100644 index 000000000..7fb306594 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC367_valid_perfect_square$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC367_valid_perfect_square.class b/.idea/production/algorithm010/Week04/LC367_valid_perfect_square.class new file mode 100644 index 000000000..881d331e6 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC367_valid_perfect_square.class differ diff --git a/.idea/production/algorithm010/Week04/LC455_assign_cookies.class b/.idea/production/algorithm010/Week04/LC455_assign_cookies.class new file mode 100644 index 000000000..f2752ac3a Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC455_assign_cookies.class differ diff --git a/.idea/production/algorithm010/Week04/LC45_jump_game_ii$Solution.class b/.idea/production/algorithm010/Week04/LC45_jump_game_ii$Solution.class new file mode 100644 index 000000000..343bfde4f Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC45_jump_game_ii$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC45_jump_game_ii.class b/.idea/production/algorithm010/Week04/LC45_jump_game_ii.class new file mode 100644 index 000000000..b02bbc426 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC45_jump_game_ii.class differ diff --git a/.idea/production/algorithm010/Week04/LC529_minesweeper$Solution.class b/.idea/production/algorithm010/Week04/LC529_minesweeper$Solution.class new file mode 100644 index 000000000..6db1c06d1 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC529_minesweeper$Solution.class differ diff --git a/.idea/production/algorithm010/Week04/LC529_minesweeper.class b/.idea/production/algorithm010/Week04/LC529_minesweeper.class new file mode 100644 index 000000000..d3eeef94a Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC529_minesweeper.class differ diff --git a/.idea/production/algorithm010/Week04/LC55_jump_game.class b/.idea/production/algorithm010/Week04/LC55_jump_game.class new file mode 100644 index 000000000..ef06fcc36 Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC55_jump_game.class differ diff --git a/.idea/production/algorithm010/Week04/LC69_sqrtx.class b/.idea/production/algorithm010/Week04/LC69_sqrtx.class new file mode 100644 index 000000000..d02480f6a Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC69_sqrtx.class differ diff --git a/.idea/production/algorithm010/Week04/LC74_search_2d_matrix.class b/.idea/production/algorithm010/Week04/LC74_search_2d_matrix.class new file mode 100644 index 000000000..4dd34228a Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC74_search_2d_matrix.class differ diff --git a/.idea/production/algorithm010/Week04/LC850_lemonade_change.class b/.idea/production/algorithm010/Week04/LC850_lemonade_change.class new file mode 100644 index 000000000..f63ce4fea Binary files /dev/null and b/.idea/production/algorithm010/Week04/LC850_lemonade_change.class differ diff --git a/.idea/production/algorithm010/Week04/NOTE.md b/.idea/production/algorithm010/Week04/NOTE.md new file mode 100644 index 000000000..db5f1b923 --- /dev/null +++ b/.idea/production/algorithm010/Week04/NOTE.md @@ -0,0 +1,108 @@ +学习笔记 +#贪心: +当下做局部最优判断 +贪心算法场景:问题能够分解成子问题来解决,子问题的最优解能递推到最终问题的最优解。这种问题 +#回朔: +能够回退 +#动态规划: +最优判断 + 回退 +动态规划会保存以前的运算结果,并根据以前的结果的结果对结果对当前进行选择,有回退功能。 + +#递归 +关注本层的逻辑,在递归前的代码是压栈的时候执行的,递归后代码是出栈时执行的; +如果压栈前改变全局变量或者参数直,就要考虑出栈时恢复,不然会影响其它子递归; +必要时画递归树帮助理解。不要尝试像电脑一样每层去调用求解. + +DFS 代码模板 +递归状态树的理解:dfs会马上进入到下一层,不用等到for循环,等dfs遇到return才会进入for循环下一个 +```$xslt +visited = set() +def dfs(node, visited): + if node in visited: # terminator + # already visited + return + visited.add(node) + # process current node here. + ... + for next_node in node.children(): + if next_node not in visited: + dfs(next_node, visited) +``` +Trivial 非递归写法: +``` +def DFS(self, tree): + if tree.root is None: + return [] + visited, stack = [], [tree.root] + while stack: + node = stack.pop() + visited.add(node) + process (node) + nodes = generate_related_nodes(node) + stack.push(nodes) + # other processing work + ... +``` + +## BFS 代码模板 +```$xslt +# Python +def BFS(graph, start, end): + visited = set() + queue = [] + queue.append([start]) + while queue: + node = queue.pop() + visited.add(node) + process(node) + nodes = generate_related_nodes(node) + queue.push(nodes) + # other processing work + ... + +``` +![](.NOTE_images/bfs-dfs.png) + +#二分查找 +二分查找的前提 +1. 目标函数单调性 (单调递增或递减) +2. 存在上下界 (bounded) +3. 能够通过索引访问 (index accessible) +4. 小心边界值 +```$xslt + //用二分查找 + //1.如果左<右,表示这部分是排序的,另一部分没有排序 + //2.在排序这部分找,如果target在左右值间,说明target在有序这边 + //3.否则在无序这边,据继续递归查找 + public int search(int[] nums, int target) { + int mid,left=0; + int right=nums.length-1; + while (left <= right){ + mid = left+(right-left)/2; + if (nums[mid]==target){ + return mid; + } + //有序在前半部分, + if (nums[left]<=nums[mid]){ + //必须考虑左右边界值等于target情况 + //这里因为right = mid-1;加入target=左边界值 + if (nums[left]<=target && target a[i-1][0] + nums[i] +5. DP要夹带更多的信息时,一般就是升维。 +6. 在此基础上考虑重新定义状态数组,降维 +7. 状态数组: a[i]: 0..i天,且nums[i]必偷的最大值, 能偷到max value : max(a) +8. DP方程 : a[i] = Max(a[i-1], a[i-2] + nums[i]); + +```$xslt + public int rob(int[] nums) { + if (nums == null || nums.length==0) return 0; + if (nums.length==1) return nums[0]; + + int n = nums.length; + int[] dp = new int[n]; + + dp[0] = nums[0]; + dp[1] = Math.max(nums[0],nums[1]); + + for (int i=2;i  2  5 +  / \         /   / \  + 2  3        1   3  6    (3 要换父节点) + / +1 + +左右旋 (左右子树) + 先下面的右子树左旋,变成了左左子树,再做整个子树右旋 +右左旋 (右左子树) + 先下面的左子树右旋,变成了右右子树,再做整个子树左旋 + +## Red-black Tree +红黑树是一种**近似平衡**的二叉树(Binary Search Tree), +它能够保证任何一个节点的左右子树**高度差小于两倍**。 + 1.每个节点要么是红或黑 + 2.根结点是黑 + 3.每个叶结点(NIL结点)是黑色的 + 4.不能有相邻接的两个红节点 + 5.任何一节点到其每个叶子的所有路径都包含相同数目的黑色结点 + + ## AVL Tree vs Red-black Tree + 1.AVL trees provide faster lookups than Red Black Trees bacause they are more strictly balanced. + + 2.Red Black Trees provide faster insertion and removal operations than AVL trees as fewer rotations are done due to relatively relaxed balancing. + + 3.AML trees store balance factors or heights with each node, thus requires storeage for an integer pernode whereas Red Black Tree requires only 1 bit of information per node. + + 4.Red Black Trees are used in most of the language libraries, like map, multimap, multisetin C++ whereas AVL trees are used in databases where faster retrievals are required. diff --git a/.idea/production/algorithm010/Week08/NOTE.md b/.idea/production/algorithm010/Week08/NOTE.md new file mode 100644 index 000000000..83d3cb7f6 --- /dev/null +++ b/.idea/production/algorithm010/Week08/NOTE.md @@ -0,0 +1,180 @@ +学习笔记 +# Sort + 1. Compare Sort + big O can not smale than O(nlogn). + n(logn): heap, mergo, quick + + 2. Non-compare Sort + can be O(n) + + https://www.cnblogs.com/onepixel/p/7674659.html + + +# 位运算 + +### and运算 & +相同位的两个数字都为1,则为1;若有一个不为1,则为0 +00101 +11100 +& +------ +00100 + +### or运算 | +相同位只要一个为1即为1。 +00101 +11100 +| +----- +11101 + +### 异或运算 ^ +相同位不同则为1,相同则为0。 +00101 +11100 +^ +----- +11001 + +### not运算 ~ +not运算的定义是把内存中的0和1全部取反。使用not运算时要格外小心,你需要注意整数类型有没有符号。如果not的对象是无符号整数(不能表示负数),那么得到的值就是它与该类型上界的差, + +### shl运算 << +a shl b就表示把a转为二进制后左移b位(在后面添b个0),a shl b的值实际上就是a乘以2的b次方 +### shr运算 >> +a shr b表示二进制右移b位(去掉末b位),相当于a除以2的b次方(取整)。 + +## 实战位运算要点: +1. 判断奇偶: + x%2==1 -> (x&1)==1 + x@2==0 -> (x&1)==0 +2. x>>1 -> x/2 + x=x/2; -> x=x>>1; + mid = (left+right)/2; -> mid=(left+right)>>1; +3. x=x&(x-1) 清零最低位的1 +4. x&-x => 得到最低位的1 +5. x&~x => 0 + +# 排序 +//https://www.cnblogs.com/onepixel/p/7674659.html + +## insertSort + 对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。 + + public int[] insertSort(int[] arr){ + int len = arr.length; + int preIdx, current; + for (int i =1;i=0 && arr[preIdx]>current){ + arr[preIdx+1] = arr[preIdx]; + preIdx--; + } + arr[preIdx+1] = current; + } + return arr; + } + +## shellSort +是简单插入排序的改进版。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序又叫缩小增量排序。 + + public int[] shellSort(int[] arr){ + int len = arr.length; + for (int gap = len/2; gap>0;gap=gap/2){ + for (int i = gap; i= 0 && current= right) return; + int mid = (left+right) >> 1; + mergeSort(arr, left, mid); + mergeSort(arr, mid+1, right); + merge(arr,left,mid,right); + } + + private void merge(int[] arr, int left, int mid, int right) { + int[] temp = new int[right-left+1]; + int i = left; + int j = mid+1; + int k = 0; + + while (i + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..d0d28e6a2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,112 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Debug (Launch) - Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Debug (Launch)-BinaryHeap", + "request": "launch", + "mainClass": "Week02.BinaryHeap", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC_239_sliding_window_maximum", + "request": "launch", + "mainClass": "Week02.LC_239_sliding_window_maximum", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC_347_top_k_frequent_elements", + "request": "launch", + "mainClass": "Week02.LC_347_top_k_frequent_elements", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-test", + "request": "launch", + "mainClass": "Week02.test", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC111_min_depth_b_tree", + "request": "launch", + "mainClass": "Week03.LC111_min_depth_b_tree", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC46_permutations", + "request": "launch", + "mainClass": "Week03.LC46_permutations", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC_17_Letter_combinations_of_phone", + "request": "launch", + "mainClass": "Week03.LC_17_Letter_combinations_of_phone", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-Lc_69_sqrtx", + "request": "launch", + "mainClass": "Week03.Lc_69_sqrtx", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-Test", + "request": "launch", + "mainClass": "Week03.Test", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC126_word_ladder_ii", + "request": "launch", + "mainClass": "Week04.LC126_word_ladder_ii", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC127_word_ladder", + "request": "launch", + "mainClass": "Week04.LC127_word_ladder", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC153_find_minimum_in_rotated_sorted_array", + "request": "launch", + "mainClass": "Week04.LC153_find_minimum_in_rotated_sorted_array", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-LC529_minesweeper", + "request": "launch", + "mainClass": "Week04.LC529_minesweeper", + "projectName": "algorithm010_c257f461" + }, + { + "type": "java", + "name": "Debug (Launch)-test(1)", + "request": "launch", + "mainClass": "Week04.test", + "projectName": "algorithm010_c257f461" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..2421e386c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.exclude": { + "**/.classpath": true, + "**/.project": true, + "**/.settings": true, + "**/.factorypath": true + } +} \ No newline at end of file diff --git a/AfterGraduation/LC1080_insufficient_nodes.java b/AfterGraduation/LC1080_insufficient_nodes.java new file mode 100644 index 000000000..fbb066997 --- /dev/null +++ b/AfterGraduation/LC1080_insufficient_nodes.java @@ -0,0 +1,72 @@ +package AfterGraduation; + +import Common.TreeNode; + +public class LC1080_insufficient_nodes { +/* +用root.val - limit 带入每层即可在sufficientSubset递归 + */ + public TreeNode sufficientSubset(TreeNode root, int limit) { + + if (root == null){ + return null; + } + //如果是叶子节点,判断是否需要删去 + if (root.left==null && root.right==null){ + if (root.val < limit) + return null; + else{ + return root; + } + } + TreeNode left = sufficientSubset(root.left,limit-root.val); + TreeNode right = sufficientSubset(root.right,limit-root.val); + + //如果两个子树都被删掉,说明这个节点是不足节点 + if(left == null && right == null) + return null; + + //没被全删点就说明不是不足节点,保留即可 + root.left = left; + root.right = right; + return root; + } + + /* +对于二叉树上某一节点的删除,一般有两种做法: +一种是由其父节点对左右子节点的删除,另一种则是将自身删除。从测试用例3 [5,-6,-6] 的返回结果为 [] 可知,在某些情况下根节点也需要被删除,由于根节点没有父结点, +因此选择第二种删除方式,为了避免像链表一样出现断链,采用后序遍历,从叶子节点开始删除。 + +解题思路:不妨设 bool dfs(X, sum, limit) 为判断 root 是否为不足节点的函数,其中 sum 为从根节点 root 到 X 的父结点的路径和,显然若出现以下三种情况,则应当删除: +1、若 X 的左孩子节点是不足节点,且右孩子也是不足节点,则 X 也是不足节点 +2、若 X 的左孩子节点是不足节点,且右孩子为空,则 X 也是不足节点 +3、若 X 的左孩子为空,且右孩子是不足节点,则 X 也是不足节点 +递归的 Base Case 为:若 X == NULL 则返回 sum < limit +以下解法错误 + private boolean dfs_wrong(TreeNode node, int sum, int limit){ + if (node == null){ + return sum < limit; + } + boolean left = dfs(node.left, sum+node.val,limit); + //Case 2: + if (left && node.right==null){ + node = null; + return true; + } + + boolean right = dfs(node.right, sum+node.val,limit); + //Case 3: + if (right && node.left == null){ + node = null; + return true; + } + //Case 1; + if (left && right){ + node = null; + return true; + } + return left && right; + } + + */ +} diff --git a/Common/BuildBinaryTree.java b/Common/BuildBinaryTree.java new file mode 100644 index 000000000..ef7e15af5 --- /dev/null +++ b/Common/BuildBinaryTree.java @@ -0,0 +1,81 @@ +package Common; + +import java.util.LinkedList; +import java.util.Queue; + +public class BuildBinaryTree { + + // PriorityQueue + // Deque -> Stack + + public static TreeNode buildBinaryTree(Integer[] list) { + + if(list == null || list.length == 0) return null; + Queue queue = new LinkedList<>(); + TreeNode root = new TreeNode(list[0]); + int j =1; + int length = list.length; + queue.add(root); + while(!queue.isEmpty() && j< length){ + + TreeNode curNode = queue.poll(); + if (list[j] != null){ + curNode.left = new TreeNode(list[j]); + queue.add(curNode.left); + } + j++; + if (j ==length){break;} + + if (list[j] != null) { + curNode.right = new TreeNode(list[j]); + queue.add(curNode.right); + } + j++; + } + return root; + } + + public TreeNode build(Integer[] list){ + if(list.length == 0) return null; + Queue queue = new LinkedList<>(); + + TreeNode root = new TreeNode(list[0]); + + queue.add(root); + int lineNum =2; + int startIndex =1; + int restLength = list.length-1; + + while(restLength >0){ + for (int index = startIndex;index < startIndex + lineNum; index=index+2){ + if (index == list.length){ + return root; + } + TreeNode curNode = queue.poll(); + if (list[index] != null) + { + if(curNode!=null) { + curNode.left = new TreeNode(list[index]); + queue.add(curNode.left); + } + } + + if (index+1 == list.length) + return root; + + if (list[index+1] != null) + { + if(curNode!=null){ + curNode.right = new TreeNode(list[index+1]); + queue.add(curNode.right); + } + } + } + startIndex +=lineNum; + restLength -=lineNum; + lineNum = queue.size() * 2; + } + return root; + } + +} diff --git a/Common/TreeNode.java b/Common/TreeNode.java new file mode 100644 index 000000000..28fe9f26c --- /dev/null +++ b/Common/TreeNode.java @@ -0,0 +1,14 @@ +package Common; + +public class TreeNode { + public int val; + public TreeNode left; + public TreeNode right; + public TreeNode() {} + public TreeNode(int val) { this.val = val; } + public TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} diff --git a/DesignPattern/DesignPattern-ResponseTimeMetrics/api-analysis/.idea/workspace.xml b/DesignPattern/DesignPattern-ResponseTimeMetrics/api-analysis/.idea/workspace.xml new file mode 100644 index 000000000..3d52f630b --- /dev/null +++ b/DesignPattern/DesignPattern-ResponseTimeMetrics/api-analysis/.idea/workspace.xml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + +