+
+
+
+
+
+
\ No newline at end of file
diff --git a/Week_01/id_1/LeetCode_101_1.java b/Week_01/id_1/LeetCode_101_1.java
new file mode 100644
index 00000000..e1528949
--- /dev/null
+++ b/Week_01/id_1/LeetCode_101_1.java
@@ -0,0 +1,61 @@
+package week01;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/10 11:47
+ * @描述 LeetCode : 101. 对称二叉树 https://leetcode-cn.com/problems/symmetric-tree/
+ */
+public class SymmetricTree101 {
+
+ public static void main(String[] args) {
+ TreeNode node = new TreeNode(1);
+ node.left=new TreeNode(2);
+ node.right=new TreeNode(2);
+ node.left.left=new TreeNode(3);
+ node.left.right=new TreeNode(4);
+ node.right.left=new TreeNode(4);
+ node.right.right=new TreeNode(4);
+ System.out.println(new SymmetricTree101().isSymmetric2(node));
+ }
+
+ /**
+ * Method 1 : 迭代 BFS —> 把每一层的节点都放在队列中,树的左子树 和 右子树 比较是否相等;
+ * 时间复杂度 : O(N) 树的节点 ; 空间复杂度 : O(N) 额外的空间队列;
+ *
+ */
+ public boolean isSymmetric(TreeNode root) {
+ Queue nodes = new LinkedList<>();
+ nodes.offer(root);
+ nodes.offer(root);
+ while (!nodes.isEmpty()){
+ TreeNode n1 = nodes.poll();
+ TreeNode n2 = nodes.poll();
+ if(n1 ==null && n2==null) continue;
+ if(n1==null || n2 ==null) return false;
+ if(n1.val!=n2.val) return false;
+ nodes.offer(n1.left);
+ nodes.offer(n2.right);
+ nodes.offer(n1.right);
+ nodes.offer(n2.left);
+ }
+ return true;
+ }
+
+ /**
+ * Method 2 : 递归 DFS —> 操作同一棵树两次,树的左子树 和 右子树 比较;
+ * 时间复杂度 : O(N) ; 空间复杂度 : O(N) ;
+ */
+ public boolean isSymmetric2(TreeNode root) {
+ return isMirrer(root,root);
+ }
+
+ private boolean isMirrer(TreeNode n1,TreeNode n2) {
+ if(n1==null&& n2==null) return true;
+ if(n1==null || n2==null) return false;
+ if(n1.val!=n2.val) return false;
+ return isMirrer(n1.left,n2.right)&& isMirrer(n1.right,n2.left);
+ }
+}
diff --git a/Week_01/id_1/LeetCode_1021_1.java b/Week_01/id_1/LeetCode_1021_1.java
new file mode 100644
index 00000000..d7869763
--- /dev/null
+++ b/Week_01/id_1/LeetCode_1021_1.java
@@ -0,0 +1,61 @@
+package week01;
+
+import java.util.Stack;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/6 18:00
+ * @描述 LeetCode : 1021. 删除最外层的括号 https://leetcode-cn.com/problems/remove-outermost-parentheses/
+ */
+public class RemoveOuterMostParenteses {
+
+ public static void main(String[] args) {
+ String str = "(()())(())";
+ String str2 = "(()())(())(()(()))";
+ String str3 = "()()";
+ System.out.println(new RemoveOuterMostParenteses().removeOuterParentheses2(str));
+ System.out.println(new RemoveOuterMostParenteses().removeOuterParentheses2(str2));
+ System.out.println(new RemoveOuterMostParenteses().removeOuterParentheses2(str3));
+ }
+
+ /**
+ * Method 1 : 使用 stack 来进行操作 。
+ * ( 先判空,非空 则加入字符串中,在push;
+ * ) 先pop,再判空,非空则加入 字符串中;
+ * 时间复杂度 : O(N) ; 空间复杂度 : O(N)
+ */
+ public String removeOuterParentheses(String S) {
+ final Stack stack = new Stack<>();
+ StringBuilder sb = new StringBuilder();
+ for (char c : S.toCharArray()) {
+ if (c == '(') {
+ if (!stack.empty()) sb.append(c);
+ stack.push(c);
+ } else {
+ stack.pop();
+ if (!stack.empty()) sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Method 2 : 用一个计数器 count 来保存; ( count++ ; ) count-- ;
+ * 时间复杂度 : O(N) ; 空间复杂度 : O(1)
+ */
+ public String removeOuterParentheses2(String S) {
+ StringBuilder sb = new StringBuilder();
+ int count = 0;
+ for (char c : S.toCharArray()) {
+ if (c == '(') {
+ count++;
+ if (count > 1) sb.append(c);
+ } else {
+ count--;
+ if (count != 0) sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+}
diff --git a/Week_01/id_1/LeetCode_1047_1.java b/Week_01/id_1/LeetCode_1047_1.java
new file mode 100644
index 00000000..9c520d0d
--- /dev/null
+++ b/Week_01/id_1/LeetCode_1047_1.java
@@ -0,0 +1,54 @@
+package week01;
+
+import java.util.Stack;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/6 12:41
+ * @描述 LeetCode : 1047. 删除字符串中的所有相邻重复项 https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
+ */
+public class RemoveAllAdjacent {
+ public static void main(String[] args) {
+ final RemoveAllAdjacent adjacent = new RemoveAllAdjacent();
+ System.out.println(adjacent.removeDuplicates2("abbaca"));
+ }
+
+ /**
+ * Method 1 : 使用堆栈 stack ;
+ * 时间复杂度 : O(N*K*K) ; 空间复杂度:O(N)
+ */
+ public String removeDuplicates(String S) {
+ final Stack stack = new Stack<>();
+ for (Character item : S.toCharArray()) {
+ if (!stack.empty() && stack.peek() == item) {
+ stack.pop();
+ } else {
+ stack.push(item);
+ }
+ }
+ final StringBuilder builder = new StringBuilder();
+ while (!stack.empty()) {
+ builder.append(stack.pop());
+ }
+ return builder.reverse().toString();
+ }
+
+ /**
+ * Method 2 : 数组使用
+ * 时间复杂度: O(S.length()) —> O(N) ; 空间复杂度: O(S.length) —> O(N)
+ */
+ public String removeDuplicates2(String S) {
+ final char[] chars = S.toCharArray();
+ int top = 0;
+ for (char c : chars) {
+ if (top > 0 && chars[top - 1] == c) {
+ top--;
+ continue;
+ }
+ chars[top++] = c;
+ }
+ return new String(chars, 0, top);
+ }
+
+
+}
diff --git a/Week_01/id_1/LeetCode_104_1.java b/Week_01/id_1/LeetCode_104_1.java
new file mode 100644
index 00000000..5569de43
--- /dev/null
+++ b/Week_01/id_1/LeetCode_104_1.java
@@ -0,0 +1,53 @@
+package week01;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/10 10:08
+ * @描述 LeetCode : 104. 二叉树的最大深度 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
+ */
+public class MaximumDepthBinaryTree104 {
+
+ public static void main(String[] args) {
+
+ }
+
+ /**
+ * Method 1: 递归 DFS —> 分别计算 左右节点的最大深度 ,比较 max ,然后加上 根节点 +1;
+ * 时间复杂度: O(N) ; 空间复杂度: O(N)
+ */
+ public int maxDepth(TreeNode root) {
+ if (root == null) {
+ return 0;
+ }
+ int left = maxDepth(root.left);
+ int right = maxDepth(root.right);
+ return Math.max(left, right) + 1;
+ }
+
+ /**
+ * Method 2 : 迭代 BFS 的思想 ——> 用队列来存储 树的节点
+ * 时间复杂度: O(N) 树的遍历 ; 空间复杂度: O(N) 额外的空间:队列 ;
+ */
+ public int maxDepth2(TreeNode root) {
+ if (root == null) return 0;
+ Queue queue = new LinkedList<>();
+ queue.offer(root);
+ int depth = 0;
+ int size = 1;
+ while (!queue.isEmpty()) {
+ TreeNode node = queue.poll();
+ if (node.left != null) queue.offer(node.left);
+ if (node.right != null) queue.offer(node.right);
+ size--;
+ if (size == 0) {
+ depth++;
+ size = queue.size();
+ }
+ }
+ return depth;
+ }
+
+}
diff --git a/Week_01/id_1/LeetCode_111_1.java b/Week_01/id_1/LeetCode_111_1.java
new file mode 100644
index 00000000..d3b19102
--- /dev/null
+++ b/Week_01/id_1/LeetCode_111_1.java
@@ -0,0 +1,58 @@
+package week01;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/10 10:13
+ * @描述 LeetCode : 111. 二叉树的最小深度 https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
+ */
+public class MinimumDepthBinaryTree111 {
+ public static void main(String[] args) {
+ final MinimumDepthBinaryTree111 tree111 = new MinimumDepthBinaryTree111();
+
+ }
+
+ /**
+ * Method 1: 递归 分别求左右节点的最小深度;
+ * 考虑情况 :
+ * 1: 左节点为空,只需要计算右节点的最小深度,即 right+1
+ * 2: 右节点为空,只需要计算左节点的最小深度,即 left+1
+ * 时间复杂度:O(N) ; 空间复杂度:O(N)
+ */
+ public int minDepth(TreeNode root) {
+ if (root == null) {
+ return 0;
+ }
+ final int left = minDepth(root.left);
+ final int right = minDepth(root.right);
+ if (left == 0 || right == 0) {
+ return left + right + 1;
+ }
+ return Math.min(left, right) + 1;
+ }
+
+ /**
+ * Method 2 : 迭代 BFS 思想 —> 把 树的节点加入到 队列当中进行出来,当节点没有左节点和右节点时,返回当前深度即为最小深度
+ */
+ public int minDepth2(TreeNode root) {
+ if (root == null) return 0;
+ Queue queue = new LinkedList<>();
+ queue.offer(root);
+ int depth = 1;
+ int size = 1;
+ while (!queue.isEmpty()) {
+ TreeNode node = queue.poll();
+ if (node.left != null) queue.offer(node.left);
+ if (node.right != null) queue.offer(node.right);
+ if (node.left == null && node.right == null) return depth;
+ size--;
+ if (size == 0) {
+ depth++;
+ size = queue.size();
+ }
+ }
+ return depth;
+ }
+}
diff --git a/Week_01/id_1/LeetCode_15_1.java b/Week_01/id_1/LeetCode_15_1.java
new file mode 100644
index 00000000..f5728115
--- /dev/null
+++ b/Week_01/id_1/LeetCode_15_1.java
@@ -0,0 +1,118 @@
+package week01;
+
+import java.util.*;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/5 16:34
+ * @描述 LeerCode : 15. 三数之和 ; https://leetcode-cn.com/problems/3sum/
+ */
+public class ThreeSum {
+ public static void main(String[] args) {
+ final ThreeSum threeSum = new ThreeSum();
+ int[] ints = {-1, 0, 1};
+ int[] int2 = {-1, 0, 1, 2, -1, -4};
+ int[] int3 = {0, 0, 0, 0};
+ int[] int4 = {-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0};
+ final List> lists = threeSum.threeSum(ints);
+ for (List list : lists) {
+ for (Integer integer : list) {
+ System.out.print(integer + ",");
+ }
+ System.out.println();
+ }
+ System.out.println("-----------------------");
+ final List> lists2 = threeSum.threeSum(int2);
+ for (List list : lists2) {
+ for (Integer integer : list) {
+ System.out.print(integer + ",");
+ }
+ System.out.println();
+ }
+ System.out.println("-----------------------");
+ final List> lists3 = threeSum.threeSum(int3);
+ for (List list : lists3) {
+ for (Integer integer : list) {
+ System.out.print(integer + ",");
+ }
+ System.out.println();
+ }
+ System.out.println("-----------------------");
+ final List> lists4 = threeSum.threeSum(int4);
+ for (List list : lists4) {
+ for (Integer integer : list) {
+ System.out.print(integer + ",");
+ }
+ System.out.println();
+ }
+ System.out.println("----------------33333-------");
+ final List> lists5 = threeSum.threeSum3(int4);
+ for (List list : lists5) {
+ for (Integer integer : list) {
+ System.out.print(integer + ",");
+ }
+ System.out.println();
+ }
+ }
+
+
+ /**
+ * Method 1: 暴力法; 3 层循环 ——> 超时了......
+ * 时间复杂度 : O(N*N*N+NlogN) ; 空间复杂度 : O(N)
+ */
+ public List> threeSum(int[] nums) {
+ Arrays.sort(nums);
+ List> lists = new LinkedList<>();
+ for (int i = 0; i < nums.length - 2 && nums[i] <= 0; i++) {
+ if (i == 0 || i > 0 && nums[i] != nums[i - 1]) {
+ for (int j = i + 1; j < nums.length - 1 && nums[i] + nums[j] <= 0; j++) {
+ if ( nums[j] != nums[j + 1]) {
+ for (int k = j + 1; k < nums.length; k++) {
+// if (k > 2 && nums[k] == nums[k - 1]) continue;
+ if (nums[i] + nums[j] + nums[k] == 0) {
+ List list = Arrays.asList(nums[i], nums[j], nums[k]);
+ lists.add(list);
+ }
+ }
+ }
+ }
+ }
+ }
+ return lists;
+ }
+
+ /**
+ * 寻找新的解决方法
+ * Method 3 : 排序后 ,再循环里面 ,从两边往中间 查找
+ */
+ public List> threeSum3(int[] nums) {
+ Arrays.sort(nums);
+ List> lists = new LinkedList<>();
+ for (int i = 0; i < nums.length - 2; i++) {
+ if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
+
+ int left = i + 1, right = nums.length - 1, needValue = 0 - nums[i];
+ while (left < right) {
+ if (needValue == (nums[left] + nums[right])) {
+ if (!lists.contains(Arrays.asList(nums[i], nums[left], nums[right]))) {
+ lists.add(Arrays.asList(nums[i], nums[left], nums[right]));
+ }
+ while (left < right && nums[right] == nums[right - 1]) right--;
+ while (left < right && nums[left] == nums[left + 1]) left++;
+ right--;
+ left++;
+ } else if (needValue < (nums[left] + nums[right])) {
+ right--;
+ } else {
+ left++;
+ }
+ }
+ }
+ }
+ return lists;
+ }
+
+}
+
+
+// 左右指针的向中间逼近问题
\ No newline at end of file
diff --git a/Week_01/id_1/LeetCode_189_1.java b/Week_01/id_1/LeetCode_189_1.java
new file mode 100644
index 00000000..5567ff6f
--- /dev/null
+++ b/Week_01/id_1/LeetCode_189_1.java
@@ -0,0 +1,69 @@
+package week01;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/4 9:41
+ * @描述 LeetCode:189. 旋转数组 https://leetcode-cn.com/problems/rotate-array/
+ */
+public class RotateArray {
+ public static void main(String[] args) {
+ final RotateArray rotateArray = new RotateArray();
+ int[] nums = new int[]{-1, -100, 3, 99};
+ rotateArray.rotate(nums, 2);
+ for (int num : nums) {
+ System.out.print(num + ",");
+ }
+ }
+
+ /**
+ * Function one :
+ * k,两层循环, 第一层 数组 array; 第二层 k,要移动的位置
+ * array[i] = array[length -1 ]
+ * 时间复杂度 : O(k*n)
+ * 空间复杂度 : O(1)
+ */
+ public static void rotate1(int[] nums, int k) {
+ if (nums.length == 0 || k == 0) {
+ return;
+ }
+ int n = nums.length;
+ k %= n;
+ for (int j = 1; j <= k; j++) {
+ int tempLast = nums[n - 1];
+ for (int i = n - 1; i > 0; i--) {
+ nums[i] = nums[i - 1];
+ }
+ nums[0] = tempLast;
+ }
+ }
+
+ /**
+ * Function two:
+ * 反转的思路:
+ * 第一次: 全部反转 一遍
+ * 接下来以 k 为节点 进行区分 reverse(nums,0,n-1)
+ * 第二次: k 之前的 数据反转一遍 reverse(nums,0,k-1)
+ * 第三次: k 之后的 数据也要反转一遍 reverse(nums,k,n-1)
+ *
+ * @param nums
+ * @param k
+ */
+ public void rotate(int[] nums, int k) {
+ int n=nums.length;
+ k %= n;
+ reserve(nums,0,n-1);
+ reserve(nums,0,k-1);
+ reserve(nums,k,n-1);
+ }
+
+ private void reserve(int[] nums, int start, int end) {
+ while (start < end) {
+ int temp = nums[start];
+ nums[start] = nums[end];
+ nums[end] = temp;
+ start++;
+ end--;
+ }
+ }
+
+}
diff --git a/Week_01/id_1/LeetCode_1_1.java b/Week_01/id_1/LeetCode_1_1.java
new file mode 100644
index 00000000..97b43b57
--- /dev/null
+++ b/Week_01/id_1/LeetCode_1_1.java
@@ -0,0 +1,55 @@
+package week01;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/6 10:20
+ * @描述 LeetCode: 1. 两数之和 https://leetcode-cn.com/problems/two-sum/
+ */
+public class TwoNum {
+ public static void main(String[] args) {
+ final TwoNum twoNum = new TwoNum();
+ int[] nums={3, 2, 4};
+ final int[] ints = twoNum.twoSum2(nums, 6);
+ for (int anInt : ints) {
+ System.out.print(anInt+",");
+ }
+ }
+
+ /**
+ * Method 1 : 暴力法, 两层循环解决
+ * 时间复杂度: O(N*N) ; 空间复杂度: O(N)
+ *
+ */
+ public int[] twoSum(int[] nums, int target) {
+ for(int i=0;i map = new HashMap<>();
+ for(int i=0;i map = new HashMap<>();
+ for (char c : s.toCharArray()) {
+ map.put(c,map.getOrDefault(c,0)+1);
+ }
+ for (char c : t.toCharArray()) {
+ final Integer integer = map.get(c);
+ if(integer==null){
+ return false;
+ }else if(integer>1){
+ map.put(c,integer-1);
+ }else{
+ map.remove(c);
+ }
+ }
+ return map.isEmpty();
+ }
+
+ /**
+ * Method 4: 用 26 个字母的 数组来 进行操作; 第一个数组 出现的字母的数组就++ ; 第一个数组 出现的字母的数组就 -- ;
+ * 时间复杂度 : O(n) 空间复杂度: O(1)
+ * @param s
+ * @param t
+ * @return
+ */
+ public boolean isAnagram4(String s, String t) {
+ if(s.length() != t.length()){
+ return false;
+ }
+ final char[] charsOne = s.toCharArray();
+ final char[] charsTwo = t.toCharArray();
+ int[] array = new int[26];
+ for(int i=0;i head
+ * 2、三步完成转化过程:
+ * 1、初始节点 next 指向 第二个节点 ; root.next = root.next.next
+ * 2、原 之前的第一个节点的 next 指向 第二个节点的下一个 :first.next = second
+ * 3、第二个节点的 next 指向第一个 second.next = first;
+ * 4、 交换完成后; 当前节点 往后面 走 两个 next;
+ * 走一步 next: curr = second;
+ * 走两步 next: curr = first (curr = second.next)
+ * @param head
+ * @return
+ */
+ public ListNode swapPairs(ListNode head) {
+ ListNode root = new ListNode(0);
+ root.next = head;
+ ListNode curr = root;
+ while (curr.next != null && curr.next.next != null) {
+ ListNode first = curr.next;
+ ListNode second = curr.next.next;
+ curr.next = second;
+ first.next = second.next;
+ second.next = first;
+ curr = first;
+ }
+ return root.next;
+ }
+
+ public ListNode swapPairs2(ListNode head) {
+ if (head == null || head.next == null) {
+ return head;
+ }
+ ListNode next = head.next;
+ head.next = swapPairs2(head.next.next);
+ next.next = head;
+ return next;
+ }
+}
diff --git a/Week_01/id_1/LeetCode_257_1.java b/Week_01/id_1/LeetCode_257_1.java
new file mode 100644
index 00000000..b41779e3
--- /dev/null
+++ b/Week_01/id_1/LeetCode_257_1.java
@@ -0,0 +1,41 @@
+package week01;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/10 15:37
+ * @描述 LeetCode : 257. 二叉树的所有路径 https://leetcode-cn.com/problems/binary-tree-paths/
+ */
+public class BinaryTreePaths257 {
+ public static void main(String[] args) {
+ TreeNode node = new TreeNode(4);
+ node.left = new TreeNode(2);
+ node.right = new TreeNode(6);
+ node.left.left = new TreeNode(1);
+ node.left.right = new TreeNode(3);
+ System.out.println(new BinaryTreePaths257().binaryTreePaths(node));
+ }
+
+ /**
+ * Method 1 : DFS 先左子树 ,再右子树
+ * 时间复杂度 : O(N) 树的所有节点全部遍历一遍 ; 空间复杂度 : O(N) 额外的开销 字符串
+ */
+ public List binaryTreePaths(TreeNode root) {
+ List list = new ArrayList<>();
+ dfs(root, list, "");
+ return list;
+ }
+
+ private void dfs(TreeNode root, List list, String str) {
+ if (root == null) return;
+ if (root.left == null && root.right == null) {
+ list.add(str += root.val);
+ return;
+ } else {
+ dfs(root.left, list, str + root.val + "->");
+ dfs(root.right, list, str + root.val + "->");
+ }
+ }
+}
diff --git a/Week_01/id_1/LeetCode_26_1.java b/Week_01/id_1/LeetCode_26_1.java
new file mode 100644
index 00000000..15418e64
--- /dev/null
+++ b/Week_01/id_1/LeetCode_26_1.java
@@ -0,0 +1,57 @@
+package week01;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/3 18:00
+ * @描述
+ */
+public class ArrayMain {
+ public static void main(String[] args) {
+ System.out.println(removeDuplicates(new int[]{0,0,1,1,1,2,2,3,3,4}));
+ }
+
+ /***
+ * 26. 删除排序数组中的重复项 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
+ * @param nums
+ * @return
+ * 要求 O(1) 的额外空间实现
+ * 思路:
+ * 因为是排序好的数组,临时变量 count=0, temp=nums[0],后一个就跟temp 比较是否相等,不相等 则 count++,temp=nums[i];
+ *
+ *结论: 用两个临时变量,一个用来计数,一个用来保存上一个不相等的值;
+ */
+ public static int removeDuplicates(int[] nums) {
+ if(nums == null) return 0;
+ int count=0;
+ int temp=nums[0];
+ for(int i=1;i0){
+ n-=count;
+ if(n>count){
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
+ *
+ * Method 2 : 纯数学题解法 ; 推导递推公式
+ */
+ public int arrangeCoins2(int n){
+ return (int)(Math.sqrt(2*(long)n+0.25)-0.5);
+ }
+ /**
+ *
+ * Method 3 : 纯数学题解法 ; 演变形式 ——> 推导递推公式
+ */
+
+ public int arrangeCoins3(int n){
+ return (int)((Math.sqrt(8*n+1)-1)/2);
+ }
+}
diff --git a/Week_01/id_1/LeetCode_49_1.java b/Week_01/id_1/LeetCode_49_1.java
new file mode 100644
index 00000000..9414b7f7
--- /dev/null
+++ b/Week_01/id_1/LeetCode_49_1.java
@@ -0,0 +1,100 @@
+package week01;
+
+
+import java.util.*;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/5 14:18
+ * @描述 LeetCode: 49. 字母异位词分组 https://leetcode-cn.com/problems/group-anagrams/
+ */
+public class GroupAnagrams {
+ public static void main(String[] args) {
+ GroupAnagrams anagrams = new GroupAnagrams();
+ String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
+ final List> lists = anagrams.groupAnagram3(strs);
+ for (List list : lists) {
+ for (String s : list) {
+ System.out.print(s + ",");
+ }
+ System.out.println();
+ }
+ }
+
+ /**
+ * Method 1: 第一想法 对 数组当中的每一个元素的字母进行排序;然后使用map 来进行排序相同进行分组
+ * 时间复杂度: O(NKlogK) ; 空间复杂度: O(NK)
+ *
+ * @param strs
+ * @return
+ */
+ public List> groupAnagrams(String[] strs) {
+ if (strs.length == 0) {
+ return null;
+ }
+ List> lists = new ArrayList<>();
+ Map> map = new HashMap<>();
+ for (int i = 0; i < strs.length; i++) {
+ final char[] chars = strs[i].toCharArray();
+ Arrays.sort(chars);
+ String temp = String.valueOf(chars);
+ List list = new ArrayList<>();
+ if (map.get(temp) != null) {
+ list = map.get(temp);
+ }
+ list.add(strs[i]);
+ map.put(temp, list);
+ }
+ for (List list : map.values()) {
+ lists.add(list);
+ }
+ return lists;
+ }
+
+ /**
+ * Method 2 : 在方法一的基础上面 进行简化;
+ * 直接在数据循环时处理 字母异位词
+ * 时间复杂度: O(N*KlonK) ; 空间复杂度: O(K)
+ */
+ public List> groupAnagrams2(String[] strs) {
+ if (strs.length == 0) {
+ return new ArrayList>();
+ }
+ Map> map = new HashMap<>();
+ for (int i = 0; i < strs.length; i++) {
+ char[] chars = strs[i].toCharArray();
+ Arrays.sort(chars);
+ String temp = String.valueOf(chars);
+ if (map.get(temp) == null) { map.put(temp, new ArrayList());}
+ map.get(temp).add(strs[i]);
+ }
+ return new ArrayList<>(map.values());
+
+ }
+
+ /**
+ * Method 3: 不使用字符串 转数组 排序; 用 26 个字母的数组 来存储 每个字母
+ *
+ * 时间复杂度: O(N*K) ; 空间复杂度:O(K)
+ *
+ */
+ public List> groupAnagram3(String[] strs) {
+ if(strs.length==0){
+ return new ArrayList>();
+ }
+ Map> map = new HashMap<>();
+ for (String str : strs) {
+ int[] ints = new int[26];
+ char[] chars = str.toCharArray();
+ for(int i=0;i mapOrDefault = map.getOrDefault(temp, new ArrayList<>());
+ mapOrDefault.add(str);
+ map.put(temp,mapOrDefault);
+ }
+ return new ArrayList<>(map.values());
+ }
+
+}
diff --git a/Week_01/id_1/LeetCode_50_1.java b/Week_01/id_1/LeetCode_50_1.java
new file mode 100644
index 00000000..17e824cb
--- /dev/null
+++ b/Week_01/id_1/LeetCode_50_1.java
@@ -0,0 +1,87 @@
+package week01;
+
+/**
+ * LeetCode : 50. Pow(x, n) https://leetcode-cn.com/problems/powx-n/
+ */
+public class PowXN {
+ public static void main(String[] args) {
+ int i=1;
+ System.out.println(i/2);
+ System.out.println(new PowXN().myPow(2, 10));
+ System.out.println(new PowXN().myPow(2.1, 3));
+ System.out.println(new PowXN().myPow(2.0, -2));
+ System.out.println("-------------");
+ System.out.println(new PowXN().myPow3(2, 5));
+ System.out.println(new PowXN().myPow3(2, 10));
+ System.out.println(new PowXN().myPow3(2.1, 3));
+ System.out.println(new PowXN().myPow3(2.0, -2));
+ }
+
+ /**
+ * Method 1 : 暴力破解法
+ * 时间复杂度: O(N) ; 空间复杂度: O(1)
+ */
+ public double myPow(double x, int n) {
+ long newN = n;
+ if (newN < 0) {
+ x = 1 / x;
+ newN = -newN;
+ }
+ double sum = 1;
+ for (int i = 0; i < newN; i++) {
+ sum *= x;
+ }
+ return sum;
+ }
+
+ /**
+ * Method 2 : 递归 二分法
+ * 时间复杂度: O(logN) ; 空间复杂度: O(logN)
+ */
+ public double myPow2(double x, int n) {
+ long newN = n;
+ if (newN < 0) {
+ x = 1 / x;
+ newN = -newN;
+ }
+ return fastPow(x, newN);
+
+ }
+
+ private double fastPow(double x, long newN) {
+ // 终止条件
+ if (newN == 0) {
+ return 1.0;
+ }
+ // 要做的事情
+ double fastPow = fastPow(x, newN / 2);
+ if (newN % 2 == 0) {
+ return fastPow * fastPow;
+ }
+ return fastPow * fastPow * x;
+ // 下一层
+ }
+
+ /**
+ * Method 3 : 偶数时 进行 幂乘;奇数 之前的结果集 * 当前自己;
+ * 时间复杂度: O(logN) ; 空间复杂度: O(1) ;
+ */
+ public double myPow3(double x, int n) {
+ long N = n;
+ if (N < 0) {
+ x = 1 / x;
+ N = -N;
+ }
+ double result=1;
+ double cur=x;
+ for(long i=N;i>0;i/=2){
+ if(i%2==1){
+ result*=cur;
+ }
+ cur*=cur;
+ }
+ return result;
+ }
+
+
+}
diff --git a/Week_01/id_1/LeetCode_783_1.java b/Week_01/id_1/LeetCode_783_1.java
new file mode 100644
index 00000000..022b26d2
--- /dev/null
+++ b/Week_01/id_1/LeetCode_783_1.java
@@ -0,0 +1,43 @@
+package week01;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/10 14:38
+ * @描述 LeetCode : 783. 二叉搜索树结点最小距离 https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/
+ */
+public class MinimumdistanceBetweenBstNodes783 {
+
+ int min = Integer.MAX_VALUE;
+ TreeNode pre = null;
+
+ public static void main(String[] args) {
+ TreeNode node = new TreeNode(4);
+ node.left = new TreeNode(2);
+ node.right = new TreeNode(6);
+ node.left.left = new TreeNode(1);
+ node.left.right = new TreeNode(3);
+ System.out.println(new MinimumdistanceBetweenBstNodes783().minDiffInBST(node));
+ }
+
+ /**
+ *
+ * Method 1 : 递归 中序遍历的方法; 先计算左边的相邻节点的最小差,在计算右边相邻节点的最小差
+ * 时间复杂度 : O(N) ; 空间复杂度: O(1) ;
+ */
+ public int minDiffInBST(TreeNode root) {
+ dfs(root);
+ return min;
+ }
+
+ private void dfs(TreeNode root) {
+ // 终止条件
+ if (root == null) return ;
+ //要做的事情 // 先求 左边的两节点的最小差
+ dfs(root.left);
+ if (pre != null) min = Math.min(root.val - pre.val, min);
+ pre = root;
+ //要做的事情 // 先求 右边的两节点的最小差
+ dfs(root.right);
+ }
+
+}
diff --git a/Week_01/id_1/LeetCode_88_1.java b/Week_01/id_1/LeetCode_88_1.java
new file mode 100644
index 00000000..e593773b
--- /dev/null
+++ b/Week_01/id_1/LeetCode_88_1.java
@@ -0,0 +1,75 @@
+package week01;
+
+import java.util.Arrays;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/4 14:55
+ * @描述 LeetCode : 88. 合并两个有序数组 https://leetcode-cn.com/problems/merge-sorted-array/
+ */
+public class MergeSortedArray {
+ public static void main(String[] args) {
+ MergeSortedArray array = new MergeSortedArray();
+ int[] a1 = new int[]{ 2, 3,4, 0, 0, 0};
+ int[] a2 = new int[]{3, 5, 6};
+ int m = 3;
+ int n = 3;
+ array.merge3(a1, m, a2, n);
+ for (int i : a1) {
+ System.out.print(i + ",");
+ }
+ }
+
+ // 方法1: 第二个数组 插入 第一个数组; 再对第一个数组进行排序
+ public void merge1(int[] nums1, int m, int[] nums2, int n) {
+ if (nums2.length == 0 || nums1.length == 0) {
+ return;
+ }
+ for (int i = 0; i < nums2.length; i++) {
+ nums1[m + i] = nums2[i];
+ }
+ Arrays.sort(nums1);
+ for (int i : nums1) {
+ System.out.print(i + ",");
+ }
+ }
+
+ // 方法2: 从前往后 进行合并
+ public void merge2(int[] nums1, int m, int[] nums2, int n) {
+ if (nums2.length == 0) {
+ return;
+ }
+ int[] nums1Copy = new int[m];
+ System.arraycopy(nums1, 0, nums1Copy, 0, m);
+ int p1 = 0;
+ int p2 = 0;
+ int newP = 0;
+ while (p1 < m && p2 < n) {
+ nums1[newP++] = (nums1Copy[p1] < nums2[p2] ? nums1Copy[p1++] : nums2[p2++]);
+ }
+ if (p1 < m) {
+ System.arraycopy(nums1Copy, p1, nums1, p1 + p2, m - p1);
+ }
+ if (p2 < n) {
+ System.arraycopy(nums2, p2, nums1, p1 + p2, n - p2);
+ }
+ }
+
+ // 方法3 : 从后往前 进行合并
+ public void merge3(int[] nums1, int m, int[] nums2, int n) {
+ if (nums2.length == 0) {
+ return;
+ }
+ int p1 = m - 1;
+ int p2 = n - 1;
+ int newP = m + n - 1;
+ while (p1 >= 0 && p2 >= 0) {
+ nums1[newP--] = (nums1[p1] > nums2[p2] ? nums1[p1--] :nums2[p2--]);
+ }
+ if(p2>=0){
+ System.arraycopy(nums2,0,nums1,0,p2+1);
+ }
+ }
+
+
+}
diff --git a/Week_01/id_1/LeetCode_938_1.java b/Week_01/id_1/LeetCode_938_1.java
new file mode 100644
index 00000000..107612b0
--- /dev/null
+++ b/Week_01/id_1/LeetCode_938_1.java
@@ -0,0 +1,41 @@
+package week01;
+
+/**
+ * @创建人 luoxiang
+ * @创建时间 2019/6/10 16:38
+ * @描述 LeetCode : 938. 二叉搜索树的范围和 https://leetcode-cn.com/problems/range-sum-of-bst/
+ */
+public class RangeSumBst938 {
+ public static void main(String[] args) {
+ TreeNode node = new TreeNode(10);
+ node.left = new TreeNode(5);
+ node.right = new TreeNode(15);
+ node.left.left = new TreeNode(3);
+ node.left.right = new TreeNode(7);
+ node.right.right = new TreeNode(18);
+ System.out.println(new RangeSumBst938().rangeSumBST(node,7,15));
+ }
+
+ /**
+ * Method 1 : DFS 递归中直接操作数据
+ * 时间复杂度 : O(N) ; 空间复杂度 : O(N) ;
+ */
+ public int rangeSumBST(TreeNode root, int L, int R) {
+ return dfs(root,L,R);
+ }
+
+ private int dfs(TreeNode root, int l, int r) {
+ if(root==null){
+ return 0;
+ }
+ if(root.valr){
+ return dfs(root.left,l,r);
+ }
+ return root.val+dfs(root.left,l,r)+dfs(root.right,l,r);
+ }
+
+
+}
diff --git a/Week_01/id_1/NOTE.md b/Week_01/id_1/NOTE.md
index 107ea7d6..0a721e9a 100644
--- a/Week_01/id_1/NOTE.md
+++ b/Week_01/id_1/NOTE.md
@@ -1 +1,26 @@
# 学习笔记
+
+## DAY 1:
+1.LeetCode 26 : I'm confused what is In situ sorting? Even if I looked up the answer in baike.
+
+2.LeetCode 189 : The first idea is use two layers of circulation, the variable of first layer of circulation is numbers of times K, next ,the variable is length of array; Time complexity is O(k*n),space complexity is O(1);To be exact, time complexity is O(n* k%=n) ;
+ Code review by refer to others , three of inversion ,the code is very clean , what's more the time complexity is getting shorter,O(n),exactly , O(2n).
+
+3.LeetCode 21: I can understand the way of solving problem, but don't understand why use a temporiry variable to next pointer?
+## DAY 2:
+4.LeetCode 88 : At first, i can think of two ways to do it.
+ One: Add the second array directly to the first array, and then sort it; (time complexity is O(n+nlogn) , space complexity is O(1) )
+ Two: Before and after thoughts, compare the size of the preceding element int two arrays, the smaller element plus(+1)
+ Three: It's the opposite of the last one. After and before thoughts. Operate directly within large array.
+
+5.LeetCode 24: It's a little hard.Especially, recursive solution.
+
+6.LeetCode 242: Four solutions came to mind. Simple but fun.
+
+7.LeetCode 49: First thought use Map to solve this problem. Three answers came up.
+
+## DAY 3 :
+8. LeetCode 1:
+9. LeetCode 15:
+10. LeetCode 1021:
+11. LeetCode 1047:
diff --git "a/Week_01/id_1/\345\255\246\344\271\240\346\200\273\347\273\223.md" "b/Week_01/id_1/\345\255\246\344\271\240\346\200\273\347\273\223.md"
new file mode 100644
index 00000000..bb6def69
--- /dev/null
+++ "b/Week_01/id_1/\345\255\246\344\271\240\346\200\273\347\273\223.md"
@@ -0,0 +1,25 @@
+一周自己的刷题,挺有意思,开始做点觉得有意义的事情。
+虽然刷题有时候比较枯燥,但当自己仔细思考题目与解法的时候,有在努力的做这一件事情,一开始都是脑海里闪出的第一想法,比如说暴力法。
+有第一种方法出现的时候,再思考第二种与第三种。
+每次提交代码时,都会非常的注意到执行完成的时间与使用的内存,再看看自己战胜了多少人。
+我给自己自己定的目标是,超过 80% 的提交者,这样的话我觉得这个代码算是比较好的。
+如果是只有超过 30% 及以下的提交者,那我会觉得这个代码会很low,往往是暴力法来解决的问题,会尝试着寻找新的满意的答案。
+当发现自己有时焦头烂额想不到好的办法的时候,阅读一下别人写的好的代码,那样的感觉 so cool.
+代码简洁而优雅,像是欣赏一位美丽优雅的女子一样,自己表现地专注而有神。
+
+在学习数据结构与算法的过程当中,我觉得最重要的事情是 开拓了自己的思维。
+以前自己想问题都是从前往后的线性结构,通过学习算法之后,学到了另外三种思维的方式。
+1、从后往前;2、从中间向两端延伸;3、从两端向中间夹。
+
+1. 多种办法;
+2.
+3. 边界条件 、 时间空间复杂度(数据库、index、redis)
+
+# 五毒神掌
+
+非常好的总结;另外记得多看leetcode中文上的题解和国际版上的discuss专区
+
+"当发现自己有时焦头烂额想不到好的办法的时候,阅读一下别人写的好的代码,那样的感觉 so cool. 代码简洁而优雅,像是欣赏一位美丽优雅的女子一样,自己表现地专注而有神。"
+
+希望能坚持这样的训练法,继续保持! Keep it up!
+
diff --git "a/Week_01/id_1/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\347\220\206\350\247\243.pdf" "b/Week_01/id_1/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\347\220\206\350\247\243.pdf"
new file mode 100644
index 00000000..485bd902
Binary files /dev/null and "b/Week_01/id_1/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\347\220\206\350\247\243.pdf" differ
diff --git a/Week_01/id_10/Leecode-101-10 .py b/Week_01/id_10/Leecode-101-10 .py
new file mode 100644
index 00000000..50def597
--- /dev/null
+++ b/Week_01/id_10/Leecode-101-10 .py
@@ -0,0 +1,21 @@
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, x):
+# self.val = x
+# self.left = None
+# self.right = None
+
+class Solution:
+ def isSymmetric(self, root: TreeNode) -> bool:
+ if not root:
+ return True
+ return self._dfs(root.left, root.right)
+
+ def _dfs(self, node1, node2):
+ if not node1 and node2 or not node2 and node1:
+ return False
+ if node1 and node2 and node1.val != node2.val:
+ return False
+ if not node1 and not node2:
+ return True
+ return self._dfs(node1.left, node2.right) and self._dfs(node2.left, node1.right)
diff --git a/Week_01/id_10/Leecode-1021-10.py b/Week_01/id_10/Leecode-1021-10.py
new file mode 100644
index 00000000..a98c1f9b
--- /dev/null
+++ b/Week_01/id_10/Leecode-1021-10.py
@@ -0,0 +1,14 @@
+class Solution:
+ def removeOuterParentheses(self, S: str) -> str:
+ result=""
+ index=0
+ for c in S:
+ if c=='(':
+ if index!=0:
+ result+=c
+ index+=1
+ else:
+ index-=1
+ if index!=0:
+ result+=c
+ return result
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-174-10.py b/Week_01/id_10/Leecode-174-10.py
new file mode 100644
index 00000000..681c1edf
--- /dev/null
+++ b/Week_01/id_10/Leecode-174-10.py
@@ -0,0 +1,51 @@
+class Solution:
+ # 第一想法 二分法 没有思路
+ # 第二想法 dp 从第一个位置递推到最后,没写出来
+ # 第三想法 暴力法 dfs 遍历所有
+ # 时间复杂度 O(2^N) 空间复杂度 O(1) N为格子总数
+ # 提交超时
+ def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
+ if len(dungeon) == 0:
+ return 0
+ if len(dungeon[0]) == 0:
+ return 0
+ self.dungeon = dungeon
+ self.minblood = -1
+ self._dfs(0, 0, 0, dungeon[0][0])
+ return self.minblood + 1
+
+ def _dfs(self, i, j, minblood, routeblood):
+ m = len(self.dungeon)
+ n = len(self.dungeon[0])
+ if minblood < 0 - routeblood:
+ minblood = 0 - routeblood
+ if i == m - 1 and j == n - 1:
+ if self.minblood == -1 or self.minblood > minblood:
+ self.minblood = minblood
+ if self.minblood > -1 and self.minblood < minblood:
+ return
+ if i < m - 1:
+ self._dfs(i + 1, j, minblood, routeblood + self.dungeon[i + 1][j])
+ if j < n - 1:
+ self._dfs(i, j + 1, minblood, routeblood + self.dungeon[i][j + 1])
+
+ # 看了解法和评论区 使用逆序dp
+ # 从最后一个位置向前推,的确没想到
+ # 时间复杂度 O(N) 空间复杂度 O(N) N为格子总数
+ def calculateMinimumHP2(self, dungeon: List[List[int]]) -> int:
+ if len(dungeon) == 0:
+ return 0
+ if len(dungeon[0]) == 0:
+ return 0
+ m = len(dungeon)
+ n = len(dungeon[0])
+ dp = [[0 for y in range(n)] for x in range(m)]
+ dp[m - 1][n - 1] = max(1, 1 - dungeon[m - 1][n - 1])
+ for i in range(m - 2, -1, -1):
+ dp[i][n - 1] = max(dp[i + 1][n - 1] - dungeon[i][n - 1], 1)
+ for i in range(n - 2, -1, -1):
+ dp[m - 1][i] = max(dp[m - 1][i + 1] - dungeon[m - 1][i], 1)
+ for i in range(m - 2, -1, -1):
+ for j in range(n - 2, -1, -1):
+ dp[i][j] = min(max(1, dp[i + 1][j] - dungeon[i][j]), max(1, dp[i][j + 1] - dungeon[i][j]))
+ return max(dp[0][0], 1)
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-189-10.py b/Week_01/id_10/Leecode-189-10.py
new file mode 100644
index 00000000..18970d48
--- /dev/null
+++ b/Week_01/id_10/Leecode-189-10.py
@@ -0,0 +1,63 @@
+class Solution:
+ def rotate(self, nums: List[int], k: int) -> None:
+ """
+ Do not return anything, modify nums in-place instead.
+ """
+ if len(nums) == 0 or len(nums) == 1:
+ return
+ l = len(nums)
+ if k > l:
+ k = k % l
+
+ endindex = len(nums)
+ startindex = k
+ while k > 0:
+ while startindex + k < endindex:
+ for i in range(k):
+ tmp = nums[startindex + i]
+ nums[startindex + i] = nums[i]
+ nums[i] = tmp
+ startindex = startindex + k
+ if startindex < endindex:
+ leftednum = endindex - startindex
+ for i in range(leftednum):
+ tmp = nums[startindex + i]
+ nums[startindex + i] = nums[i]
+ nums[i] = tmp
+ else:
+ break
+ endindex = k
+ k = k - leftednum
+ startindex = k
+
+ def rotate3(self, nums: List[int], k: int) -> None:
+ """
+ Do not return anything, modify nums in-place instead.
+ """
+ if len(nums) == 0 or len(nums) == 1:
+ return
+ l = len(nums)
+ if k > l:
+ k = k % l
+
+ newarr = nums[len(nums) - k:]
+ for i in range(len(nums) - k - 1, -1, -1):
+ nums[i + k] = nums[i]
+ for i in range(k):
+ nums[i] = newarr[i]
+
+ def rotate2(self, nums: List[int], k: int) -> None:
+ """
+ Do not return anything, modify nums in-place instead.
+ """
+ if len(nums) == 0 or len(nums) == 1:
+ return
+ l = len(nums)
+ if k > l:
+ k = k % l
+
+ for _ in range(k):
+ lastel = nums[l - 1]
+ for i in range(l - 2, -1, -1):
+ nums[i + 1] = nums[i]
+ nums[0] = lastel
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-21-10.py b/Week_01/id_10/Leecode-21-10.py
new file mode 100644
index 00000000..3be56211
--- /dev/null
+++ b/Week_01/id_10/Leecode-21-10.py
@@ -0,0 +1,30 @@
+# Definition for singly-linked list.
+# class ListNode:
+# def __init__(self, x):
+# self.val = x
+# self.next = None
+
+class Solution:
+ def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
+ cur1,cur2=l1,l2
+ head,cur=None,None
+ while cur1 or cur2:
+ prev=cur
+ if cur1 and cur2:
+ if cur1.val>cur2.val:
+ cur=cur2
+ cur2=cur2.next
+ else:
+ cur=cur1
+ cur1=cur1.next
+ elif cur1:
+ cur=cur1
+ cur1=cur1.next
+ else:
+ cur=cur2
+ cur2=cur2.next
+ if not head:
+ head=cur
+ if prev:
+ prev.next=cur
+ return head
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-236-10.py b/Week_01/id_10/Leecode-236-10.py
new file mode 100644
index 00000000..85d72421
--- /dev/null
+++ b/Week_01/id_10/Leecode-236-10.py
@@ -0,0 +1,22 @@
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, x):
+# self.val = x
+# self.left = None
+# self.right = None
+
+class Solution:
+ def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
+ if not root or not p and not q:
+ return None
+ if root==p or root==q:
+ return root
+ leftTree=self.lowestCommonAncestor(root.left,p,q)
+ rightTree=self.lowestCommonAncestor(root.right,p,q)
+ if not leftTree and not rightTree:
+ return None
+ if leftTree and rightTree:
+ return root
+ if leftTree:
+ return leftTree
+ return rightTree
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-24-10.py b/Week_01/id_10/Leecode-24-10.py
new file mode 100644
index 00000000..d1726ff0
--- /dev/null
+++ b/Week_01/id_10/Leecode-24-10.py
@@ -0,0 +1,26 @@
+# Definition for singly-linked list.
+# class ListNode:
+# def __init__(self, x):
+# self.val = x
+# self.next = None
+
+# 时间复杂度 O(n)
+# 空间复杂度 O(1)
+class Solution:
+ def swapPairs(self, head: ListNode) -> ListNode:
+ if not head or not head.next:
+ return head
+ last,node1,node2=None,head,head.next
+ head=node2
+ while node2:
+ nextnode=node2.next
+ node2.next=node1
+ if last:
+ last.next=node2
+ node1.next=nextnode
+ if not nextnode:
+ break
+ last=node1
+ node1=nextnode
+ node2=nextnode.next
+ return head
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-257-10.go b/Week_01/id_10/Leecode-257-10.go
new file mode 100644
index 00000000..131e299b
--- /dev/null
+++ b/Week_01/id_10/Leecode-257-10.go
@@ -0,0 +1,39 @@
+import "strconv"
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ * Val int
+ * Left *TreeNode
+ * Right *TreeNode
+ * }
+ */
+var allroutes []string
+
+func binaryTreePaths(root *TreeNode) []string {
+ if root == nil {
+ return []string{}
+ }
+ allroutes = make([]string, 0)
+ dfs("", root)
+ return allroutes
+}
+
+func dfs(str string, root *TreeNode) {
+ curroute := ""
+ if str == "" {
+ curroute = strconv.Itoa(root.Val)
+ } else {
+ curroute = str + "->" + strconv.Itoa(root.Val)
+ }
+ if root.Left == nil && root.Right == nil {
+ allroutes = append(allroutes, curroute)
+ return
+ }
+ if root.Left != nil {
+ dfs(curroute, root.Left)
+ }
+ if root.Right != nil {
+ dfs(curroute, root.Right)
+ }
+}
diff --git a/Week_01/id_10/Leecode-42-10.go b/Week_01/id_10/Leecode-42-10.go
new file mode 100644
index 00000000..9da96323
--- /dev/null
+++ b/Week_01/id_10/Leecode-42-10.go
@@ -0,0 +1,41 @@
+func trap(height []int) int {
+ totals := 0
+ startpos := 0
+ for i := 1; i < len(height); i++ {
+ curh := height[i]
+ if curh >= height[startpos] {
+ minh := height[startpos]
+ curw := i - startpos - 1
+ if curw == 0 {
+ startpos = i
+ continue
+ }
+ rangenums := minh * curw
+ for j := startpos + 1; j < i; j++ {
+ rangenums -= height[j]
+ }
+ totals += rangenums
+ startpos = i
+ }
+ }
+ biggestpos := startpos
+ startpos = len(height) - 1
+ for i := len(height) - 2; i >= biggestpos; i-- {
+ curh := height[i]
+ if curh >= height[startpos] {
+ minh := height[startpos]
+ curw := startpos - i - 1
+ if curw == 0 {
+ startpos = i
+ continue
+ }
+ rangenums := minh * curw
+ for j := i + 1; j < startpos; j++ {
+ rangenums -= height[j]
+ }
+ totals += rangenums
+ startpos = i
+ }
+ }
+ return totals
+}
diff --git a/Week_01/id_10/Leecode-49-10.py b/Week_01/id_10/Leecode-49-10.py
new file mode 100644
index 00000000..9226ea0a
--- /dev/null
+++ b/Week_01/id_10/Leecode-49-10.py
@@ -0,0 +1,9 @@
+class Solution:
+ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
+ strmap={}
+ for s in strs:
+ ss=''.join(sorted(s))
+ strarr=strmap.get(ss,[])
+ strarr.append(s)
+ strmap[ss]=strarr
+ return list(strmap.values())
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-50-10.py b/Week_01/id_10/Leecode-50-10.py
new file mode 100644
index 00000000..9a3997ba
--- /dev/null
+++ b/Week_01/id_10/Leecode-50-10.py
@@ -0,0 +1,24 @@
+
+# 看了别人的代码仿照写的
+# 时间复杂度 O(logN)
+# 利用位运算做除2和奇偶判断
+# 空间复杂度 O(1)
+class Solution:
+ def myPow(self, x: float, n: int) -> float:
+ isNegitive=False
+ if n==0:
+ return 1.0
+ if n==1:
+ return x
+ if n<0:
+ isNegitive=True
+ n=0-n
+ res,p=1,x
+ while n!=0:
+ if n&1==1:
+ res*=p
+ p*=p
+ n>>=1
+ if isNegitive:
+ res=1.0/res
+ return res
\ No newline at end of file
diff --git a/Week_01/id_10/Leecode-84-10.py b/Week_01/id_10/Leecode-84-10.py
new file mode 100644
index 00000000..f2ac3194
--- /dev/null
+++ b/Week_01/id_10/Leecode-84-10.py
@@ -0,0 +1,31 @@
+class Solution:
+ def largestRectangleArea(self, heights: List[int]) -> int:
+ if len(heights)==0:
+ return 0
+ stack=collections.deque()
+ maxarea=0
+ for i in range(len(heights)):
+ h=heights[i]
+ l=len(stack)
+ if l>0:
+ startindex=stack[len(stack)-1]
+ while l>0 and heights[startindex]>h:
+ popindex=stack.pop()
+ l-=1
+ startindex=-1
+ if l>0:
+ startindex=stack[l-1]
+ area=(i-startindex-1)*heights[popindex]
+ if maxarea0:
+ startindex=stack[i-1]
+ else:
+ startindex=-1
+ area=heights[stack[i]]*(lastindex-startindex)
+ if maxarea {
+ let sortedStr = Array.from(str).sort().join()
+ if(anaSet[sortedStr]){
+ anaSet[sortedStr].push(str)
+ }
+ else{
+ anaSet[sortedStr] = []
+ anaSet[sortedStr].push(str)
+ }
+
+ })
+
+ let arr = []
+ console.log(anaSet)
+ Object.keys(anaSet).forEach(ana => {
+ arr.push(anaSet[ana])
+ })
+ return arr
+};
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_15_012.java b/Week_01/id_12/LeetCode_15_012.java
new file mode 100644
index 00000000..5715bd6c
--- /dev/null
+++ b/Week_01/id_12/LeetCode_15_012.java
@@ -0,0 +1,71 @@
+//Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
+//
+// Note:
+//
+// The solution set must not contain duplicate triplets.
+//
+// Example:
+//
+//
+//Given array nums = [-1, 0, 1, 2, -1, -4],
+//
+//A solution set is:
+//[
+// [-1, 0, 1],
+// [-1, -1, 2]
+//]
+//
+//
+
+
+class Solution
+{
+ public List> threeSum(int[] nums)
+ {
+ if (nums == null || nums.length < 3)
+ {
+ return new ArrayList<>(0);
+ }
+ Arrays.sort(nums);
+ List> result = new LinkedList<>();
+ int i = 0, j, k, target;
+ while (i < nums.length - 2)
+ {
+ if (i > 0 && nums[i] == nums[i - 1])
+ {
+ i++;
+ continue;
+ }
+ j = i + 1;
+ k = nums.length - 1;
+ target = -nums[i];
+ while (j < k)
+ {
+ if (nums[j] + nums[k] == target)
+ {
+ result.add(Arrays.asList(nums[i], nums[j], nums[k]));
+ while (j < k && nums[j + 1] == nums[j])
+ {
+ j++;
+ }
+ while (j < k && nums[k - 1] == nums[k])
+ {
+ k--;
+ }
+ j++;
+ k--;
+ }
+ else if (nums[j] + nums[k] < target)
+ {
+ j++;
+ }
+ else
+ {
+ k--;
+ }
+ }
+ i++;
+ }
+ return result;
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_189_012.java b/Week_01/id_12/LeetCode_189_012.java
new file mode 100644
index 00000000..4168a29d
--- /dev/null
+++ b/Week_01/id_12/LeetCode_189_012.java
@@ -0,0 +1,65 @@
+//Given an array, rotate the array to the right by k steps, where k is non-negative.
+//
+// Example 1:
+//
+//
+//Input: [1,2,3,4,5,6,7] and k = 3
+//Output: [5,6,7,1,2,3,4]
+//Explanation:
+//rotate 1 steps to the right: [7,1,2,3,4,5,6]
+//rotate 2 steps to the right: [6,7,1,2,3,4,5]
+//rotate 3 steps to the right: [5,6,7,1,2,3,4]
+//
+//
+// Example 2:
+//
+//
+//Input: [-1,-100,3,99] and k = 2
+//Output: [3,99,-1,-100]
+//Explanation:
+//rotate 1 steps to the right: [99,-1,-100,3]
+//rotate 2 steps to the right: [3,99,-1,-100]
+//
+//
+// Note:
+//
+//
+// Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
+// Could you do it in-place with O(1) extra space?
+//
+
+
+class Solution
+{
+ public void rotate(int[] nums, int k)
+ {
+ if (nums == null || nums.length < 2 || k == 0)
+ {
+ return;
+ }
+ k = k % nums.length;
+ swap(nums, 0, nums.length - 1);
+ swap(nums, 0, k - 1);
+ swap(nums, k, nums.length - 1);
+ }
+
+ private void swap(int[] nums, int low, int high)
+ {
+ while (low < high)
+ {
+ if (nums[low] != nums[high])
+ {
+ exchange(nums, low, high);
+ }
+ low++;
+ high--;
+ }
+ }
+
+ private void exchange(int[] nums, int x, int y)
+ {
+ nums[x] ^= nums[y];
+ nums[y] ^= nums[x];
+ nums[x] ^= nums[y];
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_21_012.java b/Week_01/id_12/LeetCode_21_012.java
new file mode 100644
index 00000000..3fbaf195
--- /dev/null
+++ b/Week_01/id_12/LeetCode_21_012.java
@@ -0,0 +1,31 @@
+//Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
+//
+// Example:
+//
+//Input: 1->2->4, 1->3->4
+//Output: 1->1->2->3->4->4
+//
+//
+
+
+/**
+ * Definition for singly-linked list.
+ * public class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode(int x) { val = x; }
+ * }
+ */
+class Solution
+{
+ public ListNode mergeTwoLists(ListNode l1, ListNode l2)
+ {
+ if (l1 == null || l2 == null)
+ {
+ return l1 == null ? l2 : l1;
+ }
+ ListNode temp = l1.val < l2.val ? l1 : l2;
+ temp.next = mergeTwoLists(temp.next, temp == l1 ? l2 : l1);
+ return temp;
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_242_012.java b/Week_01/id_12/LeetCode_242_012.java
new file mode 100644
index 00000000..f37ffbc8
--- /dev/null
+++ b/Week_01/id_12/LeetCode_242_012.java
@@ -0,0 +1,76 @@
+//Given two strings s and t , write a function to determine if t is an anagram of s.
+//
+// Example 1:
+//
+//
+//Input: s = "anagram", t = "nagaram"
+//Output: true
+//
+//
+// Example 2:
+//
+//
+//Input: s = "rat", t = "car"
+//Output: false
+//
+//
+// Note:
+//You may assume the string contains only lowercase alphabets.
+//
+// Follow up:
+//What if the inputs contain unicode characters? How would you adapt your solution to such case?
+//
+
+
+class Solution1
+{
+ public boolean isAnagram(String s, String t)
+ {
+ if (s == null || t == null || s.length() != t.length())
+ {
+ return false;
+ }
+ int[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
+ 73, 79, 83, 89, 97, 101};
+ BigInteger productS = BigInteger.valueOf(1);
+ BigInteger productT = BigInteger.valueOf(1);
+ for (int i = 0; i < s.length(); i++)
+ {
+ productS = productS.multiply(BigInteger.valueOf(prime[s.charAt(i) - 'a']));
+ }
+ for (int i = 0; i < t.length(); i++)
+ {
+ productT = productT.multiply(BigInteger.valueOf(prime[t.charAt(i) - 'a']));
+ }
+ return productS.compareTo(productT) == 0;
+ }
+}
+
+
+class Solution2
+{
+ public boolean isAnagram(String s, String t)
+ {
+ if (s == null || t == null || s.length() != t.length())
+ {
+ return false;
+ }
+ int[] alphabet = new int[26];
+ for (int i = 0; i < s.length(); i++)
+ {
+ alphabet[s.charAt(i) - 'a']++;
+ }
+ for (int i = 0; i < t.length(); i++)
+ {
+ alphabet[t.charAt(i) - 'a']--;
+ }
+ for (int letter : alphabet)
+ {
+ if (letter != 0)
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_24_012.java b/Week_01/id_12/LeetCode_24_012.java
new file mode 100644
index 00000000..0011eaf6
--- /dev/null
+++ b/Week_01/id_12/LeetCode_24_012.java
@@ -0,0 +1,61 @@
+//Given a linked list, swap every two adjacent nodes and return its head.
+//
+// You may not modify the values in the list's nodes, only nodes itself may be changed.
+//
+//
+//
+// Example:
+//
+//
+//Given 1->2->3->4, you should return the list as 2->1->4->3.
+//
+//
+
+
+/**
+ * Definition for singly-linked list.
+ * public class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode(int x) { val = x; }
+ * }
+ */
+class Solution
+{
+
+ private boolean flag = true;
+
+ public ListNode swapPairs(ListNode head)
+ {
+ if (head == null || head.next == null)
+ {
+ return head;
+ }
+ ListNode start = head.next;
+ recursion(head, null, null);
+ return start;
+ }
+
+ private void recursion(ListNode node, ListNode pre1, ListNode pre2)
+ {
+ if (node == null)
+ {
+ return;
+ }
+ flag = !flag;
+ if (flag)
+ {
+ pre1.next = node.next;
+ node.next = pre1;
+ if (pre2 != null)
+ {
+ pre2.next = node;
+ }
+ recursion(pre1.next, pre1, node);
+ }
+ else
+ {
+ recursion(node.next, node, pre1);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_26_012.java b/Week_01/id_12/LeetCode_26_012.java
new file mode 100644
index 00000000..b9d55eda
--- /dev/null
+++ b/Week_01/id_12/LeetCode_26_012.java
@@ -0,0 +1,67 @@
+//Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
+//
+// Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
+//
+// Example 1:
+//
+//
+//Given nums = [1,1,2],
+//
+//Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
+//
+//It doesn't matter what you leave beyond the returned length.
+//
+// Example 2:
+//
+//
+//Given nums = [0,0,1,1,1,2,2,3,3,4],
+//
+//Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
+//
+//It doesn't matter what values are set beyond the returned length.
+//
+//
+// Clarification:
+//
+// Confused why the returned value is an integer but your answer is an array?
+//
+// Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
+//
+// Internally you can think of this:
+//
+//
+//// nums is passed in by reference. (i.e., without making a copy)
+//int len = removeDuplicates(nums);
+//
+//// any modification to nums in your function would be known by the caller.
+//// using the length returned by your function, it prints the first len elements.
+//for (int i = 0; i < len; i++) {
+// print(nums[i]);
+//}
+//
+
+
+class Solution
+{
+ public int removeDuplicates(int[] nums)
+ {
+ if (nums == null || nums.length == 0)
+ {
+ return 0;
+ }
+ int x = 1, y = 0;
+ while (x> groupAnagrams(String[] strs)
+ {
+ if (strs == null || strs.length == 0)
+ {
+ return new ArrayList<>(0);
+ }
+ int[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
+ 73, 79, 83, 89, 97, 101};
+ HashMap> map = new HashMap<>();
+ int product;
+ for (String str : strs)
+ {
+ product = 1;
+ for (char c : str.toCharArray())
+ {
+ product *= prime[c - 'a'];
+ }
+ if (map.containsKey(product))
+ {
+ map.get(product).add(str);
+ }
+ else
+ {
+ List list = new LinkedList<>();
+ list.add(str);
+ map.put(product, list);
+ }
+ }
+ return new LinkedList<>(map.values());
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/LeetCode_88_012.java b/Week_01/id_12/LeetCode_88_012.java
new file mode 100644
index 00000000..3763878d
--- /dev/null
+++ b/Week_01/id_12/LeetCode_88_012.java
@@ -0,0 +1,51 @@
+//Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
+//
+// Note:
+//
+//
+// The number of elements initialized in nums1 and nums2 are m and n respectively.
+// You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
+//
+//
+// Example:
+//
+//
+//Input:
+//nums1 = [1,2,3,0,0,0], m = 3
+//nums2 = [2,5,6], n = 3
+//
+//Output: [1,2,2,3,5,6]
+//
+//
+
+
+class Solution
+{
+ public void merge(int[] nums1, int m, int[] nums2, int n)
+ {
+ int i = m - 1, j = n - 1, k = m + n - 1;
+ while (k >= 0)
+ {
+ if (i < 0 && j >= 0)
+ {
+ nums1[k--] = nums2[j--];
+ }
+ else if (j < 0 && i >= 0)
+ {
+ nums1[k--] = nums1[i--];
+ }
+ else if (i < 0 && j < 0)
+ {
+ break;
+ }
+ else if (nums1[i] > nums2[j])
+ {
+ nums1[k--] = nums1[i--];
+ }
+ else
+ {
+ nums1[k--] = nums2[j--];
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_12/NOTE.md b/Week_01/id_12/NOTE.md
deleted file mode 100644
index 107ea7d6..00000000
--- a/Week_01/id_12/NOTE.md
+++ /dev/null
@@ -1 +0,0 @@
-# 学习笔记
diff --git "a/Week_01/id_12/\345\255\246\344\271\240\346\200\273\347\273\223.md" "b/Week_01/id_12/\345\255\246\344\271\240\346\200\273\347\273\223.md"
new file mode 100644
index 00000000..6955ce6b
--- /dev/null
+++ "b/Week_01/id_12/\345\255\246\344\271\240\346\200\273\347\273\223.md"
@@ -0,0 +1,17 @@
+# 学习笔记
+LeetCode链接:https://leetcode.com/shockang/
+本周主要针对性的在LeetCode上刷了关于数组和链表的题目,掌握了几个小技巧:
+1.通过
+a ^= b;
+b ^= a;
+a ^= b;
+可以实现a,b交换,这在数组元素交换中经常可以用到;
+2.数组不考虑重复的时候可以尝试将数组排序方便后续处理;
+3.可以通过递归来解决链表问题;
+4.可以通过素数相乘判断anagram;
+5.数组合并的时候应该从后向前遍历;
+6.链表的相关操作其实就是指针(引用)的传递,操作之前注意引用的备份。
+
+
+
+Chao:题目完成数目很多,非常好。注意多看中文题解和国际站上的代码,进行代码优化。 另外注意自己的代码格式,多用format plugin以及国际上通用的格式。
\ No newline at end of file
diff --git "a/Week_01/id_12/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.xmind" "b/Week_01/id_12/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.xmind"
new file mode 100644
index 00000000..8442bf07
Binary files /dev/null and "b/Week_01/id_12/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.xmind" differ
diff --git a/Week_01/id_13/LeetCode_101_013.js b/Week_01/id_13/LeetCode_101_013.js
new file mode 100644
index 00000000..6de3ff22
--- /dev/null
+++ b/Week_01/id_13/LeetCode_101_013.js
@@ -0,0 +1,27 @@
+/**
+ * Definition for a binary tree node.
+ * function TreeNode(val) {
+ * this.val = val;
+ * this.left = this.right = null;
+ * }
+ */
+/**
+ * @param {TreeNode} root
+ * @return {boolean}
+ * https://leetcode.com/problems/symmetric-tree/
+ */
+var isSymmetric = function(root) {
+
+ if(!root) return root;
+
+ let isMirror= (left, right) => {
+ if (!left && !right) return true;
+ if (!left || !right) return false;
+ return (( left.val === right.val)
+ && isMirror(left.left,right.right)
+ && isMirror(left.right, right.left)
+ )
+ }
+ return isMirror(root.left, root.right);
+};
+
diff --git a/Week_01/id_13/LeetCode_15_013.js b/Week_01/id_13/LeetCode_15_013.js
new file mode 100644
index 00000000..9940c88b
--- /dev/null
+++ b/Week_01/id_13/LeetCode_15_013.js
@@ -0,0 +1,61 @@
+/**
+ * @param {number[]} nums
+ * @return {number[][]}
+ * https://leetcode.com/problems/3sum/
+ */
+
+var threeSum = function(nums) {
+ if(nums.length < 3) return [];
+ nums.sort(function(a,b){return a-b;});
+ let start= 0;
+ let result = new Map();
+ while(nums.length-1>start+1)
+ {
+ let end = nums.length-1;
+ let index = start+1;
+ while(index0)
+ {
+ while(nums[end]==nums[end-1])
+ {
+ end --;
+ }
+ end = end-1;
+ }
+ else
+ {
+ while(nums[index]==nums[index+1])
+ {
+ index++;
+ }
+ index++;
+ }
+ }
+ start = start+1;
+ }
+ let resultArray = new Array();
+ result.forEach(element => {
+ resultArray.push(element);
+
+ });
+
+ return resultArray;
+
+};
+
+let nums = [-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6];
+console.log(threeSum(nums));
diff --git a/Week_01/id_13/LeetCode_174_013.js b/Week_01/id_13/LeetCode_174_013.js
new file mode 100644
index 00000000..f674483e
--- /dev/null
+++ b/Week_01/id_13/LeetCode_174_013.js
@@ -0,0 +1,54 @@
+/**
+ * @param {number[][]} dungeon
+ * @return {number}
+ * https://leetcode.com/problems/dungeon-game/
+ */
+var calculateMinimumHP = function(dungeon) {
+ if (!dungeon || dungeon.length == 0 || dungeon[0].length == 0) return 0;
+ let m = dungeon.length;
+ let n = dungeon[0].length;
+ let health = buildArray(m,n);
+ console.log(m);
+ console.log(n);
+ console.log(health);
+
+ health[m - 1][n - 1] = Math.max(1 - dungeon[m - 1][n - 1], 1);
+
+ for(let i = m - 2; i >= 0; i--)
+ {
+ health[i][n - 1] = Math.max(health[i + 1][n - 1] - dungeon[i][n - 1], 1);
+ }
+ for (let j = n - 2; j >= 0; j--) {
+ health[m - 1][j] = Math.max(health[m - 1][j + 1] - dungeon[m - 1][j], 1);
+ }
+ for (let i = m - 2; i >= 0; i--) {
+ for (let j = n - 2; j >= 0; j--) {
+ let down = Math.max(health[i + 1][j] - dungeon[i][j], 1);
+ let right = Math.max(health[i][j + 1] - dungeon[i][j], 1);
+ health[i][j] = Math.min(right, down);
+ }
+ }
+ return health[0][0];
+
+};
+
+function buildArray(m,n)
+{
+ let result = new Array();
+ for(let i = 0; i 0)
+ {
+ k=k%nums.length;
+ reverse(nums,0,nums.length-1);
+ reverse(nums,0,k-1);
+ reverse(nums,k,nums.length-1);
+ }
+ return nums;
+
+};
+
+function reverse(nums,start,end)
+{
+ if(!nums || !nums.length || start >=end) return ;
+ while (startheight[head])
+ {
+ let a= (tail-head) * height[head] - temp;
+ totalArea += a;
+ temp=0;
+ head= tail;
+ }
+ tail ++;
+ }
+ head = height.length-1;
+ tail = head-1;
+ temp = 0;
+ while(tail >=0 )
+ {
+ temp = temp+height[tail+1];
+ if(height[tail]>=height[head])
+ {
+ let a= (head-tail)*height[head]-temp;
+ totalArea += a;
+ temp=0;
+ head= tail;
+ }
+ tail --;
+ }
+ return totalArea;
+};
+
+let height=[2,0,2];
+console.log(trap(height));
diff --git a/Week_01/id_13/LeetCode_441_013.js b/Week_01/id_13/LeetCode_441_013.js
new file mode 100644
index 00000000..dc2a5ba9
--- /dev/null
+++ b/Week_01/id_13/LeetCode_441_013.js
@@ -0,0 +1,20 @@
+//https://leetcode.com/problems/arranging-coins/
+/**
+ * @param {number} n
+ * @return {number}
+ */
+var arrangeCoins = function(n) {
+ if(n==0) return 0;
+ if(n==1) return 1;
+ let k =1 ;
+ while(n>0 && n >=k)
+ {
+ n=n-k;
+ k++;
+ }
+ return k-1;
+
+};
+let n = 8;
+
+console.log(arrangeCoins(n));
diff --git a/Week_01/id_13/LeetCode_49_013.js b/Week_01/id_13/LeetCode_49_013.js
new file mode 100644
index 00000000..68142166
--- /dev/null
+++ b/Week_01/id_13/LeetCode_49_013.js
@@ -0,0 +1,16 @@
+/**
+ * @param {string[]} strs
+ * @return {string[][]}
+ * https://leetcode.com/problems/group-anagrams/
+ */
+var groupAnagrams = function(strs) {
+ let res = {};
+ for (let str of strs) {
+ let tmp = str.split('').sort().join('');
+ (res[tmp] == null) ? res[tmp] = [str] : res[tmp].push(str);
+ }
+ return Object.values(res);
+};
+
+let input= ["eat", "tea", "tan", "ate", "nat", "bat"];
+console.log(groupAnagrams(input));
diff --git a/Week_01/id_13/LeetCode_50_013.js b/Week_01/id_13/LeetCode_50_013.js
new file mode 100644
index 00000000..9661b715
--- /dev/null
+++ b/Week_01/id_13/LeetCode_50_013.js
@@ -0,0 +1,24 @@
+/**
+ * @param {number} x
+ * @param {number} n
+ * @return {number}
+ * https://leetcode.com/problems/powx-n/
+ */
+
+var myPow = function(x, n) {
+ if(n == 1) return x;
+ if(n == 0) return 1;
+ if(n < 0) return 1/myPow(x,-n);
+ if(n%2==0)
+ {
+ let val= myPow(x,n/2);
+ return val*val;
+ }
+ else
+ {
+ let val = myPow(x,(n-1)/2);
+ return val*val*x;
+ }
+
+}
+console.log(myPow(0.00001,2147483647));
diff --git a/Week_01/id_13/LeetCode_88_013.js b/Week_01/id_13/LeetCode_88_013.js
new file mode 100644
index 00000000..940799cf
--- /dev/null
+++ b/Week_01/id_13/LeetCode_88_013.js
@@ -0,0 +1,33 @@
+/**
+ * @param {number[]} nums1
+ * @param {number} m
+ * @param {number[]} nums2
+ * @param {number} n
+ * @return {void} Do not return anything, modify nums1 in-place instead.
+ * https://leetcode.com/problems/merge-sorted-array/
+ */
+var merge = function(nums1, m, nums2, n) {
+ var x = 0,
+ y = 0;
+
+ nums1.splice(m, nums1.length);
+ nums2.splice(n, nums2.length);
+
+ while(y < n){
+ if(nums2[y] < nums1[x] || nums1[x] === undefined){
+ nums1.splice(x, 0, nums2[y]);
+ x++;
+ y++;
+ } else {
+ x++;
+ }
+ }
+ // return nums1;
+};
+
+let nums1 = [2,0];
+let m = 1;
+let nums2 = [1];
+let n = 1;
+merge(nums1, m, nums2, n);
+console.log(nums1);
diff --git a/Week_01/id_13/NOTE.md b/Week_01/id_13/NOTE.md
index 107ea7d6..031fe4d0 100644
--- a/Week_01/id_13/NOTE.md
+++ b/Week_01/id_13/NOTE.md
@@ -1 +1,6 @@
# 学习笔记
+
+Chao:
+1. 代码格式需要加强
+2. 注意多看中文题解和国际站上的代码,进行代码优化。
+3. 代码逻辑的清晰性还有进一步提高的空间
\ No newline at end of file
diff --git a/Week_01/id_15/LeetCode_189_015.py b/Week_01/id_15/LeetCode_189_015.py
new file mode 100644
index 00000000..1c241955
--- /dev/null
+++ b/Week_01/id_15/LeetCode_189_015.py
@@ -0,0 +1,9 @@
+# Language: Python
+class Solution:
+ def rotate(self, nums: List[int], k: int) -> None:
+ """
+ Do not return anything, modify nums in-place instead.
+ """
+ k %= len(nums)
+ if k:
+ nums[:k], nums[k:] = nums[-k:], nums[:-k]
diff --git a/Week_01/id_15/LeetCode_26_015.py b/Week_01/id_15/LeetCode_26_015.py
new file mode 100644
index 00000000..68619dfb
--- /dev/null
+++ b/Week_01/id_15/LeetCode_26_015.py
@@ -0,0 +1,11 @@
+# Language: Python
+class Solution:
+ def removeDuplicates(self, nums: List[int]) -> int:
+ curr_idx = 0
+ while curr_idx < len(nums) - 1:
+ if nums[curr_idx] == nums[curr_idx+1]:
+ del nums[curr_idx]
+ curr_idx -= 1
+ curr_idx += 1
+ return len(nums)
+
diff --git a/Week_01/id_15/NOTE.md b/Week_01/id_15/NOTE.md
index 107ea7d6..7faa5445 100644
--- a/Week_01/id_15/NOTE.md
+++ b/Week_01/id_15/NOTE.md
@@ -1 +1,53 @@
# 学习笔记
+## 本周回顾
+本人非计算机专业出身,相比于其他同学而言基础会相对薄弱些。针对自己的能力特点,为自己设定了合理的学习计划和目标,并且于本周达成了三个小目标。
+1. 算法图解图书阅读(核心内容笔记):
+ 1. 二分查找的速度比简单(全局遍历)的查找速度快的多
+ 2. O(log n) 比O(n)快,需要搜索的元素越多,前者比后者越快
+ 3. 算法运行时间并不以秒为单位
+ 4. 算法运行时间是从其增速的角度衡量
+ 5. 算法运行时间用大O表示法来表示
+ 6. 需要存储多个元素时,可以用数组或链表
+ 7. 数组的元素都在一起
+ 8. 链表的元素是分开的,其中每个元素都存储了下一个元素的地址
+ 9. 数组的读取速度是很快的
+ 10. 链表的插入和删除速度是很快的
+ 11. 同一个数组中,所有元素的类型都必须相同
+ 12. 递归指的是调用自己的函数
+ 13. 每个递归函数都有两个条件:基线条件和递归条件
+ 14. 栈有两种操作:压入和弹出;并且栈是后进先出(LIFO);队列是先进先出(FIFO)
+ 15. 所有函数调用都进入调用栈
+ 16. D & C (分治)将问题逐步分解,编写涉及数组的递归函数时,基线条件通常是数组为空或只包含一个元素。
+ 17. 散列表合适用于模拟映射关系
+ 18. 一旦装填因子超过0.7,就该调整该散列表的长度
+ 19. 图的广度优先搜索支出是否具有从A到B的最短路径,如果有广度优先搜索将找出最短路径
+ 20. 面临类似寻找最短路径的问题时,可以尝试使用图来建立模型,再使用广度优先搜索来解决问题。
+ 21. 你需要按照加入顺利来检查搜索列表中的人,否则找到的就不是最短路径,因此搜索列表必须是队列。
+ 22. 对于检查过的人,务必不要再去检查,否则形成无限循环。
+ 23. 广度优先搜索用于在非加权图中查找最短路径
+ 24. 狄克思算法用于在加权图中寻找最短路径
+ 25. 仅当权重为非负的时候,狄克思算法才管用
+ 26. 如果图包含负权边,请使用贝尔曼-福德算法
+ 27. 贪婪算法寻找局部最优解,企图以这种方式获取全局最优解
+ 28. 对于NP完全问题(Non-deterministic Polynomial的问题,即多项式复杂程度的非确定性问题),还没有找到快速解决方案
+ 29. 面临NP问题的最优做法是使用近似算法
+ 30. 贪婪算法易于实现,运行速度快,是不错的近似算法
+ 31.在给定约束条件下优化某种指标的时候,动态规划很有用
+ 32. 问题可分解为离散子问题,可使用动态规划来解决
+ 33. 每种动态规划方案都涉及网络
+ 34. 单元格中的值通通常就是要优化的值
+ 35. 每个单元格都是一个子问题,因此你需要考虑如何将问题分解为子问题
+
+2. Git视频课程30讲
+ 2.1 核心内容笔记:https://mubu.com/doc/21G3uNP1Lw
+
+3. 算法视频课程20节
+4. 算法习题2题
+
+## 下周规划
+1. 读完半本(200页左右)的算法与数据结构Python语言描述
+2. 继续刷剩余的算法题(上周剩余及本周新增)
+
+
+
+Chao: 非常好的总结!继续加油!
\ No newline at end of file
diff --git a/Week_01/id_16/74 b/Week_01/id_16/74
new file mode 100644
index 00000000..6964952c
--- /dev/null
+++ b/Week_01/id_16/74
@@ -0,0 +1,26 @@
+import java.util.*;
+class Solution {
+ public List> groupAnagrams(String[] strs)
+ {
+ List a=new List();
+ for(int i=0;i ans = new HashMap();
+ for (String s:strs)
+ {
+ char[] ca=s.toCharArray();
+ Arrays.sort(ca);
+ String key=String.valueOf(ca);
+ if (!ans.containsKey(key)) ans.put(key, new ArrayList());
+ ans.get(key).add(s);
+ }
+ return new ArrayList(ans.values());
+ }
\ No newline at end of file
diff --git a/Week_01/id_16/Solution.java b/Week_01/id_16/Solution.java
new file mode 100644
index 00000000..ce43548b
--- /dev/null
+++ b/Week_01/id_16/Solution.java
@@ -0,0 +1,11 @@
+public class Solution {
+ public boolean isAnagram(String s,String t)
+ {
+ char[] sChars=s.toCharArray();
+ char[] tChars=t.toCharArray();
+ Arrays.sort(sChars);
+ Arrays.sort(tChars);
+ return String.valueOf(sChars).equals(String.valueOf(tChars));
+
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_16/algorithm.pdf b/Week_01/id_16/algorithm.pdf
new file mode 100644
index 00000000..84967d5b
Binary files /dev/null and b/Week_01/id_16/algorithm.pdf differ
diff --git a/Week_01/id_17/LeetCode_15_17.go b/Week_01/id_17/LeetCode_15_17.go
new file mode 100644
index 00000000..865c5d29
--- /dev/null
+++ b/Week_01/id_17/LeetCode_15_17.go
@@ -0,0 +1,74 @@
+import (
+ "sort"
+)
+
+/*
+ * @lc app=leetcode id=15 lang=golang
+ *
+ * [15] 3Sum
+ *
+ * https://leetcode.com/problems/3sum/description/
+ *
+ * algorithms
+ * Medium (24.03%)
+ * Likes: 3806
+ * Dislikes: 425
+ * Total Accepted: 555.7K
+ * Total Submissions: 2.3M
+ * Testcase Example: '[-1,0,1,2,-1,-4]'
+ *
+ * Given an array nums of n integers, are there elements a, b, c in nums such
+ * that a + b + c = 0? Find all unique triplets in the array which gives the
+ * sum of zero.
+ *
+ * Note:
+ *
+ * The solution set must not contain duplicate triplets.
+ *
+ * Example:
+ *
+ *
+ * Given array nums = [-1, 0, 1, 2, -1, -4],
+ *
+ * A solution set is:
+ * [
+ * [-1, 0, 1],
+ * [-1, -1, 2]
+ * ]
+ *
+ *
+ */
+func threeSum(nums []int) [][]int {
+ sort.Ints(nums)
+ var res [][]int
+
+ for i1 := 0; i1 < len(nums)-2; i1++ {
+ if i1 > 0 && nums[i1] == nums[i1-1] {
+ continue
+ }
+ left := i1 + 1
+ right := len(nums) - 1
+ for left < right {
+ // fmt.Println(nums[i1], nums[left], nums[right])
+ tmpsum := nums[i1] + nums[left] + nums[right]
+ if tmpsum == 0 {
+ res = append(res, []int{nums[i1], nums[left], nums[right]})
+
+ for left < right && nums[left] == nums[left+1] {
+ left++
+ }
+ for left < right && nums[right] == nums[right-1] {
+ right--
+ }
+ left++
+ right--
+ } else if tmpsum > 0 {
+ right--
+ } else if tmpsum < 0 {
+ left++
+ }
+ }
+ }
+ return res
+}
+
diff --git a/Week_01/id_17/LeetCode_84_17.go b/Week_01/id_17/LeetCode_84_17.go
new file mode 100644
index 00000000..3c37ee1e
--- /dev/null
+++ b/Week_01/id_17/LeetCode_84_17.go
@@ -0,0 +1,68 @@
+/*
+ * @lc app=leetcode id=84 lang=golang
+ *
+ * [84] Largest Rectangle in Histogram
+ *
+ * https://leetcode.com/problems/largest-rectangle-in-histogram/description/
+ *
+ * algorithms
+ * Hard (31.15%)
+ * Likes: 1874
+ * Dislikes: 50
+ * Total Accepted: 174.6K
+ * Total Submissions: 560.4K
+ * Testcase Example: '[2,1,5,6,2,3]'
+ *
+ * Given n non-negative integers representing the histogram's bar height where
+ * the width of each bar is 1, find the area of largest rectangle in the
+ * histogram.
+ *
+ *
+ *
+ *
+ * Above is a histogram where width of each bar is 1, given height =
+ * [2,1,5,6,2,3].
+ *
+ *
+ *
+ *
+ * The largest rectangle is shown in the shaded area, which has area = 10
+ * unit.
+ *
+ *
+ *
+ * Example:
+ *
+ *
+ * Input: [2,1,5,6,2,3]
+ * Output: 10
+ *
+ *
+ */
+func largestRectangleArea(heights []int) int {
+ var newstack = []int{0}
+ var posstack = []int{0}
+ heights = append(heights, 0)
+ maxarea := 0
+ for i, v := range heights {
+ for l := len(newstack); newstack[l-1] > v; l = len(newstack) {
+ tmpheight := newstack[l-1]
+ left := posstack[l-2]
+ right := i
+ maxarea = max((right-left)*tmpheight, maxarea)
+ posstack = posstack[:l-1]
+ newstack = newstack[:l-1]
+ }
+ newstack = append(newstack, v)
+ posstack = append(posstack, i+1)
+ }
+ return maxarea
+}
+func max(x, y int) int {
+ if x > y {
+ return x
+ }
+ return y
+}
+
+
diff --git "a/Week_01/id_17/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.png" "b/Week_01/id_17/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.png"
new file mode 100644
index 00000000..f059a66b
Binary files /dev/null and "b/Week_01/id_17/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225.png" differ
diff --git a/Week_01/id_18/LeetCode_101_18.java b/Week_01/id_18/LeetCode_101_18.java
new file mode 100644
index 00000000..c41e289f
--- /dev/null
+++ b/Week_01/id_18/LeetCode_101_18.java
@@ -0,0 +1,92 @@
+package Week_01.id_18;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.Stack;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/10 18:52
+ */
+public class LeetCode_101_18 {
+ public boolean isSymmetric(TreeNode root) {
+ return doCheck(root, root);
+ }
+
+ private boolean doCheck(TreeNode left, TreeNode right) {
+ if (left == null && right == null) {
+ return true;
+ }
+
+ if (left == null || right == null) {
+ return false;
+ }
+
+ return (left.val == right.val) && doCheck(left.left, right.right) && doCheck(left.right, right.left);
+ }
+
+ public boolean isSymmetric1(TreeNode root) {
+ Queue queue = new LinkedList<>();
+ queue.add(root);
+ queue.add(root);
+
+ while (!queue.isEmpty()) {
+ TreeNode left = queue.poll();
+ TreeNode right = queue.poll();
+
+ if (left == null && right == null) {
+ continue;
+ }
+
+ if (left == null || right == null) {
+ return false;
+ }
+
+ if (left.val != right.val) {
+ return false;
+ }
+
+ queue.add(left.left);
+ queue.add(right.right);
+ queue.add(left.right);
+ queue.add(right.left);
+ }
+ return true;
+ }
+
+ public boolean isSymmetric2(TreeNode root) {
+ Stack stack = new Stack<>();
+ stack.push(root);
+ stack.push(root);
+
+ while (!stack.isEmpty()) {
+ TreeNode left = stack.pop();
+ TreeNode right = stack.pop();
+
+ if (left == null && right == null) {
+ continue;
+ }
+
+ if (left == null || right == null) {
+ return false;
+ }
+
+ if (left.val != right.val) {
+ return false;
+ }
+
+ stack.push(left.left);
+ stack.push(right.right);
+ stack.push(left.right);
+ stack.push(right.left);
+ }
+ return true;
+ }
+
+ private class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+ TreeNode(int x) { val = x; }
+ }
+}
diff --git a/Week_01/id_18/LeetCode_1021_18.java b/Week_01/id_18/LeetCode_1021_18.java
new file mode 100644
index 00000000..b89784ed
--- /dev/null
+++ b/Week_01/id_18/LeetCode_1021_18.java
@@ -0,0 +1,22 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/10 13:17
+ */
+public class LeetCode_1021_18 {
+ public String removeOuterParentheses(String S) {
+ int count = 0, start = 1;
+ char[] cs = S.toCharArray();
+ StringBuilder sb = new StringBuilder();
+
+ for (int i = 0; i < cs.length; i++) {
+ count = cs[i] == '(' ? ++count : --count;
+ if (count == 0) {
+ sb.append(S, start, i);
+ start = i + 2;
+ }
+ }
+ return sb.toString();
+ }
+}
diff --git a/Week_01/id_18/LeetCode_1047_18.java b/Week_01/id_18/LeetCode_1047_18.java
new file mode 100644
index 00000000..eccb5c9c
--- /dev/null
+++ b/Week_01/id_18/LeetCode_1047_18.java
@@ -0,0 +1,49 @@
+package Week_01.id_18;
+
+import java.util.Stack;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/5 13:28
+ */
+public class LeetCode_1047_18 {
+ public String removeDuplicates(String S) {
+ if (S == null || "".equals(S)) {
+ return S;
+ }
+
+ Stack stack = new Stack<>();
+ for (char c: S.toCharArray()) {
+ if (stack.isEmpty() || stack.peek() != c) {
+ stack.push(c);
+ } else {
+ stack.pop();
+ }
+ }
+
+ StringBuilder result = new StringBuilder();
+ for (Character c: stack) {
+ result.append(c);
+ }
+
+ return result.toString();
+ }
+
+ public String removeDuplicates1(String S) {
+ if (S == null || "".equals(S)) {
+ return S;
+ }
+
+ char[] cs = S.toCharArray();
+ int nonDuplicatesIndex = -1;
+ for (int i = 0; i < cs.length; i++) {
+ if (nonDuplicatesIndex == -1 || cs[nonDuplicatesIndex] != cs[i]) {
+ cs[++nonDuplicatesIndex] = cs[i];
+ } else {
+ nonDuplicatesIndex--;
+ }
+ }
+
+ return new String(cs, 0, nonDuplicatesIndex + 1);
+ }
+}
diff --git a/Week_01/id_18/LeetCode_104_18.java b/Week_01/id_18/LeetCode_104_18.java
new file mode 100644
index 00000000..1c836afc
--- /dev/null
+++ b/Week_01/id_18/LeetCode_104_18.java
@@ -0,0 +1,23 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/7 18:56
+ */
+public class LeetCode_104_18 {
+ public int maxDepth(TreeNode root) {
+ return doSearch(0, root);
+ }
+
+ private int doSearch(int level, TreeNode root) {
+ return root == null? level: Math.max(doSearch(++level, root.left), doSearch(level, root.right));
+ }
+
+ private class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+ TreeNode(int x) { val = x; }
+ }
+}
+
diff --git a/Week_01/id_18/LeetCode_15_18.java b/Week_01/id_18/LeetCode_15_18.java
new file mode 100644
index 00000000..32cbba6b
--- /dev/null
+++ b/Week_01/id_18/LeetCode_15_18.java
@@ -0,0 +1,58 @@
+package Week_01.id_18;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/9 19:23
+ */
+public class LeetCode_15_18 {
+ public List> threeSum(int[] nums) {
+ if (nums == null || nums.length < 3) {
+ return Collections.emptyList();
+ }
+
+ Arrays.sort(nums);
+ List> result = new ArrayList<>();
+
+ int pointer = 0, head, tail, target;
+ while (pointer < nums.length - 2) {
+ if (pointer > 0 && nums[pointer] == nums[pointer - 1]) {
+ pointer++;
+ continue;
+ }
+
+ head = pointer + 1;
+ tail = nums.length - 1;
+ target = -nums[pointer];
+
+ while (head < tail) {
+ if (nums[head] + nums[tail] == target) {
+ result.add(Arrays.asList(nums[pointer], nums[head], nums[tail]));
+
+ while (head < tail && nums[head] == nums[head + 1]) {
+ head++;
+ }
+
+ while (head < tail && nums[tail - 1] == nums[tail]) {
+ tail--;
+ }
+
+ head++;
+ tail--;
+ } else if (nums[head] + nums[tail] < target) {
+ head++;
+ } else {
+ tail--;
+ }
+ }
+
+ pointer++;
+ }
+
+ return result;
+ }
+}
diff --git a/Week_01/id_18/LeetCode_189_18.java b/Week_01/id_18/LeetCode_189_18.java
new file mode 100644
index 00000000..1f32ddca
--- /dev/null
+++ b/Week_01/id_18/LeetCode_189_18.java
@@ -0,0 +1,74 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/8 17:29
+ */
+public class LeetCode_189_18 {
+ public void rotate(int[] nums, int k) {
+ if (nums == null || nums.length == 0) {
+ return;
+ }
+
+ for (int i = 0; i < k; i++) {
+ doMove(nums);
+ }
+ }
+
+ private void doMove(int[] nums) {
+ int store = nums[0];
+ for (int i = 0; i < nums.length - 1; i++) {
+ int tmp = store;
+ store = nums[i + 1];
+ nums[i + 1] = tmp;
+ }
+ nums[0] = store;
+ }
+
+ public void rotate1(int[] nums, int k) {
+ if (k == 0 || nums == null || nums.length <= 1 || (k % nums.length == 0)) {
+ return;
+ }
+
+
+ int index = 0;
+ int start = 0;
+ int store = nums[index];
+ for (int i = 0; i < nums.length; i++) {
+ index = index + k >= nums.length ? (index + k) % nums.length: index + k;
+ int tmpStore = store;
+ store = nums[index];
+ nums[index] = tmpStore;
+ if (index == start) {
+ start = ++index;
+ store = nums[index];
+ }
+ }
+ }
+
+ public void rotate2(int[] nums, int k) {
+ if (k == 0 || nums == null || nums.length < 2 || k % nums.length == 0) {
+ return;
+ }
+
+ k %= nums.length;
+
+ swap(nums, 0, nums.length - 1);
+ swap(nums, 0, k - 1);
+ swap(nums, k, nums.length - 1);
+ }
+
+ private void swap(int[] nums, int head, int tail) {
+ while (head < tail) {
+ exchange(nums, head, tail);
+ head++;
+ tail--;
+ }
+ }
+
+ private void exchange(int[] nums, int x, int y) {
+ nums[x] ^= nums[y];
+ nums[y] ^= nums[x];
+ nums[x] ^= nums[y];
+ }
+}
diff --git a/Week_01/id_18/LeetCode_21_18.java b/Week_01/id_18/LeetCode_21_18.java
new file mode 100644
index 00000000..7e9a9286
--- /dev/null
+++ b/Week_01/id_18/LeetCode_21_18.java
@@ -0,0 +1,22 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/9 20:19
+ */
+public class LeetCode_21_18 {
+ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
+ if (l1 == null || l2 == null)
+ {
+ return l1 == null ? l2 : l1;
+ }
+ ListNode temp = l1.val < l2.val ? l1 : l2;
+ temp.next = mergeTwoLists(temp.next, temp == l1 ? l2 : l1);
+ return temp;
+ }
+ private class ListNode {
+ int val;
+ ListNode next;
+ ListNode(int x) { val = x; }
+ }
+}
diff --git a/Week_01/id_18/LeetCode_242_18.java b/Week_01/id_18/LeetCode_242_18.java
new file mode 100644
index 00000000..b2c4e6c5
--- /dev/null
+++ b/Week_01/id_18/LeetCode_242_18.java
@@ -0,0 +1,136 @@
+package Week_01.id_18;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/4 13:09
+ */
+public class LeetCode_242_18 {
+ public boolean isAnagram1(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+
+ char[] tc = t.toCharArray();
+ for (char sc: s.toCharArray()) {
+ for (int i = 0; i < tc.length; i++) {
+ if (sc == tc[i]) {
+ tc[i] = '#';
+ break;
+ }
+ }
+ }
+
+ for (char c: tc) {
+ if (c != '#') {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public boolean isAnagram2(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+
+ Map map = Arrays.stream(s.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
+ for (String ts: t.split("")) {
+ if (map.containsKey(ts)) {
+ map.put(ts, map.get(ts) - 1);
+ continue;
+ }
+ return false;
+ }
+
+ for (Long value: map.values()) {
+ if (value != 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public boolean isAnagram3(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+
+ Map map = s.chars().boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
+ for (char c: t.toCharArray()) {
+ int ci = (int)c;
+ if (map.containsKey(ci)) {
+ map.put(ci, map.get(ci) - 1);
+ continue;
+ }
+ return false;
+ }
+
+ for (Long value: map.values()) {
+ if (value != 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public boolean isAnagram4(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+ char[] cs = s.toCharArray();
+ char[] ct = t.toCharArray();
+ Arrays.sort(cs);
+ Arrays.sort(ct);
+ return Arrays.equals(cs, ct);
+ }
+
+ public boolean isAnagram5(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+ char[] cs = s.toCharArray();
+ char[] ct = t.toCharArray();
+ int[] countArr = new int[26];
+
+ for (int i = 0; i < cs.length; i++) {
+ countArr[cs[i] - 'a']++;
+ countArr[ct[i] - 'a']--;
+ }
+
+ for (int i: countArr) {
+ if (i != 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public boolean isAnagram6( String s, String t) {
+ if (s == null || t == null || s.length() != t.length()) {
+ return false;
+ }
+ int[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
+ 73, 79, 83, 89, 97, 101};
+ BigDecimal productS = BigDecimal.valueOf(1);
+ BigDecimal productT = BigDecimal.valueOf(1);
+ for (int i = 0; i < s.length(); i++)
+ {
+ productS = productS.multiply(BigDecimal.valueOf(prime[s.charAt(i) - 'a']));
+ }
+ for (int i = 0; i < t.length(); i++)
+ {
+ productT = productT.multiply(BigDecimal.valueOf(prime[t.charAt(i) - 'a']));
+ }
+ return productS.compareTo(productT) == 0;
+ }
+}
diff --git a/Week_01/id_18/LeetCode_26_18.java b/Week_01/id_18/LeetCode_26_18.java
new file mode 100644
index 00000000..b60e4925
--- /dev/null
+++ b/Week_01/id_18/LeetCode_26_18.java
@@ -0,0 +1,19 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/3 19:25
+ */
+public class LeetCode_26_18 {
+ public int removeDuplicates(int[] nums) {
+ int nonDuplicatesIndex = 1;
+ for (int i = 1; i < nums.length; i++) {
+ if (nums[i] == nums[i - 1]) {
+ continue;
+ }
+ nums[nonDuplicatesIndex++] = nums[i];
+ }
+
+ return nonDuplicatesIndex;
+ }
+}
diff --git a/Week_01/id_18/LeetCode_441_18.java b/Week_01/id_18/LeetCode_441_18.java
new file mode 100644
index 00000000..89159161
--- /dev/null
+++ b/Week_01/id_18/LeetCode_441_18.java
@@ -0,0 +1,19 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/6 13:30
+ */
+public class LeetCode_441_18 {
+ public int arrangeCoins(int n) {
+ return (int)(Math.sqrt(n * 2.0 + 0.25) - 0.5);
+ }
+
+ public int arrangeCoins1(int n) {
+ int c = 0;
+ while(c < n) {
+ n -= ++c;
+ }
+ return c;
+ }
+}
diff --git a/Week_01/id_18/LeetCode_49_18.java b/Week_01/id_18/LeetCode_49_18.java
new file mode 100644
index 00000000..3721f24b
--- /dev/null
+++ b/Week_01/id_18/LeetCode_49_18.java
@@ -0,0 +1,27 @@
+package Week_01.id_18;
+
+import java.util.*;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/10 12:47
+ */
+public class LeetCode_49_18 {
+ public List> groupAnagrams(String[] strs) {
+ if (strs == null || strs.length == 0) {
+ return Collections.emptyList();
+ }
+
+ Map> map = new HashMap<>();
+ for (String s: strs) {
+ char[] cs = s.toCharArray();
+ Arrays.sort(cs);
+ String tmp = String.valueOf(cs);
+ if (!map.containsKey(tmp)) {
+ map.put(tmp, new ArrayList<>());
+ }
+ map.get(tmp).add(s);
+ }
+ return new ArrayList<>(map.values());
+ }
+}
diff --git a/Week_01/id_18/LeetCode_50_18.java b/Week_01/id_18/LeetCode_50_18.java
new file mode 100644
index 00000000..a7793497
--- /dev/null
+++ b/Week_01/id_18/LeetCode_50_18.java
@@ -0,0 +1,31 @@
+package Week_01.id_18;
+
+/**
+ * @author LiveForExperience
+ * @date 2019/6/10 13:33
+ */
+public class LeetCode_50_18 {
+ private double myPow(double x, int n) {
+ if (n == 0) {
+ return 1;
+ }
+
+ if (n == 1) {
+ return x;
+ }
+
+ if (n == Integer.MIN_VALUE){
+ return 1.0 / myPow(x, -(n/2));
+ }
+
+ if (n < 0) {
+ return 1.0 / myPow(x, -n);
+ }
+
+
+ double y = myPow(x, n / 2);
+ y *= y;
+
+ return n % 2 == 1 ? y * x : y;
+ }
+}
diff --git a/Week_01/id_18/NOTE.md b/Week_01/id_18/NOTE.md
index 107ea7d6..011f11ee 100644
--- a/Week_01/id_18/NOTE.md
+++ b/Week_01/id_18/NOTE.md
@@ -1 +1,1083 @@
# 学习笔记
+## LeetCode_26_18
+### 题目:
+给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
+
+不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
+
+示例 1:
+```
+给定数组 nums = [1,1,2],
+
+函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
+
+你不需要考虑数组中超出新长度后面的元素。
+```
+示例 2:
+```
+给定 nums = [0,0,1,1,1,2,2,3,3,4],
+
+函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
+
+你不需要考虑数组中超出新长度后面的元素。
+```
+说明:
+```
+为什么返回数值是整数,但输出的答案是数组呢?
+
+请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
+```
+你可以想象内部操作如下:
+```
+// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+int len = removeDuplicates(nums);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
+for (int i = 0; i < len; i++) {
+ print(nums[i]);
+}
+```
+### 解法一
+#### 思路
+看到这道题的第一反应就是和覃超老师PPT_**Day-1-2中第24页Array实战的第1题_remove zero**的解题思路很相似。可以用两个游标来解这道题,一个负责遍历数组,一个负责做特殊的工作,remove zero是把非0数放入该游标指向的位置,而这题是把不重复的数放入该游标指向的位置。
+#### 解题过程
+1. 一开始使用了和remove zero很类似的代码,结果出现的数组越界的问题:
+```java
+class Solution {
+ public int removeDuplicates(int[] nums) {
+ int nonDuplicatesIndex = 0;
+ for (int i = 0; i < nums.length; i++) {
+ if (nums[i] != nums[i + 1]) {
+ nums[nonDuplicatesIndex++] = nums[i];
+ }
+ }
+
+ return nonDuplicatesIndex;
+ }
+}
+```
+这时候的思路是,碰到第一个不重复的元素,我就把他放在第二个游标nonDuplicatesIndex指向的位置,然后让它向后移一位,同时第一个游标继续往后查。
+但是这样就会导致遍历到最后一个元素的时候就会越界。
+
+2. 于是就想,要么就比较游标i和它前一个元素,先解决越界的问题。同时,我也就要从下标1开始遍历,否则又得越界,那用人话来解释从1开始的话......又想了下,因为,数组第一个绝对就是不重复的呀,那放在nonDuplicatesIndex的位置天经地义啊,好了,逻辑自洽了。。。
+```java
+class Solution {
+ public int removeDuplicates(int[] nums) {
+ int nonDuplicatesIndex = 1;
+ for (int i = 1; i < nums.length; i++) {
+ if (nums[i] != nums[i - 1]) {
+ nums[nonDuplicatesIndex++] = nums[i];
+ }
+ }
+
+ return nonDuplicatesIndex;
+ }
+}
+```
+这次的提交结果,耗时是3ms,只打败了50%多的人,说明时间还可以优化。
+
+3. 于是就想到了用如下的方式试一下,耗时居然提升到了2ms。。。不知道是瞎猫碰到死耗子,还是本身就有精度的误差,其实没什么区别。。。
+```java
+class Solution {
+ public int removeDuplicates(int[] nums) {
+ int nonDuplicatesIndex = 1;
+ for (int i = 1; i < nums.length; i++) {
+ if (nums[i] == nums[i - 1]) {
+ continue;
+ }
+ nums[nonDuplicatesIndex++] = nums[i];
+ }
+
+ return nonDuplicatesIndex;
+ }
+}
+```
+### 收获
+当看完题目后,直接O(1)的从脑子里就闪出了remove zero的解题思路,当时感觉那叫一个爽啊。切身体会到五毒神掌,多做题真的很有用,让大脑形成了一种对某种题目的认知框架,可以辅助自己迅速找到解决问题的方案。
+## LeetCode_242_18
+### 题目
+给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
+
+示例 1:
+```
+输入: s = "anagram", t = "nagaram"
+输出: true
+```
+示例 2:
+```
+输入: s = "rat", t = "car"
+输出: false
+```
+说明:
+```
+你可以假设字符串只包含小写字母。
+```
+### 解法一
+#### 思路
+上来看到两个数比较,控制不住脾气,就是一顿**两个for的嵌套**。不过耗时1000ms+,有很大的优化空间。
+### 解题过程
+话不多说,就是干:
+```java
+class Solution {
+ public boolean isAnagram(String s, String t) {
+ char[] tc = t.toCharArray();
+ for (char sc: s.toCharArray()) {
+ for (int i = 0; i < tc.length; i++) {
+ if (sc == tc[i]) {
+ tc[i] = '#';
+ break;
+ }
+ }
+ }
+
+ for (char c: tc) {
+ if (c != '#') {
+ return false;
+ }
+ }
+
+ return s.length() == t.length();
+ }
+}
+```
+因为有两个for嵌套,所以时间复杂度是O(n^2),效率不高。
+### 解法二
+#### 思路
+两个字符串的比较,其实也可以想成是组成的字符个数的比较,可以用一个map来统计两个字符串中各个字符的个数是否相等。
+#### 解题过程
+这个话也不多说了,继续干:
+```java
+class Solution {
+ public boolean isAnagram(String s, String t) {
+ Map map = Arrays.stream(s.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
+ for (String ts: t.split("")) {
+ if (map.containsKey(ts)) {
+ map.put(ts, map.get(ts) - 1);
+ continue;
+ }
+ return false;
+ }
+
+ for (Long value: map.values()) {
+ if (value != 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
+```
+因为时间复杂度是O(n),所以时间确实是从一开始的1000ms缩短到了300ms不到,但是仍然只打败了5%的人,而且因为使用的是String,空间也多占用了20M。。。还可以优化。
+### 解法三
+#### 思路
+先在解法二的基础上,优化下空间大小,那就使用char数组。
+#### 解题过程
+```java
+public class Solution {
+ public boolean isAnagram(String s, String t) {
+ Map map = s.chars().boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
+ for (char c: t.toCharArray()) {
+ int ci = (int)c;
+ if (map.containsKey(ci)) {
+ map.put(ci, map.get(ci) - 1);
+ continue;
+ }
+ return false;
+ }
+
+ for (Long value: map.values()) {
+ if (value != 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
+```
+执行后,空间和解法一少了1M左右,同时速度比解法二也提升了,140ms了,但是还是只打败了5%,还可以优化。
+### 解法四
+#### 思路
+在想出以上3种解法后,挣扎了10分多种,没再能想出更好的方法,于是使用五毒神掌第一式,看solution:
+#### 解题过程
+```java
+class Solution {
+ public boolean isAnagram(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+ char[] cs = s.toCharArray();
+ char[] ct = t.toCharArray();
+ Arrays.sort(cs);
+ Arrays.sort(ct);
+ return Arrays.equals(cs, ct);
+ }
+}
+```
+看完代码,就想自己怎么这么蠢,现成的好东西不用。。。
+### 解法五
+#### 思路
+继续找solution中的好解法:
+#### 解题思路
+```java
+class Solution {
+ public boolean isAnagram(String s, String t) {
+ if (s.length() != t.length()) {
+ return false;
+ }
+ char[] cs = s.toCharArray();
+ char[] ct = t.toCharArray();
+ int[] countArr = new int[26];
+
+ for (int i = 0; i < cs.length; i++) {
+ countArr[cs[i] - 'a']++;
+ countArr[ct[i] - 'a']--;
+ }
+
+ for (int i: countArr) {
+ if (i != 0) {
+ return false;
+ }
+ }
+
+ return ture;
+ }
+}
+```
+### 解法六
+参考了其他同学的解法,直接使用素数相乘的方法,简直绝了
+```java
+class Solution {
+ public boolean isAnagram(String s, String t) {
+ if (s == null || t == null || s.length() != t.length()) {
+ return false;
+ }
+ int[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
+ 73, 79, 83, 89, 97, 101};
+ BigInteger productS = BigInteger.valueOf(1);
+ BigInteger productT = BigInteger.valueOf(1);
+ for (int i = 0; i < s.length(); i++)
+ {
+ productS = productS.multiply(BigInteger.valueOf(prime[s.charAt(i) - 'a']));
+ }
+ for (int i = 0; i < t.length(); i++)
+ {
+ productT = productT.multiply(BigInteger.valueOf(prime[t.charAt(i) - 'a']));
+ }
+ return productS.compareTo(productT) == 0;
+ }
+}
+```
+发现solution的第2个解法与我之前想的第2、3种很像,但它没有使用map来映射,而是直接使用arr[index] - 'a'的方法,巧妙的利用数组下标来映射字母并计数。提交后速度变得只有几ms了,提升的非常明显。这个方法的时间复杂是O(n)。
+### 收获
+通过5种解法的摸索和学习过程,有两点的收获:
+- 感觉把题目提交并成功,而且每一次都比上一次更优化的时候,这种反馈确实会上瘾,现在感觉每次空下来,都想刷道题爽一下了。
+- 在自己尝试了3种解法之后,开始搜寻和学习网上优质解法的时候,在看的时候,可以结合自己的思维过程,更好的体会到别人解法的思路,学习起来更有效率了,而且也更容易记忆。
+- 从解法六收获到的是,解法真的非常多,只要愿意思考。
+## LeetCode_1047_18
+### 题目
+给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
+
+在 S 上反复执行重复项删除操作,直到无法继续删除。
+
+在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
+
+示例:
+```
+输入:"abbaca"
+输出:"ca"
+解释:
+例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
+```
+### 解法一
+#### 思路
+因为是比较相邻的两个字符是否相等,而且是在删去重复地内容后,继续不断比较地过程,所以非常像栈的push和pop的过程,所以想到了用栈来解决这道题(其实主要是题目分类在栈分类里:D)
+#### 解题过程
+1. 用一个栈来进行对字符数组遍历过程中压栈和出栈的过程;
+2. 每次压栈之前都判断栈是否为空,或者栈顶字符是否与当前遍历元素相同;
+3. 如果不同且非空,就压栈;
+4. 否则就出栈,然后循环往复,直到遍历结束;
+5. 之后再遍历一下这个栈,拼出字符串后得到结果。
+```java
+class Solution {
+ public String removeDuplicates(String S) {
+ if (S == null || "".equals(S)) {
+ return S;
+ }
+
+ Stack stack = new Stack<>();
+ for (char c: S.toCharArray()) {
+ if (stack.isEmpty() || stack.peek() != c) {
+ stack.push(c);
+ } else {
+ stack.pop();
+ }
+ }
+
+ StringBuilder result = new StringBuilder();
+ for (Character c: stack) {
+ result.append(c);
+ }
+
+ return result.toString();
+ }
+}
+```
+提交结果后,耗时只打败了50%的人,可以继续优化。
+### 解法二
+#### 思路
+在国际站看到了一个使用双游标的解法,发现自己是被作业的分类限制了思考,其实题目的解法和26题比较相似。
+#### 解题过程
+1. 使用两个指针
+2. nonDuplicatesIndex游标用来指向保存没有被对对碰掉的元素所保存的位置
+3. i指针用来遍历整个数组
+4. nonDuplicatesIndex指针是从-1开始的,思路和我26题从1开始比较类似,区别在于最后new String的时候需不需要+1
+5. 然后遍历的时候就判断是否为-1,或者是否不相同
+6. 如果满足就++nonDuplicatesIndex,并将i遍历到的元素,写到nonDuplicatesIndex所指向的位置
+7. 否则就让nonDuplicatesIndex回退,这样就相当于两个一样的元素对对碰掉了
+```java
+class Solution {
+ public String removeDuplicates(String S) {
+ if (S == null || "".equals(S)) {
+ return S;
+ }
+
+ char[] cs = S.toCharArray();
+ int nonDuplicatesIndex = -1;
+ for (int i = 0; i < cs.length; i++) {
+ if (nonDuplicatesIndex == -1 || cs[nonDuplicatesIndex] != cs[i]) {
+ cs[++nonDuplicatesIndex] = cs[i];
+ } else {
+ nonDuplicatesIndex--;
+ }
+ }
+
+ return new String(cs, 0, nonDuplicatesIndex + 1);
+ }
+}
+```
+### 收获
+这道题的收获:
+1. 面对题目时候,通过形成的认知框架迅速解题固然很好,但是也不能让自己被框架限制住思维,否则就像这道题一样只能想到栈的方式(可能只是因为自己做的太少,框架太差了。。。)。
+2. 加深了双指针的使用方法。
+3. 熟悉了栈的相关类型的解题思路。
+## LeetCode_441_18
+### 题目
+你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
+
+给定一个数字 n,找出可形成完整阶梯行的总行数。
+
+n 是一个非负整数,并且在32位有符号整型的范围内。
+
+示例 1:
+```
+n = 5
+硬币可排列成以下几行:
+¤
+¤ ¤
+¤ ¤
+因为第三行不完整,所以返回2.
+```
+示例 2:
+```
+n = 8
+硬币可排列成以下几行:
+¤
+¤ ¤
+¤ ¤ ¤
+¤ ¤
+因为第四行不完整,所以返回3.
+```
+### 思路
+看到这题,看着一层层的往后推的图的样子,脑子里就直接想到了下面四段话:
+1. terminator
+2. process
+3. drill down
+4. restore
+
+然后就开始写:
+```java
+class Solution {
+ public int arrangeCoins(int n) {
+ return doJob(0, 0, n);
+ }
+
+ private int doJob(int level, int sum, int n) {
+ return sum + ++level > n ? --level : doJob(level, sum+level, n);
+ }
+}
+```
+满心等着ac,结果int类型溢出,导致虚拟机栈溢出。懵了。揉了揉脸,再看了下题目,差点抽自己耳光,最简单的数学题啊。。。**真的是手里拿着锤子,看什么都是钉子**。。。
+
+于是又满怀信心的敲出了如下的代码:
+```java
+class Solution {
+ public int arrangeCoins(int n) {
+ int i = 0;
+ for (int sum = 0; sum <= n;) {
+ sum = (1 + ++i) * i / 2;
+ }
+ return --i;
+ }
+}
+````
+结果超时。。。
+### 解法一
+#### 思路
+1. 其实明显不用从1开始算,直接使用求根公式就可以算出那个值
+2. (m + 0.5)^2 = 2 * n - 0.25 =》 m = sqrt(2 * n - 0.25) - 0.5
+3. 然后再求小于这个整数中最大的那个就可以了
+#### 解题过程
+```java
+class Solution {
+ public int arrangeCoins(int n) {
+ return (int)(Math.sqrt(n * 2 + 0.25) - 0.5);
+ }
+}
+```
+坑爹,结果又没注意整数类型溢出,又报错,那就只能先转成double了:
+```java
+class Solution {
+ public int arrangeCoins(int n) {
+ return (int)(Math.sqrt(n * 2.0 + 0.25) - 0.5);
+ }
+}
+```
+这样问题就解决了。但压根没用到作业分类的二分法啊,所以要继续找答案。
+### 解法二
+#### 思路
+在网上的留言讨论里找到了一个大开眼界的方法:
+1. 这个解法从两头来处理这个n
+2. 一头是从0开始,存做c,先用c和n作比较,如果小于n就c++
+3. 但不是应该用**总和**来和n作比较吗?而且为什么要先比较再++呢?
+4. 因为他另一头做的事情才是关键,他直接把c从n中减去
+5. 然后用c再和被减去c的n再进行比较
+6. 这样的原因是只要一直减去c,最终都会遇到最后一行作比较的时候,如果大于n了,那就说明n不够分配下一组+1的数了,c就是最后的行数。
+#### 解题过程
+理解了思路,做起来就方便了
+```java
+class Solution {
+ public int arrangeCoins(int n) {
+ int c = 0;
+ while(c < n) {
+ n -= ++c;
+ }
+ return c;
+ }
+}
+```
+但效果没有刚才直接使用求根公式的算法好。但是分类要求的二分查找是怎么玩的呢?
+### 收获
+1. 体会到数学是算法的基础,有时候直接拿数学简化公式以后,算起来会更快
+2. 感觉先好好想一下怎么解题的思路,等想清楚了再做题,做题的感觉反而更爽,不过思考的过程确实很累,结合之前不断试错修改的过程,两种方式都有锻炼自己的地方。
+## LeetCode_104_18
+### 题目
+给定一个二叉树,找出其最大深度。
+
+二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
+
+说明: 叶子节点是指没有子节点的节点。
+
+示例:
+```
+给定二叉树 [3,9,20,null,null,15,7],
+
+ 3
+ / \
+ 9 20
+ / \
+ 15 7
+返回它的最大深度 3 。
+```
+### 解法一
+#### 思路
+使用分治的思路
+1. terminator:root为null的时候,返回当前层数;
+2. prepare:层数加1;
+3. subproblems下钻到当前节点的左右子树;
+4. process & generate:比较左右子树返回的层数,向上返回最大值。
+#### 解题过程
+因为思路中涉及到了层数的概念,所以就需要新建一个函数,入参增加一个层数用来下钻的时候传递。
+```java
+class Solution {
+ public int maxDepth(TreeNode root) {
+ return doSearch(0, root);
+ }
+
+ private int doSearch(int level, TreeNode root) {
+ //terminator
+ if (root == null) {
+ return level;
+ }
+ //prepare
+ level++;
+ //subproblems & prepare & generate result
+ return Math.max(doSearch(level, root.left), doSearch(level, root.right));
+ }
+}
+```
+通过样板很快就写出了可以执行的代码,然后又再精简了一下
+```java
+public class LeetCode_104_18 {
+ public int maxDepth(TreeNode root) {
+ return doSearch(0, root);
+ }
+
+ private int doSearch(int level, TreeNode root) {
+ return root == null? level: Math.max(doSearch(++level, root.left), doSearch(level, root.right));
+ }
+}
+```
+### 收获
+具体题目和样板之间有时候并不能完全匹配,但是在做题的过程中能够体会到大致的思路是可以通过样板来指导的。通过做题进一步的熟练了分治的思考和代码的编写,写的速度比以前明显感觉快了。
+## LeetCode_189_18
+### 题目
+给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
+
+示例 1:
+```
+输入: [1,2,3,4,5,6,7] 和 k = 3
+输出: [5,6,7,1,2,3,4]
+解释:
+向右旋转 1 步: [7,1,2,3,4,5,6]
+向右旋转 2 步: [6,7,1,2,3,4,5]
+向右旋转 3 步: [5,6,7,1,2,3,4]
+```
+示例 2:
+```
+输入: [-1,-100,3,99] 和 k = 2
+输出: [3,99,-1,-100]
+解释:
+向右旋转 1 步: [99,-1,-100,3]
+向右旋转 2 步: [3,99,-1,-100]
+```
+### 思路
+首先想到的就是较为暴力的方法:
+1. 移动k次
+2. 每次移动一个数,时间复杂度是O(n)
+### 解法一
+```java
+class Solution {
+ public void rotate(int[] nums, int k) {
+ if (nums == null || nums.length == 0) {
+ return;
+ }
+
+ for (int i = 0; i < k; i++) {
+ doMove(nums);
+ }
+ }
+
+ private void doMove(int[] nums) {
+ int store = nums[0];
+ for (int i = 0; i < nums.length - 1; i++) {
+ int tmp = store;
+ store = nums[i + 1];
+ nums[i + 1] = tmp;
+ }
+ nums[0] = store;
+ }
+}
+```
+doMove函数负责定义每次移动的逻辑,代码中需要使用一个变量来保存需要移动的数。但这样的时间复杂度事O(n^2)
+### 解法二
+从上面的解法中可以发现:
+1. 解法一每次只走一部,重复走了k次,但其实可以一步到位,这样可以减少一层循环嵌套。
+ 1. 从第一个元素开始遍历整个数组
+ 2. 第i个元素会放在i+k的位置
+ 3. i+k的元素则保存下来,i+2k的位置
+ 4. 以此类推
+ 5. 但是会有一个特殊情况,就是在过界以后,会进行%的处理,如果处理结束后回到了第一个元素的位置,那么因为第一个位置的元素已经去到该去的位置了,所以就从第二个元素开始继续。
+ > 对于为什么回到第一个元素的位置后,就特殊处理为从后一个元素开始,没法很好的理解。
+2. 碰到如下情况则可以直接返回:
+ 1. 数组的长度小于等于1;
+ 2. k==0的时候;
+ 3. k是数组长度的整数倍。
+```java
+class Solution {
+ public void rotate(int[] nums, int k) {
+ if (k == 0 || nums == null || nums.length <= 1 || (k % nums.length == 0)) {
+ return;
+ }
+
+ int index = 0;
+ int start = 0;
+ int store = nums[index];
+ for (int i = 0; i < nums.length; i++) {
+ index = index + k >= nums.length ? (index + k) % nums.length: index + k;
+ int tmpStore = store;
+ store = nums[index];
+ nums[index] = tmpStore;
+ if (index == start) {
+ start = ++index;
+ store = nums[index];
+ }
+ }
+ }
+}
+```
+### 解法三
+这个解法是通过review同学代码学习到的:
+1. 先将数组头尾的交换,相当于进行倒序排列。
+2. 然后再以k为中心点,两边同时头尾交换的倒序排列
+3. 这样就直接实现了,非常巧妙
+4. 使用异或操作,实现原地的数值交换
+```java
+class Solution {
+ public void rotate(int[] nums, int k) {
+ if (k == 0 || nums == null || nums.length < 2 || k % nums.length == 0) {
+ return;
+ }
+
+ k %= nums.length;
+
+ swap(nums, 0, nums.length - 1);
+ swap(nums, 0, k - 1);
+ swap(nums, k, nums.length - 1);
+ }
+
+ private void swap(int[] nums, int head, int tail) {
+ while (head < tail) {
+ exchange(nums, head, tail);
+ head++;
+ tail--;
+ }
+ }
+
+ private void exchange(int[] nums, int x, int y) {
+ nums[x] ^= nums[y];
+ nums[y] ^= nums[x];
+ nums[x] ^= nums[y];
+ }
+}
+```
+### 收获
+从这道题中学到的:
+1. 前置条件检验的好,可以避免异常,同时能够提高效率。
+2. 解法二思考了很久,无论是看题解还是自己思考,都debug了很久才了解了大概的意思,自己禁不住要人肉在脑中算一下整个过程,否则没办法很好的理解。这一点不知道该怎么克服,但至少找到了一个问题,也算是收获。
+3. 解法三让我收获了异或的一种实用的使用方法,同时也了解一种非常巧妙地解题方式。
+## LeetCode_15_18
+### 题目
+给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
+
+注意:答案中不可以包含重复的三元组。
+```
+例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
+
+满足要求的三元组集合为:
+[
+ [-1, 0, 1],
+ [-1, -1, 2]
+]
+```
+### 思路
+1. 3个数的和为0,意味着1个数和另2个数互为相反数。
+2. 数组中的元素是重复的,而结果不能重复,所以可以先排序数组,从而方便遍历时候进行过滤
+3. 可以使用一个指针遍历数组,另两个指针代表剩余元素的头尾指针,或者是另两个数,从而计算它们的和
+### 解法一
+```java
+class Solution {
+ public List> threeSum(int[] nums) {
+ if (nums == null || nums.length < 3) {
+ return Collections.emptyList();
+ }
+
+ Arrays.sort(nums);
+ List> result = new ArrayList<>();
+
+ int pointer = 0, head, tail, target;
+ while (pointer < nums.length - 2) {
+ if (pointer > 0 && nums[pointer] == nums[pointer - 1]) {
+ pointer++;
+ continue;
+ }
+
+ head = pointer + 1;
+ tail = nums.length - 1;
+ target = -nums[pointer];
+
+ while (head < tail) {
+ if (nums[head] + nums[tail] == target) {
+ result.add(Arrays.asList(nums[pointer], nums[head], nums[tail]));
+
+ while (head < tail && nums[head] == nums[head + 1]) {
+ head++;
+ }
+
+ while (head < tail && nums[tail - 1] == nums[tail]) {
+ tail--;
+ }
+
+ head++;
+ tail--;
+ } else if (nums[head] + nums[tail] < target) {
+ head++;
+ } else {
+ tail--;
+ }
+ }
+
+ pointer++;
+ }
+
+ return result;
+ }
+}
+```
+解题过程中:
+1. return Collections.emptyList();是因为return null报错,要求的是空list。
+2. while (pointer < nums.length - 2) 是因为数组最后的2个元素不需要遍历,因为没有剩余的元素可以和它们相加计算,或者说,它们的可能性在之前已经检查过了。
+### 收获
+做这道题目时,脑中有一个大概的思路,但具体的代码是通过review其他同学的过程清晰出来的,review和看sol一样可以帮助自己快速的找到思路,再通过对代码的思考,解题的过程就能清晰了。总之review是一件很棒的事情。学习他人的代码和思路对提升自己非常有帮助。
+## LeetCode_21_18
+### 题目
+将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
+
+示例:
+```
+输入:1->2->4, 1->3->4
+输出:1->1->2->3->4->4
+```
+### 思路
+上来先想到的是循环,但循环的话应该是要嵌套的,而且感觉非常麻烦。在通过学习其他人的代码过程中,看到了使用递归的方式,思路如下:
+1. 比较两个节点的值谁小,小的那个放在新的链表中
+2. 然后把新增到新链表中的的节点的next节点(其实也就是两个链表中当前val较小的那个的后一个值)和另一个链表的比输的那个较大的节点再比较
+3. 循环往复
+4. 退出条件是某一个链表的节点比完了,也就说明,另一个链表剩下的节点一定比新增的链表的所有节点都大了,直接连在后面就可以了
+### 解法一
+```java
+class Solution {
+ private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
+ if (l1 == null || l2 == null)
+ {
+ return l1 == null ? l2 : l1;
+ }
+ ListNode temp = l1.val < l2.val ? l1 : l2;
+ temp.next = mergeTwoLists(temp.next, temp == l1 ? l2 : l1);
+ return temp;
+ }
+}
+```
+### 收获
+在理解这个递归解法的过程中,还是禁不住想去人肉递归一下,而且起始理解的时候也有些吃力,还是通过写下思路的方式,才能写出。还是需要多练习。
+## LeetCode_49_18
+### 题目
+给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
+
+示例:
+```
+输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
+输出:
+[
+ ["ate","eat","tea"],
+ ["nat","tan"],
+ ["bat"]
+]
+```
+说明:
+```
+所有输入均为小写字母。
+不考虑答案输出的顺序。
+```
+### 思路
+1. 将数组中的字符串打散,重新排序在转换回字符串,这样异位词就处理成了一样的,方便比较
+2. 将重新的排序的字符串作为key放入map中映射,value放一个list,如果一致就放入,否则新建一个list
+3. 时间复杂度是O(n)
+### 解法一
+```java
+class Solution {
+ public List> groupAnagrams(String[] strs) {
+ if (strs == null || strs.length == 0) {
+ return Collections.emptyList();
+ }
+
+ Map> map = new HashMap<>();
+ for (String s: strs) {
+ char[] cs = s.toCharArray();
+ Arrays.sort(cs);
+ String tmp = String.valueOf(cs);
+ if (!map.containsKey(tmp)) {
+ map.put(tmp, new ArrayList<>());
+ }
+ map.get(tmp).add(s);
+ }
+ return new ArrayList<>(map.values());
+ }
+}
+```
+### 收获
+先想再做,解题轻松。
+## LeetCode_1021_18
+### 题目
+有效括号字符串为空 ("")、"(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。例如,"","()","(())()" 和 "(()(()))" 都是有效的括号字符串。
+
+如果有效字符串 S 非空,且不存在将其拆分为 S = A+B 的方法,我们称其为原语(primitive),其中 A 和 B 都是非空有效括号字符串。
+
+给出一个非空有效字符串 S,考虑将其进行原语化分解,使得:S = P_1 + P_2 + ... + P_k,其中 P_i 是有效括号字符串原语。
+
+对 S 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 S 。
+
+示例 1:
+```
+输入:"(()())(())"
+输出:"()()()"
+解释:
+输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
+删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。
+```
+示例 2:
+```
+输入:"(()())(())(()(()))"
+输出:"()()()()(())"
+解释:
+输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))",
+删除每隔部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"。
+```
+示例 3:
+```
+输入:"()()"
+输出:""
+解释:
+输入字符串为 "()()",原语化分解得到 "()" + "()",
+删除每个部分中的最外层括号后得到 "" + "" = ""。
+```
+提示:
+```
+S.length <= 10000
+S[i] 为 "(" 或 ")"
+S 是一个有效括号字符串
+```
+### 思路
+1. 首先判定字符串中的括号组合哪些是有效的
+2. 从题目中的案例中找出一个规律,所有的组合,都是由两个右括号结尾
+3. 再进一步就是,如果左括号出现时+1,右括号出现时-1,那么所有有效的组合都是在计数为0的时候产生
+2. 选出来有效的括号组合以后再去掉外层的括号就可以了
+### 解题一
+```java
+class Solution {
+ public String removeOuterParentheses(String S) {
+ int count = 0, start = 1;
+ char[] cs = S.toCharArray();
+ StringBuilder sb = new StringBuilder();
+
+ for (int i = 0; i < cs.length; i++) {
+ count = cs[i] == '(' ? ++count : --count;
+ if (count == 0) {
+ sb.append(S, start, i);
+ start = i + 2;
+ }
+ }
+ return sb.toString();
+ }
+}
+```
+### 收获
+先想再做,解题轻松。
+## LeetCode_50_18
+### 题目
+实现 pow(x, n) ,即计算 x 的 n 次幂函数。
+
+示例 1:
+```
+输入: 2.00000, 10
+输出: 1024.00000
+```
+示例 2:
+```
+输入: 2.10000, 3
+输出: 9.26100
+```
+示例 3:
+```
+输入: 2.00000, -2
+输出: 0.25000
+解释: 2-2 = 1/22 = 1/4 = 0.25
+```
+说明:
+```
+-100.0 < x < 100.0
+n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。
+```
+### 思路
+第一步想到的还是递归以及4个模板单词
+1. 想好退出条件
+2. 不断的递归乘以之前返回的值
+3. 因为是y*=y,所以递归的时候需要直接n/2
+### 解法一
+```java
+class Solution {
+ public double myPow(double x, int n) {
+ if (n == 0) {
+ return 1;
+ }
+
+ if (n == 1) {
+ return x;
+ }
+
+ if (n == Integer.MIN_VALUE){
+ return 1.0 / myPow(x, -(n/2));
+ }
+
+ if (n < 0) {
+ return 1.0 / myPow(x, -n);
+ }
+
+
+ double y = myPow(x, n / 2);
+ y *= y;
+
+ return n % 2 == 1 ? y * x : y;
+ }
+}
+```
+解这道题目时,难点主要是在退出条件中:
+1. 为0的时候返回1
+2. 为1的时候返回本身
+3. 如果时负数,需要算-n的倒数
+4. 但是,如果是int的最小值,-n会导致int类型永远是最小值,进而导致栈溢出,需要先二分
+### 收获
+主要是提交的过程中,碰到各种解题时没有想到的特殊情况,也就是问题的界限没有确定好,需要在运行的时候不断地试错。
+## LeetCode_101_18
+### 题目
+给定一个二叉树,检查它是否是镜像对称的。
+
+例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
+```
+ 1
+ / \
+ 2 2
+ / \ / \
+3 4 4 3
+```
+但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
+```
+ 1
+ / \
+ 2 2
+ \ \
+ 3 3
+```
+### 思路
+镜像代表:
+1. 左子树的左子树等于右子树的右子树
+2. 左子树的右子树等于右子树的左子树
+3. 根节点相同
+### 解法一
+```java
+class Solution {
+ public boolean isSymmetric(TreeNode root) {
+ return doCheck(root, root);
+ }
+
+ private boolean doCheck(TreeNode left, TreeNode right) {
+ if (left == null && right == null) {
+ return true;
+ }
+
+ if (left == null || right == null) {
+ return false;
+ }
+
+ return (left.val == right.val) && doCheck(left.left, right.right) && doCheck(left.right, right.left);
+ }
+}
+```
+### 解法二
+#### 思路
+思路和解法一的递归类似,使用近似BFS的方式来解
+#### 代码
+```java
+class Solution {
+ public boolean isSymmetric(TreeNode root) {
+ Queue queue = new LinkedList<>();
+ queue.add(root);
+ queue.add(root);
+
+ while (!queue.isEmpty()) {
+ TreeNode left = queue.poll();
+ TreeNode right = queue.poll();
+
+ if (left == null && right == null) {
+ continue;
+ }
+
+ if (left == null || right == null) {
+ return false;
+ }
+
+ if (left.val != right.val) {
+ return false;
+ }
+
+ queue.add(left.left);
+ queue.add(right.right);
+ queue.add(left.right);
+ queue.add(right.left);
+ }
+ return true;
+ }
+}
+```
+### 解法三
+#### 思路
+和解法一一样的思路,使用stack实现,但是看网上说这个算dfs,我觉得不是啊。。。
+#### 代码
+```java
+class Solution {
+ public boolean isSymmetric(TreeNode root) {
+ Stack stack = new Stack<>();
+ stack.push(root);
+ stack.push(root);
+
+ while (!stack.isEmpty()) {
+ TreeNode left = stack.pop();
+ TreeNode right = stack.pop();
+
+ if (left == null && right == null) {
+ continue;
+ }
+
+ if (left == null || right == null) {
+ return false;
+ }
+
+ if (left.val != right.val) {
+ return false;
+ }
+
+ stack.push(left.left);
+ stack.push(right.right);
+ stack.push(left.right);
+ stack.push(right.left);
+ }
+ return true;
+ }
+}
+```
+### 收获
+对于BFS的用法有了进一步的理解和熟悉。
+## LeetCode_84_18
+### 题目
+给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
+
+求在该柱状图中,能够勾勒出来的矩形的最大面积。
+### 解法一
+#### 思路
+1. 使用一个栈来记录遍历数组时候记录到的相对高的方块
+2. 如果遇到比上一个低的方块,就把上一个出栈
+3. 它是整个栈加上遍历到的这个元素中最高的一个
+4. 使用这个方块的高度能够得到最大面积,就要乘上遍历到的位置减去栈顶那个方块的位置。
+5. 因为出栈的这个方块比已经出栈的都矮,但比栈中和遍历到的方块都高,所以从栈顶到遍历到的位置的宽度上,都可以放得下出栈的这个方块的大小
+6. 然后就一直出栈,直到比遍历到的这个方块矮为止。
+7. 继续遍历,循环往复。
+#### 代码
+
+### 收获
+
+
+
+
+Chao:非常好的总结。继续保持这样的方式进行训练和刷题下去。注意多看中文题解和国际站上的代码,进行代码优化!
\ No newline at end of file
diff --git "a/Week_01/id_18/\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204\345\255\246\344\271\240\350\204\221\345\233\276.mindmap.png" "b/Week_01/id_18/\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204\345\255\246\344\271\240\350\204\221\345\233\276.mindmap.png"
new file mode 100644
index 00000000..87ed691c
Binary files /dev/null and "b/Week_01/id_18/\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204\345\255\246\344\271\240\350\204\221\345\233\276.mindmap.png" differ
diff --git a/Week_01/id_19/LeetCode_189_19.java b/Week_01/id_19/LeetCode_189_19.java
new file mode 100644
index 00000000..0db3a2f8
--- /dev/null
+++ b/Week_01/id_19/LeetCode_189_19.java
@@ -0,0 +1,64 @@
+class Solution {
+ public void rotate(int[] nums, int k) {
+ if(k <= 0 || nums == null || nums.length == 0) return;
+ k = k % nums.length;
+ if(k == 0) return;
+ int[] temp = new int[k];
+ for(int i = nums.length-k,j=0;i < nums.length;i++,j++)
+ temp[j] = nums[i];
+ for(int i = nums.length-k-1,j = nums.length-1;i >= 0;i--,j--)
+ nums[j] = nums[i];
+ for(int i = 0,j = 0; i < k; i++,j++)
+ nums[i]=temp[j];
+ }
+}
+
+public class MainClass {
+ public static int[] stringToIntegerArray(String input) {
+ input = input.trim();
+ input = input.substring(1, input.length() - 1);
+ if (input.length() == 0) {
+ return new int[0];
+ }
+
+ String[] parts = input.split(",");
+ int[] output = new int[parts.length];
+ for(int index = 0; index < parts.length; index++) {
+ String part = parts[index].trim();
+ output[index] = Integer.parseInt(part);
+ }
+ return output;
+ }
+
+ public static String integerArrayToString(int[] nums, int length) {
+ if (length == 0) {
+ return "[]";
+ }
+
+ String result = "";
+ for(int index = 0; index < length; index++) {
+ int number = nums[index];
+ result += Integer.toString(number) + ", ";
+ }
+ return "[" + result.substring(0, result.length() - 2) + "]";
+ }
+
+ public static String integerArrayToString(int[] nums) {
+ return integerArrayToString(nums, nums.length);
+ }
+
+ public static void main(String[] args) throws IOException {
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ String line;
+ while ((line = in.readLine()) != null) {
+ int[] nums = stringToIntegerArray(line);
+ line = in.readLine();
+ int k = Integer.parseInt(line);
+
+ new Solution().rotate(nums, k);
+ String out = integerArrayToString(nums);
+
+ System.out.print(out);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_19/LeetCode_26_19.java b/Week_01/id_19/LeetCode_26_19.java
new file mode 100644
index 00000000..037cb9de
--- /dev/null
+++ b/Week_01/id_19/LeetCode_26_19.java
@@ -0,0 +1,59 @@
+class Solution {
+ public int removeDuplicates(int[] nums) {
+ if(nums==null || nums.length==0) return 0;
+ int left=0,right=0;
+ while((++right) stack = new ArrayDeque<>();
+ Deque innerStack = new ArrayDeque<>();
+ stack.push(root);
+ while (true) {
+ if (stack.isEmpty())
+ break;
+ while (!stack.isEmpty()) {
+ TreeNode cur = stack.pop();
+ if (cur.left != null)
+ innerStack.add(cur.left);
+ if (cur.right != null)
+ innerStack.add(cur.right);
+ }
+ if (innerStack.size() % 2 != 0)
+ return false;
+ while (!innerStack.isEmpty()) {
+ if (innerStack.peekFirst().val != innerStack.peekLast().val)
+ return false;
+ else {
+ stack.addFirst(innerStack.removeFirst());
+ stack.addLast(innerStack.removeLast());
+ }
+ }
+ }
+ return true;
+ }
+
+
+ /**
+ * Have referenced from solution
+ *
+ * @param root
+ * @return
+ */
+ public boolean isSymmetricByIteration(TreeNode root) {
+ Queue queue = new LinkedList();
+ queue.add(root);
+ queue.add(root);
+ while (!queue.isEmpty()) {
+ TreeNode cur = queue.poll();
+ TreeNode mirrCur = queue.poll();
+ if (cur == null && mirrCur == null)
+ continue;
+ if (cur == null || mirrCur == null)
+ return false;
+ if (cur.val != mirrCur.val)
+ return false;
+ queue.add(cur.left);
+ queue.add(mirrCur.right);
+ queue.add(cur.right);
+ queue.add(mirrCur.left);
+ }
+ return true;
+ }
+
+ /**
+ * Use traverse to judge whether the tree is symmetric
+ *
+ * @param root
+ * @return
+ */
+ public boolean isSymmetric(TreeNode root) {
+ if (root == null)
+ return false;
+ return isSymmetricByTraverse(root.left, root.right);
+ }
+
+ public boolean isSymmetricByTraverse(TreeNode cur, TreeNode mirrCur) {
+ if (cur == null || mirrCur == null) {
+ if (mirrCur != null || cur != null)
+ return false;
+ else
+ return true;
+ }
+ return (cur.val == mirrCur.val) && isSymmetricByTraverse(cur.left, mirrCur.right)
+ && isSymmetricByTraverse(cur.right, mirrCur.left);
+ }
+
+ public static void test1() {
+ TreeNode root = new TreeNode(1);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(2);
+ root.left.left = new TreeNode(3);
+ root.left.right = new TreeNode(4);
+ root.right.left = new TreeNode(4);
+ root.right.right = new TreeNode(3);
+ LeetCode101_2 s = new LeetCode101_2();
+ System.out.println(s.isSymmetric(root));
+ }
+
+ public static void test2() {
+ TreeNode root = new TreeNode(2);
+ root.left = new TreeNode(3);
+ root.right = new TreeNode(3);
+ root.left.left = new TreeNode(4);
+ root.left.right = new TreeNode(5);
+ root.right.left = new TreeNode(5);
+ LeetCode101_2 s = new LeetCode101_2();
+ System.out.println(s.isSymmetric(root));
+ }
+
+ public static void main(String[] args) {
+ test2();
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_104_2.java b/Week_01/id_2/LeetCode_104_2.java
new file mode 100644
index 00000000..526c284f
--- /dev/null
+++ b/Week_01/id_2/LeetCode_104_2.java
@@ -0,0 +1,26 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+/**
+ * Given a binary tree, find its maximum depth.
+ *
+ * The maximum depth is the number of nodes along the longest path from the root
+ * node down to the farthest leaf node.
+ *
+ * Note: A leaf is a node with no children.
+ *
+ * @author liliangzi
+ *
+ */
+public class LeetCode_104_2 {
+
+ public int maxDepth(TreeNode root) {
+ if (root == null)
+ return 0;
+ return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
+ }
+
+ public static void main(String[] args) {
+
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_111_2.java b/Week_01/id_2/LeetCode_111_2.java
new file mode 100644
index 00000000..ba2a1c8f
--- /dev/null
+++ b/Week_01/id_2/LeetCode_111_2.java
@@ -0,0 +1,36 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+/**
+ * Given a binary tree, find its minimum depth.
+ *
+ * The minimum depth is the number of nodes along the shortest path from the
+ * root node down to the nearest leaf node.
+ *
+ * Note: A leaf is a node with no children.
+ *
+ * @author liliangzi
+ *
+ */
+
+
+
+public class LeetCode_111_2 {
+
+ public int minDepth(TreeNode root) {
+ if (root == null)
+ return 0;
+ int left = minDepth(root.left);
+ int right = minDepth(root.right);
+
+
+ // 简化代码,用三目运算符
+ if (right == 0)
+ return left + 1;
+ if (left == 0)
+ return right + 1;
+ return Math.min(left, right) + 1;
+ }
+
+
+
+}
diff --git a/Week_01/id_2/LeetCode_15_2.java b/Week_01/id_2/LeetCode_15_2.java
new file mode 100644
index 00000000..74af2cc2
--- /dev/null
+++ b/Week_01/id_2/LeetCode_15_2.java
@@ -0,0 +1,111 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * Given an array nums of n integers, are there elements a, b, c in nums such
+ * that a + b + c = 0? Find all unique triplets in the array which gives the sum
+ * of zero.
+ *
+ * Note:
+ *
+ * The solution set must not contain duplicate triplets.
+ *
+ * Given array nums = [-1, 0, 1, 2, -1, -4],
+ *
+ * A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
+ *
+ * @author liliangzi
+ *
+ */
+public class LeetCode_15_2 {
+
+ /**
+ * Output limit exceeded brutal force
+ *
+ * @param nums
+ * @return
+ */
+
+ // 很好!!!
+
+ public List> threeSumExceedOutputLimit(int[] nums) {
+ List> list = new ArrayList>();
+ for (int i = 0; i < nums.length; i++) {
+ for (int j = i + 1; j < nums.length; j++) {
+ for (int k = j + 1; k < nums.length; k++) {
+ if (nums[k] == -(nums[i] + nums[j]))
+ list.add(new ArrayList(Arrays.asList(nums[i], nums[j], nums[k])));
+ }
+ }
+ }
+ return list;
+ }
+
+ /**
+ * have referenced discussion
+ *
+ * @param nums
+ * @return
+ */
+ public List> threeSum(int[] nums) {
+ List> list = new ArrayList>();
+ Arrays.sort(nums);
+ if ( nums.length > 0 && nums[0] > 0)
+ return list;
+ int lowPtr = 0, highPtr = 0;
+ for (int i = 0; i < nums.length - 2; i++) {
+ if (i == 0 || nums[i] != nums[i - 1]) {
+ int val = -nums[i];
+ lowPtr = i + 1;
+ highPtr = nums.length - 1;
+ while (lowPtr < highPtr) {
+ if (nums[lowPtr] + nums[highPtr] == val) {
+ list.add(new ArrayList(Arrays.asList(nums[i], nums[lowPtr], nums[highPtr])));
+ while (lowPtr < highPtr && nums[lowPtr] == nums[lowPtr + 1])
+ lowPtr++;
+ while (lowPtr < highPtr && nums[highPtr] == nums[highPtr - 1])
+ highPtr--;
+ lowPtr++;
+ highPtr--;
+ } else if (nums[lowPtr] + nums[highPtr] < val) {
+ while (lowPtr < highPtr && nums[lowPtr] == nums[lowPtr + 1])
+ lowPtr++;
+ lowPtr++;
+ } else {
+ while (lowPtr < highPtr && nums[highPtr] == nums[highPtr - 1])
+ highPtr--;
+ highPtr--;
+ }
+ }
+ }
+ }
+ return list;
+ }
+
+ static Random random = new Random();
+
+ public static void main(String[] args) {
+ int n = random.nextInt(15) + 1;
+ int[] nums = new int[n];
+ for (int i = 0; i < n; i++) {
+ if (i % 2 == 0)
+ nums[i] = -random.nextInt(15);
+ else
+ nums[i] = random.nextInt(15);
+ }
+ LeetCode_15_2 threeSum = new LeetCode_15_2();
+ int[] nums1 = { -7, 4, -5, 12, -5, 14, -12, 10, -4, 12, -9, 0, -9 };
+ int[] nums2 = { 0, 0, 0 };
+ for (int i = 0; i < nums1.length; i++) {
+ System.out.print(nums1[i] + " ");
+ }
+ System.out.println();
+ List> list = threeSum.threeSum(nums2);
+ System.out.println(list);
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_235_2.java b/Week_01/id_2/LeetCode_235_2.java
new file mode 100644
index 00000000..ad6d9eca
--- /dev/null
+++ b/Week_01/id_2/LeetCode_235_2.java
@@ -0,0 +1,98 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Given a binary search tree, find the lowest common ancestor (LCA) of two
+ * given nodes in the bst.
+ *
+ * @author liliangzi
+ *
+ *
+ * Definition for a binary tree node. public class TreeNode { int val;
+ * TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
+ */
+public class LeetCode_235_2 {
+
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
+ List pAncestors = new ArrayList();
+ List qAncestors = new ArrayList();
+ pAncestors.add(root);
+ qAncestors.add(root);
+ getAncestors(root, p, pAncestors);
+ getAncestors(root, q, qAncestors);
+ TreeNode lca = null;
+ for (int i = pAncestors.size() - 1; i >= 0; i--) {
+ lca = pAncestors.get(i);
+ if (qAncestors.contains(lca)) {
+ return lca;
+ }
+ }
+ return null;
+ }
+
+ public TreeNode lowestCommonAncestorByIteration(TreeNode root, TreeNode p, TreeNode q) {
+ while (root != null && root.val != p.val && root.val != q.val) {
+ if (p.val <= root.val && q.val <= root.val)
+ root = root.left;
+ else if (p.val >= root.val && q.val >= root.val)
+ root = root.right;
+ else
+ return root;
+ }
+ return root;
+ }
+
+ public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
+
+ // Chao: 如果有相应的return在if语句里的时候,建议能直接不需要else的分支。
+
+ if (root.val == p.val || root.val == q.val)
+ return root;
+ if ((p.val <= root.val && q.val >= root.val) || (p.val >= root.val && q.val <= root.val)) {
+ return root;
+ } else {
+ if (root.left != null && p.val <= root.val && q.val <= root.val) {
+ return lowestCommonAncestor2(root.left, p, q);
+ }
+ if (root.right != null && p.val >= root.val && q.val >= root.val) {
+ return lowestCommonAncestor2(root.right, p, q);
+ }
+ }
+ return root;
+ }
+
+ public void getAncestors(TreeNode root, TreeNode node, List ancestors) {
+ if (root == node) {
+ return;
+ } else {
+ if (root.left != null && node.val < root.val) {
+ ancestors.add(root.left);
+ getAncestors(root.left, node, ancestors);
+ }
+ if (root.right != null && node.val > root.val) {
+ ancestors.add(root.right);
+ getAncestors(root.right, node, ancestors);
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ TreeNode root = new TreeNode(6);
+ TreeNode p = new TreeNode(2);
+ root.left = p;
+ root.right = new TreeNode(8);
+ root.left.left = new TreeNode(0);
+ root.left.right = new TreeNode(4);
+ root.left.right.left = new TreeNode(3);
+ TreeNode q = new TreeNode(5);
+ root.left.right.right = q;
+ root.right.left = new TreeNode(7);
+ root.right.right = new TreeNode(9);
+ LeetCode_235_2 lca = new LeetCode_235_2();
+ TreeNode lc = lca.lowestCommonAncestor2(root, p, q);
+ System.out.println(lc.val);
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_236_2.java b/Week_01/id_2/LeetCode_236_2.java
new file mode 100644
index 00000000..184caf06
--- /dev/null
+++ b/Week_01/id_2/LeetCode_236_2.java
@@ -0,0 +1,175 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Definition for a binary tree node. public class TreeNode { int val; TreeNode
+ * left; TreeNode right; TreeNode(int x) { val = x; } }
+ *
+ *
+ */
+
+public class LeetCode_236_2 {
+
+ private TreeNode lca;
+
+ public LeetCode_236_2() {
+ this.lca = null;
+ }
+
+ /**
+ * Have referenced from solution (use iteration). The thought is as similar as
+ * myNeedRefact method but more efficient.
+ *
+ * @param root
+ * @param p
+ * @param q
+ * @return
+ */
+ public TreeNode lowestCommonAncestorByIteration(TreeNode root, TreeNode p, TreeNode q) {
+ Deque stack = new ArrayDeque();
+ Map parentMap = new HashMap();
+ parentMap.put(root, null);
+ stack.push(root);
+
+ while (!((parentMap.containsKey(p)) && (parentMap.containsKey(q)))) {
+ if (stack.isEmpty())
+ break;
+ TreeNode cur = stack.pop();
+ if (cur.left != null) {
+ parentMap.put(cur.left, cur);
+ stack.push(cur.left);
+ }
+ if (cur.right != null) {
+ parentMap.put(cur.right, cur);
+ stack.push(cur.right);
+ }
+ }
+
+ Set pAncestors = new HashSet();
+
+ while ( p!=null ) {
+ pAncestors.add(p);
+ p = parentMap.get(p);
+ }
+
+ while ( !pAncestors.contains(q)) {
+ q = parentMap.get(q);
+ }
+ return q;
+ }
+
+ /**
+ * Have refereced from solution (use Backtrack)
+ *
+ * @param root
+ * @param p
+ * @param q
+ * @return
+ */
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
+ traverse(root, p, q);
+
+ return this.lca;
+ }
+
+ public boolean traverse(TreeNode currentNode, TreeNode p, TreeNode q) {
+ if (currentNode == null) {
+ return false;
+ }
+ int left = traverse(currentNode.left, p, q) ? 1 : 0;
+ int right = traverse(currentNode.right, p, q) ? 1 : 0;
+ int mid = ((currentNode == p) || (currentNode == q)) ? 1 : 0;
+ if (left + right + mid >= 2)
+ this.lca = currentNode;
+ return (left + right + mid) > 0;
+
+ }
+
+ /**
+ * Time limit exceed.
+ *
+ * @param root
+ * @param p
+ * @param q
+ * @return
+ */
+ public TreeNode lowestCommonAncestorNeedToRefact(TreeNode root, TreeNode p, TreeNode q) {
+ Map pAncestors = new HashMap();
+ Map qAncestors = new HashMap();
+ pAncestors.put(root, null);
+ getLowestCommonAncestorByTraverseNeedToRefact(root, p, pAncestors);
+ getLowestCommonAncestorByTraverseNeedToRefact(root, q, qAncestors);
+ TreeNode lca = p;
+ TreeNode qAncestor = q;
+ while (lca != root) {
+ while (qAncestors.get(qAncestor) != root) {
+ if (qAncestors.get(qAncestor) == lca)
+ return lca;
+ qAncestor = qAncestors.get(qAncestor);
+ }
+ lca = pAncestors.get(lca);
+ }
+ return root;
+ }
+
+ /**
+ * Time limit exceed
+ *
+ * @param root
+ * @param targetNode
+ * @param map
+ */
+ public void getLowestCommonAncestorByTraverseNeedToRefact(TreeNode root, TreeNode targetNode,
+ Map map) {
+ if (root == targetNode) {
+ return;
+ }
+ if (root.left != null) {
+ map.put(root.left, root);
+ getLowestCommonAncestorByTraverseNeedToRefact(root.left, targetNode, map);
+ }
+ if (root.right != null) {
+ map.put(root.right, root);
+ getLowestCommonAncestorByTraverseNeedToRefact(root.right, targetNode, map);
+ }
+
+ }
+
+ public static void test1() {
+ TreeNode root = new TreeNode(3);
+ TreeNode p = new TreeNode(5);
+ root.left = p;
+ root.right = new TreeNode(1);
+ root.left.left = new TreeNode(6);
+ root.left.right = new TreeNode(2);
+ root.left.right.left = new TreeNode(7);
+ TreeNode q = new TreeNode(4);
+ root.left.right.right = q;
+ root.right.left = new TreeNode(0);
+ root.right.right = new TreeNode(8);
+ LeetCode_236_2 lca = new LeetCode_236_2();
+ TreeNode lc = lca.lowestCommonAncestorByIteration(root, p, q);
+ System.out.println(lc.val);
+ }
+
+ public static void test2() {
+ TreeNode root = new TreeNode(1);
+ TreeNode p = root;
+ TreeNode q = new TreeNode(2);
+ LeetCode_236_2 lca = new LeetCode_236_2();
+ TreeNode lc = lca.lowestCommonAncestorByIteration(root, p, q);
+ System.out.println(lc.val);
+ }
+
+ public static void main(String[] args) {
+ test1();
+ test2();
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_24_2.java b/Week_01/id_2/LeetCode_24_2.java
new file mode 100644
index 00000000..5c3337d8
--- /dev/null
+++ b/Week_01/id_2/LeetCode_24_2.java
@@ -0,0 +1,81 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+public class LeetCode_24_2 {
+
+ /**
+ * Given a linked list, swap every two adjacent nodes and return its head.
+ *
+ * You may not modify the values in the list's nodes, only nodes itself may be
+ * changed.
+ *
+ * Given 1->2->3->4, you should return the list as 2->1->4->3.
+ *
+ * @author liliangzi
+ *
+ */
+
+ class ListNode {
+ int val;
+ ListNode next;
+
+ ListNode(int x) {
+ val = x;
+ }
+ }
+
+ /**
+ * need to refact
+ * @param head
+ * @return
+ */
+ public ListNode swapPairs(ListNode head) {
+ if (head == null)
+ return head;
+ ListNode tempNext = null;
+ if ((tempNext = head.next) != null) {
+ ListNode tempNextNext = tempNext.next;
+ ListNode newHead = tempNext;
+ ListNode last = head;
+ newHead.next = head;
+ while (tempNextNext != null) {
+ tempNext = tempNextNext;
+ head = tempNext.next;
+ if (head != null)
+ tempNextNext = head.next;
+ else {
+ last.next = tempNext;
+ tempNext.next = null;
+ return newHead;
+ }
+ head.next = tempNext;
+ last.next = head;
+ last = tempNext;
+ }
+ last.next = null;
+ return newHead;
+ } else {
+ return head;
+ }
+ }
+
+ public static void print(ListNode node) {
+ while (node != null) {
+ System.out.print(node.val + "->");
+ node = node.next;
+ }
+ }
+
+ public static void test() {
+ LeetCode_24_2 s = new LeetCode_24_2();
+ LeetCode_24_2.ListNode head = s.new ListNode(1);
+ // head.next = s.new ListNode(2);
+ // head.next.next = s.new ListNode(3);
+ // head.next.next.next = s.new ListNode(4);
+ print(s.swapPairs(head));
+ }
+
+ public static void main(String[] args) {
+ test();
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_49_2.java b/Week_01/id_2/LeetCode_49_2.java
new file mode 100644
index 00000000..5c69a5eb
--- /dev/null
+++ b/Week_01/id_2/LeetCode_49_2.java
@@ -0,0 +1,41 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+public class LeetCode_49_2 {
+
+
+ public static List> groupAnagrams(String[] strs) {
+ List> list = new ArrayList>();
+ Map> mapList = new HashMap<>();
+ List subList = new ArrayList<>();
+ char[] temp;
+ for (int i = 0; i < strs.length; i++) {
+ temp = strs[i].toCharArray();
+ Arrays.sort(temp);
+ int hash = String.valueOf(temp).hashCode();
+ if ((subList = mapList.get(hash)) == null) {
+ subList = new ArrayList<>();
+ }
+ subList.add(strs[i]);
+ mapList.put(hash, subList);
+ }
+ Iterator>> it = mapList.entrySet().iterator();
+ while (it.hasNext()) {
+ list.add(it.next().getValue());
+ }
+ return list;
+ }
+
+ public static void main(String[] args) {
+ String[] strs = { "eat", "tea", "tan", "ate", "nat", "bat" };
+ List> list = groupAnagrams(strs);
+ System.out.println(list);
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_50_2.java b/Week_01/id_2/LeetCode_50_2.java
new file mode 100644
index 00000000..e9123a6e
--- /dev/null
+++ b/Week_01/id_2/LeetCode_50_2.java
@@ -0,0 +1,25 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+public class LeetCode_50_2 {
+
+ public double myPow(double x, int n) {
+ if (n < 0) {
+ if (n == Integer.MIN_VALUE)
+ return 1 / (myPow(x, Integer.MAX_VALUE) * x);
+ return 1 / myPow(x, -n);
+ }
+ if (n == 0)
+ return 1;
+ if (n == 1)
+ return x;
+
+ double temp = myPow(x, n / 2);
+ return n % 2 == 0 ? temp * temp : x * temp * temp;
+ }
+
+ public static void main(String[] args) {
+ LeetCode_50_2 p = new LeetCode_50_2();
+ System.out.println(p.myPow(2.1, 3));
+ }
+
+}
diff --git a/Week_01/id_2/LeetCode_783_2.java b/Week_01/id_2/LeetCode_783_2.java
new file mode 100644
index 00000000..1b55f3d6
--- /dev/null
+++ b/Week_01/id_2/LeetCode_783_2.java
@@ -0,0 +1,64 @@
+package com.llz.algorithm.algorithm2019.firstweek;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class LeetCode_783_2 {
+
+ /**
+ * Brutal force
+ * Haven't considered the given tree is BST.
+ * @param root
+ * @return
+ */
+ public int minDiffInBSTByBF(TreeNode root) {
+ List list = new ArrayList();
+ int minDist = root.val;
+ dfs(root, list);
+ for (int i = 0; i < list.size(); i++) {
+ for (int j = i + 1; j < list.size(); j++) {
+ if (Math.abs(list.get(i) - list.get(j)) < minDist)
+ minDist = Math.abs(list.get(i) - list.get(j));
+ }
+ }
+ return minDist;
+ }
+
+ public void dfs(TreeNode root, List list) {
+ if (root == null)
+ return;
+ if (list.contains(root.val))
+ return;
+ list.add(root.val);
+ dfs(root.left, list);
+ dfs(root.right, list);
+ }
+
+ Integer res = Integer.MAX_VALUE, pre = null;
+
+ public int minDiffInBST(TreeNode root) {
+ if (root.left != null)
+ minDiffInBST(root.left);
+ if (pre != null)
+ res = Math.min(res, root.val - pre);
+ pre = root.val;
+ if (root.right != null)
+ minDiffInBST(root.right);
+ return res;
+ }
+
+ public static void test() {
+ LeetCode_783_2 m = new LeetCode_783_2();
+ TreeNode root = new TreeNode(4);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(6);
+ root.left.left = new TreeNode(1);
+ root.left.right = new TreeNode(3);
+ System.out.println(m.minDiffInBST(root));
+ }
+
+ public static void main(String[] args) {
+ test();
+ }
+
+}
diff --git a/Week_01/id_2/NOTE.md b/Week_01/id_2/NOTE.md
index 107ea7d6..0482a11c 100644
--- a/Week_01/id_2/NOTE.md
+++ b/Week_01/id_2/NOTE.md
@@ -1 +1,4 @@
# 学习笔记
+
+
+Chao: 代码总体不错,没看到明显问题。 继续保持!
\ No newline at end of file
diff --git a/Week_01/id_20/leetcode_189_20.js b/Week_01/id_20/leetcode_189_20.js
new file mode 100644
index 00000000..e7014b49
--- /dev/null
+++ b/Week_01/id_20/leetcode_189_20.js
@@ -0,0 +1,21 @@
+/**
+ * @param {number[]} nums
+ * @param {number} k
+ * @return {void} Do not return anything, modify nums in-place instead.
+
+ 暴力解法 先删除 再添加 元素 循环操作
+ */
+var rotate = function(nums, k) {
+ var len = nums.length;
+ if(len<2){
+ return;
+ }
+ if(k<0){
+ return;
+ }
+ while(k--){
+ nums.unshift(nums.pop());
+ }
+ return nums;
+
+};
diff --git a/Week_01/id_20/leetcode_21_20.js b/Week_01/id_20/leetcode_21_20.js
new file mode 100644
index 00000000..3e5c222d
--- /dev/null
+++ b/Week_01/id_20/leetcode_21_20.js
@@ -0,0 +1,44 @@
+
+ var mergeTwoLists = function (l1, l2) {
+ let pHead = null,
+ pHead1 = l1,
+ pHead2 = l2,
+ p1 = l1,
+ p2 = l2,
+ p = null;
+ if (!l1) {
+ return l2;
+ } else if (!l2) {
+ return l1;
+ }
+
+ if (l1.val < l2.val) {
+ pHead = p1;
+ p = p1;
+ p1 = p1.next;
+ } else {
+ pHead = p2;
+ p = p2;
+ p2 = p2.next;
+ }
+
+ while (p1 && p2) {
+ if (p1.val < p2.val) {
+ p.next = p1;
+ p = p.next;
+ p1 = p1.next;
+ } else {
+ p.next = p2;
+ p = p.next;
+ p2 = p2.next;
+ }
+ }
+
+ if (p1) {
+ p.next = p1;
+ } else if (p2) {
+ p.next = p2;
+ }
+
+ return pHead;
+};
diff --git a/Week_01/id_22/LeetCode_1047_022.js b/Week_01/id_22/LeetCode_1047_022.js
new file mode 100644
index 00000000..0a5376ec
--- /dev/null
+++ b/Week_01/id_22/LeetCode_1047_022.js
@@ -0,0 +1,21 @@
+/*
+ * @lc app=leetcode.cn id=1047 lang=javascript
+ *
+ * [1047] 删除字符串中的所有相邻重复项
+ */
+/**
+ * @param {string} S
+ * @return {string}
+ */
+var removeDuplicates = function (S) {
+ let stack = [];
+ for (let i = 0; i < S.length; i++) {
+ if (S[i] === stack[stack.length - 1]) {
+ stack.pop();
+ } else {
+ stack.push(S[i])
+ };
+ }
+ return stack.join('');
+};
+
diff --git a/Week_01/id_22/LeetCode_104_022.js b/Week_01/id_22/LeetCode_104_022.js
new file mode 100644
index 00000000..46d50277
--- /dev/null
+++ b/Week_01/id_22/LeetCode_104_022.js
@@ -0,0 +1,26 @@
+/*
+ * @lc app=leetcode.cn id=104 lang=javascript
+ *
+ * [104] 二叉树的最大深度
+ */
+/**
+ * Definition for a binary tree node.
+ * function TreeNode(val) {
+ * this.val = val;
+ * this.left = this.right = null;
+ * }
+ */
+/**
+ * @param {TreeNode} root
+ * @return {number}
+ */
+var maxDepth = function (root) {
+ if (root == null) {
+ return 0;
+ } else {
+ let left_height = maxDepth(root.left),
+ right_height = maxDepth(root.right);
+ return Math.max(left_height, right_height) + 1;
+ }
+};
+
diff --git a/Week_01/id_22/LeetCode_189_022.js b/Week_01/id_22/LeetCode_189_022.js
new file mode 100644
index 00000000..1f7e8033
--- /dev/null
+++ b/Week_01/id_22/LeetCode_189_022.js
@@ -0,0 +1,16 @@
+/*
+ * @lc app=leetcode.cn id=189 lang=javascript
+ *
+ * [189] 旋转数组
+ */
+/**
+ * @param {number[]} nums
+ * @param {number} k
+ * @return {void} Do not return anything, modify nums in-place instead.
+ */
+var rotate = function (nums, k) {
+ while (k--) {
+ nums.unshift(nums.pop())
+ }
+};
+
diff --git a/Week_01/id_22/LeetCode_242_022.js b/Week_01/id_22/LeetCode_242_022.js
new file mode 100644
index 00000000..6dee1754
--- /dev/null
+++ b/Week_01/id_22/LeetCode_242_022.js
@@ -0,0 +1,36 @@
+/*
+ * @lc app=leetcode.cn id=242 lang=javascript
+ *
+ * [242] 有效的字母异位词
+ */
+/**
+ * @param {string} s
+ * @param {string} t
+ * @return {boolean}
+ */
+var isAnagram = function (s, t) {
+ let json = {}
+ for (var i = 0; i < s.length; i++) {
+ json[s[i]] ? json[s[i]]++ : json[s[i]] = 1
+ }
+ for (var i = 0; i < t.length; i++) {
+ if (json[t[i]]) {
+ json[t[i]]--
+ } else {
+ return false
+ }
+ }
+ let arr = Object.keys(json),
+ len = arr.length;
+ for (var i = 0; i < len; i++) {
+ if (json[arr[i]] !== 0) {
+ return false
+ } else {
+ if (i === len - 1) {
+ return true
+ }
+ }
+ }
+ return true
+};
+
diff --git a/Week_01/id_22/LeetCode_26_022.js b/Week_01/id_22/LeetCode_26_022.js
new file mode 100644
index 00000000..ed2a0adb
--- /dev/null
+++ b/Week_01/id_22/LeetCode_26_022.js
@@ -0,0 +1,22 @@
+/*
+ * @lc app=leetcode.cn id=26 lang=javascript
+ *
+ * [26] 删除排序数组中的重复项
+ */
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var removeDuplicates = function (nums) {
+ let i = 0,
+ k = nums.length;
+ for (let j = 1; j < k; j++) {
+ if (nums[j] !== nums[i]) {
+ i++
+ nums[i] = nums[j]
+ }
+ }
+ nums.splice(i + 1);
+ return k
+};
+
diff --git a/Week_01/id_22/LeetCode_441_022.js b/Week_01/id_22/LeetCode_441_022.js
new file mode 100644
index 00000000..9c816b76
--- /dev/null
+++ b/Week_01/id_22/LeetCode_441_022.js
@@ -0,0 +1,25 @@
+/*
+ * @lc app=leetcode.cn id=441 lang=javascript
+ *
+ * [441] 排列硬币
+ */
+/**
+ * @param {number} n
+ * @return {number}
+ */
+var arrangeCoins = function (n) {
+ if (n <= 1) {
+ return n
+ }
+ let k = 1;
+ while (n > 0) {
+ n -= k;
+ k++;
+ }
+ if (n == 0) {
+ return k - 1;
+ } else {
+ return k - 2;
+ }
+};
+
diff --git "a/Week_01/id_22/\346\225\260\346\215\256\347\273\223\346\236\204&\347\256\227\346\263\225.pdf" "b/Week_01/id_22/\346\225\260\346\215\256\347\273\223\346\236\204&\347\256\227\346\263\225.pdf"
new file mode 100644
index 00000000..249f61e4
Binary files /dev/null and "b/Week_01/id_22/\346\225\260\346\215\256\347\273\223\346\236\204&\347\256\227\346\263\225.pdf" differ
diff --git a/Week_01/id_23/LeetCode_26_23 b/Week_01/id_23/LeetCode_26_23
new file mode 100644
index 00000000..a19785f9
--- /dev/null
+++ b/Week_01/id_23/LeetCode_26_23
@@ -0,0 +1,13 @@
+class Solution {
+ public int removeDuplicates(int[] nums) {
+ if (nums.length == 0 ) return 0;
+ int i = 0;
+ for(int j=1; j= 0 && n >= 0) {
+ nums1[p--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
+ }
+
+ while (n >= 0) {
+ nums1[p--] = nums2[n--];
+ }
+ }
+}
\ No newline at end of file
diff --git a/Week_01/id_23/NOTE.md b/Week_01/id_23/NOTE.md
index 107ea7d6..d376e4f1 100644
--- a/Week_01/id_23/NOTE.md
+++ b/Week_01/id_23/NOTE.md
@@ -1 +1,13 @@
-# 学习笔记
+# 学习笔记
+###本周总结
+之前在算法与数据结构的面试和任务上栽过很多次跟头,但每次痛心疾首之后,立过的flag继续随着时间的调用,一次次入栈…………变成了压箱底儿的任务。我越来越坚信,如果有个事情来了,你却没有勇敢地去解决掉,它一定会再来。生活真是这样,它会一次次地让你去做这个功课,知道你学会为止。生活大课适用,同理算法课也适用此公式。
+
+#### 关于Big O
+数据结构和算法本身解决的是“快”和“省”的问题。大O表示的是算法运行时间的增速!
+
+#### 关于递归 Recursion
+“如果使用循环,程序地性能可能更高;如果适用递归,程序可能更容易理解。如何选择要看什么对你来说更重要” --Leigh Caldwell
+递归套路模板 1. Recursion terminator2. Process logic 3.Drill down 4.Reverse the current level status if needed
+
+
+Chao:很不错的总结!希望保持这样的训练方法和势头,继续加油!
\ No newline at end of file
diff --git "a/Week_01/id_23/\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225_Eva.png" "b/Week_01/id_23/\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225_Eva.png"
new file mode 100644
index 00000000..69e38f16
Binary files /dev/null and "b/Week_01/id_23/\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225_Eva.png" differ
diff --git a/Week_01/id_24/LeetCode_189_024.py b/Week_01/id_24/LeetCode_189_024.py
new file mode 100644
index 00000000..9df12f66
--- /dev/null
+++ b/Week_01/id_24/LeetCode_189_024.py
@@ -0,0 +1,8 @@
+def rotate(nums, k):
+ """
+ :type nums: List[int]
+ :type k: int
+ :rtype: None Do not return anything, modify nums in-place instead.
+ """
+ for i in range(k):
+ nums.insert(0, nums.pop(len(nums) - 1))
\ No newline at end of file
diff --git a/Week_01/id_24/LeetCode_26_024.py b/Week_01/id_24/LeetCode_26_024.py
new file mode 100644
index 00000000..24c31d0c
--- /dev/null
+++ b/Week_01/id_24/LeetCode_26_024.py
@@ -0,0 +1,14 @@
+import math
+def removeDuplicates(nums):
+ if len(nums) <= 1: return len(nums)
+ i = 1
+ j = 0
+ while i < len(nums):
+ if nums[i] > nums[j]:
+ j = j + 1
+ if i > j:
+ nums[j] = nums[i]
+ i = i + 1
+ return j + 1
+
+removeDuplicates([1])
diff --git a/Week_01/id_24/LeetCode_441_024.py b/Week_01/id_24/LeetCode_441_024.py
new file mode 100644
index 00000000..4bc6ca7a
--- /dev/null
+++ b/Week_01/id_24/LeetCode_441_024.py
@@ -0,0 +1,24 @@
+import math
+def arrangeCoins(n):
+ """
+ :type n: int
+ :rtype: int
+ """
+ # s1
+ # for i in range(int(math.sqrt(2 * n)) + 1):
+ # if n - i * (i+1) / 2 <= i:
+ # return i
+
+ # s2
+ # i = int(math.sqrt(2 * n))
+ # print(i)
+ # while i >= 0:
+ # if n - i * (i+1) / 2 < 0:
+ # i = i - 1
+ # else:
+ # return i
+
+ # s3
+ return int(math.ceil((-3 + math.sqrt(9 + 8 * n))/2))
+
+print(arrangeCoins(6))
diff --git a/Week_01/id_24/LeetCode_88_024.py b/Week_01/id_24/LeetCode_88_024.py
new file mode 100644
index 00000000..63d6de3b
--- /dev/null
+++ b/Week_01/id_24/LeetCode_88_024.py
@@ -0,0 +1,53 @@
+def merge(nums1, m, nums2, n):
+ """
+ :type nums1: List[int]
+ :type m: int
+ :type nums2: List[int]
+ :type n: int
+ :rtype: None Do not return anything, modify nums1 in-place instead.
+ """
+
+ # s1
+ # i = 0
+ # j = 0
+ # ls = []
+ # while i + j < m + n:
+ # if i == m or nums1[i] > nums2[j]:
+ # ls.append(nums2[j])
+ # j = j + 1
+ # elif j == n or nums1[i] < nums2[j]:
+ # ls.append(nums1[i])
+ # i = i + 1
+ # elif nums1[i] == nums2[j]:
+ # ls.append(nums1[i])
+ # ls.append(nums2[j])
+ # i = i + 1
+ # j = j + 1
+
+ # nums1 = ls
+ # print(nums1)
+
+ # s2
+ if n == 0: return False
+ index = m + n - 1
+ while index >= 0:
+ if n == 0 or (m != 0 and nums1[m - 1] > nums2[n - 1]):
+ nums1[index] = nums1[m - 1]
+ if m > 0: m = m - 1
+ elif m == 0 or (n != 0 and nums1[m - 1] < nums2[n - 1]):
+ nums1[index] = nums2[n - 1]
+ if n > 0: n = n - 1
+ else:
+ nums1[index] = nums1[m - 1]
+ nums1[index - 1] = nums2[n - 1]
+ index = index - 1
+ if m > 0: m = m - 1
+ if n > 0: n = n - 1
+ index = index - 1
+
+merge(
+ [2,0],
+ 1,
+ [1],
+ 1
+)
\ No newline at end of file
diff --git a/Week_01/id_24/NOTE.md b/Week_01/id_24/NOTE.md
index 107ea7d6..251c2839 100644
--- a/Week_01/id_24/NOTE.md
+++ b/Week_01/id_24/NOTE.md
@@ -1 +1,20 @@
# 学习笔记
+
+毕业十年后,重新学习算法这件事,对我来说就是 An Olympian Task。解算法题,是一件既要花费时间又要花费精力的事,也是一个自我否定和反复失望的过程。
+
+上一份工作离职后,我一直处于远程工作状态,只跟业务打交道。之前偶尔写写代码,现在已经完全不写了。所以,这周的学习我是从刷 Python 极客专栏和官网教程开始的,进度比其他同学要缓慢很多。
+
+上周参加线下课时,因为没有预习,也没有预习的能力,所以,第一天很出戏。题目切换到中文版后,真的是每个汉字都认识,连在一起就懵圈。到第二天上课,我才终于进入学习状态。覃老师的讲解,帮我建立了算法的知识框架,并指明了解题的基本方向。当然,框架中具体内容的完善,需要我自己去努力。
+
+目前拿到题目,我的第一反应是,能不能从数学角度推导出求解公式。譬如这个题目 https://leetcode-cn.com/submissions/detail/20284600/ ,我第一反应就是等差数列求解,范围内取极值,但又感觉很取巧。于是,我还是强迫自己先用程序的思维去解题,写出其他答案后,再来实现自己的推导公式。
+
+现在没有优化的概念,基本就是把自己能想到的答案先码出来,除非题目特别要求,并没有认真计算解法的算法复杂度和空间复杂度。这周只完成了四道最简单的题目,希望下周每天都能打卡算法题,无论简单还是复杂,争取把它养成习惯。
+
+Python 是一门很简洁的语言,我看了自己的解题代码,感觉很累赘,希望在这个月学习算法的过程中也能进一步深入学习 Python 。
+
+
+
+Chao:看到你重新开始学习python和开始训练算法和写代码,非常佩服和让人觉得inspiring。希望用五毒神掌的办法保持训练下去。
+
+有志者、事竟成,破釜沉舟,百二秦关终属楚;
+苦心人、天不负,卧薪尝胆,三千越甲可吞吴。
diff --git "a/Week_01/id_24/\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204\345\255\246\344\271\240\350\204\221\345\233\276-V1.0.png" "b/Week_01/id_24/\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204\345\255\246\344\271\240\350\204\221\345\233\276-V1.0.png"
new file mode 100644
index 00000000..b69a789e
Binary files /dev/null and "b/Week_01/id_24/\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204\345\255\246\344\271\240\350\204\221\345\233\276-V1.0.png" differ
diff --git a/Week_01/id_25/LeetCode_42_025.java b/Week_01/id_25/LeetCode_42_025.java
new file mode 100644
index 00000000..24608fad
--- /dev/null
+++ b/Week_01/id_25/LeetCode_42_025.java
@@ -0,0 +1,143 @@
+package highFrequencyLeetcode.leetcode_42;
+
+import java.util.Stack;
+
+/**
+ * Hard
+ * (注解文档查看快捷键 选中类名或方法名 按ctrl + Q)
+ *
+ * 思维全过程记录方案:
+ * 1 背基础结构和算法 | 记录在课程笔记
+ * 2 看题 -> 悟题 思考过程 | 记录在wiki
+ * 3 悟题 -> 写题 实现难点 | 记录在代码注解
+ * 4 写题 -> 优化 多种解法 | 记录在leetcode提交
+ *
+ * 问题:
+ * Given n non-negative integers representing an elevation map
+ * where the width of each bar is 1,
+ * compute how much water it is able to trap after raining.
+ *
+ *
+ * @param columns
+ * @return
+ */
+ public static int bruteForce(int[] columns) {
+ int result = 0, length = columns.length;
+ for (int i = 0; i < length; i++) {
+ int maxl = 0, maxr = 0;
+ for (int j = i; j < length; j++) {
+ maxr = Math.max(maxr, columns[j]);
+ }
+ for (int j = i; j >= 0; j--) {
+ maxl = Math.max(maxl, columns[j]);
+ }
+ result += Math.min(maxl, maxr) - columns[i];
+ }
+ return result;
+ }
+
+ /**
+ * 解法2 DP求解
+ *
+ * @param columns
+ * @return
+ */
+ public static int dpOne(int[] columns) {
+ if (columns.length == 0) {
+ return 0;
+ }
+ int result = 0, length = columns.length;
+ int[] maxl = new int[columns.length];
+ int[] maxr = new int[columns.length];
+ maxl[0] = columns[0];
+ maxr[length - 1] = columns[length - 1];
+ for (int i = 1; i < length; i++) {
+ maxl[i] = Math.max(maxl[i - 1], columns[i]);
+ }
+ for (int i = length - 2; i >= 0; i--) {
+ maxr[i] = Math.max(maxr[i + 1], columns[i]);
+ }
+ for (int i = 1; i < columns.length - 1; i++) {
+ result += Math.min(maxl[i], maxr[i]) - columns[i];
+ }
+ return result;
+ }
+
+ /**
+ * 解法3 DP求解 单次遍历
+ *
+ * @param columns
+ * @return
+ */
+ public static int dpTwo(int[] columns) {
+ int result = 0, left = 0, right = columns.length - 1;
+ int maxl = 0, maxr = 0;
+ while (left < right) {
+ if (columns[left] < columns[right]) {
+ if (columns[left] >= maxl) {
+ maxl = columns[left];
+ } else {
+ result += (maxl - columns[left]);
+ }
+ ++left;
+ } else {
+ if (columns[right] >= maxr) {
+ maxr = columns[right];
+ } else {
+ result += (maxr - columns[right]);
+ }
+ --right;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * 解法4 栈解法
+ *
+ * @param columns
+ * @return
+ */
+ public static int stack(int[] columns) {
+ int result = 0, cursor = 0, length = columns.length;
+ Stack stack = new Stack<>();
+ while (cursor < length) {
+ if (stack.isEmpty() || columns[cursor] < columns[stack.peek()]) {
+ stack.push(cursor++);
+ } else {
+ int bottom = stack.pop();
+ if (stack.isEmpty()) {
+ continue;
+ }
+ int left = stack.peek();
+ int square = (Math.min(columns[left], columns[cursor]) - columns[bottom]) * (cursor - left - 1);
+ result += square;
+ }
+ }
+ return result;
+ }
+
+}
diff --git a/Week_01/id_25/LeetCode_62_025.java b/Week_01/id_25/LeetCode_62_025.java
new file mode 100644
index 00000000..73221c75
--- /dev/null
+++ b/Week_01/id_25/LeetCode_62_025.java
@@ -0,0 +1,189 @@
+package highFrequencyLeetcode.leetcode_62;
+
+/**
+ * Medium
+ * (注解文档查看快捷键 选中类名或方法名 按ctrl + Q)
+ *
+ * 思维全过程记录方案:
+ * 1 背基础结构和算法 | 记录在课程笔记
+ * 2 看题 -> 悟题 思考过程 | 记录在wiki
+ * 3 悟题 -> 写题 实现难点 | 记录在代码注解
+ * 4 写题 -> 优化 多种解法 | 记录在leetcode提交
+ *
+ * 问题:
+ * A robot is located at the top-left corner of a m x n grid
+ * (marked 'Start' in the diagram below).
+ * The robot can only move either down or right at any point in time.
+ * The robot is trying to reach the bottom-right corner of the grid
+ * (marked 'Finish' in the diagram below).
+ * How many possible unique paths are there?
+ *