diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..5b0a56e
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,6 @@
+*.rb linguist-language=C
+ChenXiaoxu/* linguist-vendored
+CuiGuangfan/* linguist-vendored
+SheYi/* linguist-vendored
+JiQiang/* linguist-vendored
+XuPershing/* linguist-vendored
diff --git a/ALeetcode/week-10/137-SingleNumber2.md b/ALeetcode/week-10/137-SingleNumber2.md
new file mode 100644
index 0000000..030138b
--- /dev/null
+++ b/ALeetcode/week-10/137-SingleNumber2.md
@@ -0,0 +1,70 @@
+Single Number II
+----------------
+* Author: LHearen
+* E-mail: LHearen@126.com
+* Time : Tue May 31 09:20:30 CST 2016
+* Description: Given an array of integers, every element appears three times except for one. Find that single one.
+Note:
+Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
+* Source: https://leetcode.com/problems/single-number-ii/
+
+### Solution
+Along with detailed comments in the code, no further comments will be added here.
+
+```
+//AC - 8ms;
+//direct method - calculate each bit and find the single one;
+int singleNumber0(int* nums, int size)
+{
+ int b, ret = 0;
+ int len = sizeof(int)*8;
+ for(int i = 0; i < len; i++)
+ {
+ b = 1 << i;
+ int count = 0;
+ for(int j = 0; j < size; j++)
+ if(b & nums[j])
+ count++;
+ if(count%3)
+ ret |= b;
+
+ }
+ return ret;
+
+}
+```
+
+
+```
+//AC - 4ms;
+//inspired by logical circuit design and boolean algebra;
+//counter - unit of 3;
+//current incoming next
+//a b c a b
+//0 0 0 0 0
+//0 1 0 0 1
+//1 0 0 1 0
+//0 0 1 0 1
+//0 1 1 1 0
+//1 0 1 0 0
+//a = a&~b&~c + ~a&b&c;
+//b = ~a&b&~c + ~a&~b&c;
+//return a|b since the single number can appear once or twice;
+int singleNumber(int* nums, int size)
+{
+ int a=0, b=0;
+ for(int i = 0; i < size; i++)
+ {
+ int t = (~a&b&nums[i])|(a&~b&~nums[i]);
+ b = (~a&~b&nums[i])|(~a&b&~nums[i]);
+ a = t;
+
+ }
+ return a|b;
+
+}
+```
+
+### Feedback
+There must be some better solutions to this problem, if you've got one, please never hesitate to inform me of that, so many thanks in advance!
+
diff --git a/ALeetcode/week-12/324-WiggleSort2.md b/ALeetcode/week-12/324-WiggleSort2.md
new file mode 100644
index 0000000..ac7c8f4
--- /dev/null
+++ b/ALeetcode/week-12/324-WiggleSort2.md
@@ -0,0 +1,89 @@
+WiggleSortII
+------------
+* Author: LHearen
+* E-mail: LHearen@126.com
+* Time : Tue May 31 09:20:30 CST 2016
+* Description: Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
+Example:
+(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
+(2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].
+Note:
+You may assume all input has valid answer.
+Follow Up:
+Can you do it in O(n) time and/or in-place with O(1) extra space?
+* Source: https://leetcode.com/problems/wiggle-sort-ii/
+
+### Solution
+
+Can there any true O(n) solution of this problem?
+I solved this by sorting and using another O(n) space to store the sorted elements and then rearranging them into the original array.
+
+There are several things we should clearly find out:
+
+in the result array, the even index position will contain smaller elements while the odd index position bigger ones;
+to avoid the middle part of the sorted array meet each other too quick (for example if we rearrange them like this: the smaller in ascending order, the bigger in descending order -> in different order but filling the result array in the same direction); so we have to rearrange them in the same order to the result array;
+but soon we will tumble into a dilemma by this test case [4, 5, 5, 6] in which if we rearrange them as the rules mentioned above it can be wrong;
+how about starting to place them from the end of the result array? then -> 5, 6, 4, 5 should be the result (the smaller: 4, 5; the bigger: 5, 6 and place the bigger first 5 since the last index is 3, an odd index then the first 4 of the smaller and then the second of the bigger 6, and then the second of the smaller 5). Quite right! the odd and even position rule -> big : odd, small : even
+Bang! End of Story!
+
+space cost O(n)
+time cost O(nlogn)
+
+
+### Details in C
+
+```
+void swap(int* p, int* q)
+{
+ int t=*p; *p=*q; *q=t;
+
+}
+
+void sort(int* nums, int begin, int end)
+{
+ int l=begin, r=end;
+ int v = nums[l+(r-l)/2];
+ while(l <= r)
+ {
+ while(nums[l] < v) l++;
+ while(nums[r] > v) r--;
+ if(l <= r)
+ {
+ swap(nums+l, nums+r);
+ l++; r--;
+
+ }
+
+ }
+ if(begin < r)
+ sort(nums, begin, r);
+ if(l < end)
+ sort(nums, l, end);
+
+}
+
+//AC - 40ms;
+void wiggleSort(int* nums, int size)
+{
+ sort(nums, 0, size-1); //using quick sort to sort the array first;
+ int *arr = (int*)malloc(sizeof(int)*size);
+ for(int i = 0; i < size; i++)
+ arr[i] = nums[i];
+ int small= 0; //the first of smallers;
+ int big = (size-1)/2+1; //the first of biggers;
+ int index = size-1; //start to fill in reverse direction: from right to left;
+ if(size%2 == 0) //if the size is even then the last should be indexed by odd size-1, so place the bigger one in odd position size-1;
+ nums[index--] = arr[big++];
+ while(index > -1)
+ {
+ nums[index--] = arr[small++];
+ if(index > -1) //in case of "underflow";
+ nums[index--] = arr[big++];
+
+ }
+
+}
+```
+
+### Feedback
+There must be some better solutions to this problem, if you've got one, please never hesitate to inform me of that, so many thanks in advance!
diff --git a/ALeetcode/week-8/63-UniquePaths2.md b/ALeetcode/week-8/63-UniquePaths2.md
new file mode 100644
index 0000000..cc3baf1
--- /dev/null
+++ b/ALeetcode/week-8/63-UniquePaths2.md
@@ -0,0 +1,101 @@
+---
+author: LHearen
+time: Sat Jan 23 20:20:36 CST 2016
+categories:
+-week8
+tags:
+-DP
+---
+
+#### Unique Paths II
+
+*Author : LHearen*
+
+*E-mail : LHearen@126.com*
+
+*Time : Sat Jan 23 20:53:00 CST 2016*
+
+*Description : Follow up for Unique Paths: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid.*
+
+*Source : https://leetcode.com/problems/unique-paths-ii/*
+
+#### Specification
+There is a direct way as UniquePaths, but that will definitely make the solution hard to realize and the complexity in space and time cost is rather high. At this very moment, we can spot one special feature in this problem, over-lapping sub-problems and the direct way will take lots of time to do the same sub-problems and that is why it will cost so much. So to improve the performance, we should try to store the handled sub-problems for later use and as a result we can try *Memoization* to hack it first; since *Memoization* can be simply used, then the *Dynamic Programming* might be helpful, so we will have the second solution.
+
+Both of them will cost O(row*column) in space and time complexity.
+
+#### Solution 1 - Memoization
+To fully make use of the outstanding features of DP, we have to allocate an array to store the states for each position and initialize them to -1 for checking whether it is handled already or not, and when it is handled, we can then directly use it to further decrease time cost. As we can easily understand, when there is a block in the **position** the path will be set as 0 and all others will be determined by the left and upper position and before we continue we will record the current result to the array for re-use.
+
+
+##### Code in C - 4ms;
+```
+int traverse(int** grid, int row, int col, int** arrs)
+{
+ if(row < 0 || col < 0)
+ return 0;
+ if(arrs[row][col] != -1)
+ return arrs[row][col];
+ int count = 0;
+ if(grid[row][col] == 1)
+ count = 0;
+ else if(row == 0 && col == 0)
+ count = 1;
+ else
+ count = traverse(grid, row-1, col, arrs) + traverse(grid, row, col-1, arrs);
+ arrs[row][col] = count;
+ return count;
+}
+
+//AC - 4ms;
+int uniquePathsWithObstacles1(int** grid, int rSize, int cSize)
+{
+ int **arrs = (int**)malloc(sizeof(int*)*rSize);
+ for(int i = 0; i < rSize; i++)
+ {
+ arrs[i] = (int*)malloc(sizeof(int)*cSize);
+ for(int j = 0; j < cSize; j++)
+ arrs[i][j] = -1;
+ }
+ traverse(grid, rSize-1, cSize-1, arrs);
+ return arrs[rSize-1][cSize-1];
+}
+```
+
+#### Solution 2 - Dynamic Programming
+As we can see, the previous solution will demand initialization before traversal and then in each recursion there will also require further function invoking, to further improve the performance and save time cost, we can have a try at Dynamic Programming which is a bottom-up method and the major clues are quite the same as the previous one.
+
+##### Code in C - 0ms;
+```
+//AC - 0ms;
+int uniquePathsWithObstacles0(int** grid, int rSize, int cSize)
+{
+ int** arrs = (int**)malloc(sizeof(int*)*rSize);
+ for(int i = 0; i < rSize; i++)
+ arrs[i] = (int*)malloc(sizeof(int)*cSize);
+ for(int i = 0; i < rSize; i++)
+ for(int j = 0; j < cSize; j++)
+ {
+ if(i==0 && j==0 && grid[i][j]==0)
+ {
+ arrs[i][j] = 1;
+ continue;
+ }
+ if(grid[i][j] == 1)
+ {
+ arrs[i][j] = 0;
+ continue;
+ }
+ int count = 0;
+ if(j-1 > -1)
+ count += arrs[i][j-1];
+ if(i-1 > -1)
+ count += arrs[i-1][j];
+ arrs[i][j] = count;
+ }
+ return arrs[rSize-1][cSize-1];
+}
+```
+
+##### Additional
+There must be some better solutions to this problem, if you've got one, please never hesitate to inform me of that, so many thanks in advance!
diff --git a/ChenXiaoxu/1. Two Sum.md b/ChenXiaoxu/1. Two Sum.md
new file mode 100644
index 0000000..624595c
--- /dev/null
+++ b/ChenXiaoxu/1. Two Sum.md
@@ -0,0 +1,52 @@
+# 1. Two Sum
+
+
+## Problem
+Given an array of integers, return indices of the two numbers such that they add up to a specific target.
+
+You may assume that each input would have exactly one solution.
+
+Example:
+```
+Given nums = [2, 7, 11, 15], target = 9,
+
+Because nums[0] + nums[1] = 2 + 7 = 9,
+return [0, 1].
+```
+tag:
+
+## Solution
+
+方法一:暴力破解
+
+时间复杂度O(N2)
+
+竟然**Accepted** :)
+
+方法二:哈希表
+
+时间复杂度O(N), 空间复杂度O(N);
+
+**java**
+```java
+ public int[] twoSum(int[] nums, int target) {
+ Map map = new HashMap();
+ for(int i=0; i< nums.length; i++) {
+ if(map.containsKey(target-nums[i])) {
+ return new int[]{map.get(target-nums[i]), i};
+ } else {
+ map.put(nums[i], i);
+ }
+ }
+ return null;
+ }
+```
+
+**go**
+```go
+
+```
+
+方法三:排序,逼近
+
+时间复杂度O(NLogN)
\ No newline at end of file
diff --git a/ChenXiaoxu/101. Symmetric Tree.md b/ChenXiaoxu/101. Symmetric Tree.md
new file mode 100644
index 0000000..3dacee0
--- /dev/null
+++ b/ChenXiaoxu/101. Symmetric Tree.md
@@ -0,0 +1,96 @@
+# 101. Symmetric Tree
+
+## Problem
+
+Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
+
+For example, this binary tree is symmetric:
+```
+ 1
+ / \
+ 2 2
+ / \ / \
+3 4 4 3
+But the following is not:
+ 1
+ / \
+ 2 2
+ \ \
+ 3 3
+```
+Note:
+Bonus points if you could solve it both recursively and iteratively.
+
+tag:
+
+## Solution
+
+### 递归
+
+树中两个点对称(n1,n2), 递归判断n1的左子树与n2的右子树,n2的右子树与n2的左子树是否对称
+
+**java**
+```java
+ public boolean isSymmetric(TreeNode root) {
+ return root==null || helper(root.left, root.right);
+ }
+
+ boolean helper(TreeNode n1, TreeNode n1) {
+ if(n1==null || n2==null) return n1==n2;
+ if(n1.val != n2.val) return false;
+ return helper(n1.left, n2.right)&&helper(n1.right, n2.left);
+ }
+```
+
+**go**
+```go
+
+```
+
+### 迭代
+
+**模拟递归工作栈**
+
+```java
+ public boolean isSymmetric(TreeNode root) {
+ if(root==null) return true;
+
+ Stack s = new Stack();
+
+ if(root.left!=null && root.right!=null) {
+ s.push(root.left);
+ s.push(root.right);
+ } else if(root.left==null && root.right==null) {
+ return true;
+ } else {
+ return false;
+ }
+
+ TreeNode left, right;
+
+ while(!s.isEmpty()) {
+ // if(s.size()%2!=0) return false;
+ right = s.pop();
+ left = s.pop();
+ if(right.val!=left.val) return false;
+
+ if(left.left!=null && right.right!=null) {
+ s.push(left.left);
+ s.push(right.right);
+ } else if(!(left.left==null&&right.right==null)) {
+ return false;
+ }
+
+ if(left.right!=null && right.left!=null) {
+ s.push(left.right);
+ s.push(right.left);
+ } else if(!(left.right==null && right.left==null)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+```
+
+**层次遍历,判断每层是否对称**
\ No newline at end of file
diff --git a/ChenXiaoxu/102. Binary Tree Level Order Traversal.md b/ChenXiaoxu/102. Binary Tree Level Order Traversal.md
new file mode 100644
index 0000000..4364568
--- /dev/null
+++ b/ChenXiaoxu/102. Binary Tree Level Order Traversal.md
@@ -0,0 +1,57 @@
+# [107. Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)
+
+## Problem
+
+Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
+
+For example:
+Given binary tree {3,9,20,#,#,15,7},
+```
+ 3
+ / \
+ 9 20
+ / \
+ 15 7
+```
+return its level order traversal as:
+```
+[
+ [15,7],
+ [9,20],
+ [3]
+]
+```
+
+tag:
+- tree
+- bfs
+
+## Solution
+
+类似于[Binary Tree Level Order Traversal I](https://leetcode.com/problems/binary-tree-level-order-traversal/) 对二叉树宽度优先搜索,并记录每层的节点数,遍历完一层后,把结果**插入到链表头部**,再进行下一层遍历
+
+**java**
+```java
+ public List> levelOrderBottom(TreeNode root) {
+ List> res = new LinkedList>();
+ if (root==null) return res;
+ Queue q = new LinkedList();
+ q.offer(root);
+ while(!q.isEmpty()) {
+ int levelNum = q.size();
+ List subList = new LinkedList();
+ for(int i=0; i> zigzagLevelOrder(TreeNode root) {
+ List> res = new LinkedList>();
+ if(root==null) return res;
+ Queue q = new LinkedList();
+ q.offer(root);
+ boolean level = false;
+ while(!q.isEmpty()) {
+ int levelNum = q.size();
+ List subList = new LinkedList();
+ for(int i=0; i> levelOrder(TreeNode root) {
+ Queue q = new LinkedList();
+ List> res = new LinkedList>();
+
+ if(root==null) return res;
+
+ q.offer(root);
+ while(!q.isEmpty()) {
+ int levelNum = q.size();
+ List subList = new LinkedList();
+ for(int i=0; iHere is the proof. Proved by contradiction:
+
+>Suppose the returned result is not the optimal solution. Then there must exist an optimal solution, say a container with aol and aor (left and right respectively), such that it has a greater volume than the one we got. Since our algorithm stops only if the two pointers meet. So, we must have visited one of them but not the other. WLOG, let's say we visited aol but not aor. When a pointer stops at a_ol, it won't move until
+
+>The other pointer also points to aol. In this case, iteration ends. But the other pointer must have visited aor on its way from right end to aol. Contradiction to our assumption that we didn't visit aor.
+
+>The other pointer arrives at a value, say arr, that is greater than aol before it reaches aor. In this case, we does move aol. But notice that the volume of aol and arr is already greater than aol and aor (as it is wider and heigher), which means that aol and aor is not the optimal solution -- Contradiction!
+
+**java**
+```java
+ public int maxArea(int[] height) {
+ int i=0, j=height.length-1;
+ int res = 0;
+ while(i - 左右子树高度之差不大于1
+> - 左右子树也是平衡二叉树
+
+**java**
+```java
+ public boolean isBalanced(TreeNode root) {
+ if(root==null) return true;
+ return Math.abs(depth(root.left)-depth(root.right))<=1 && isBalanced(root.left) && isBalanced(root.right);
+ }
+
+ int depth(TreeNode root) {
+ if(root==null) return 0;
+ return Math.max(depth(root.left),depth(root.right))+1;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/111. Minimum Depth of Binary Tree.md b/ChenXiaoxu/111. Minimum Depth of Binary Tree.md
new file mode 100644
index 0000000..17951bb
--- /dev/null
+++ b/ChenXiaoxu/111. Minimum Depth of Binary Tree.md
@@ -0,0 +1,29 @@
+# 111. Minimum Depth of Binary Tree
+
+## Problem
+
+Given a binary tree, find its minimum depth.
+
+tag:
+
+## Solution
+
+当前树最小高度等于:
+- 若左子树为空, 右子树高度+1
+- 若有子树为空, 左子树高度+1
+- 左右子树种最小高度+1
+
+**java**
+```java
+ public int minDepth(TreeNode root) {
+ if(root==null) return 0;
+ if(root.left==null) return minDepth(root.right)+1;
+ if(root.right==null) return minDepth(root.left)+1;
+ return Math.min(minDepth(root.left), minDepth(root.right))+1;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/121. Best Time to Buy and Sell Stock.md b/ChenXiaoxu/121. Best Time to Buy and Sell Stock.md
new file mode 100644
index 0000000..113528d
--- /dev/null
+++ b/ChenXiaoxu/121. Best Time to Buy and Sell Stock.md
@@ -0,0 +1,37 @@
+# 121. Best Time to Buy and Sell Stock
+
+## Problem
+
+Say you have an array for which the ith element is the price of a given stock on day i.
+
+If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
+
+tag:
+- dp
+
+## Solution
+动态规划:
+```
+profit[i] = profit[i-1] if price[i]-min <= profit[i-1]
+ = price[i] if price[i]-min > profit[i-1]
+
+min = min price[j] (j<=i)
+```
+**java**
+```java
+ public int maxProfit(int[] prices) {
+ int min = Integer.MAX_VALUE, profit = 0;
+ for(int i=0; i profit)
+ profit = prices[i]-min;
+ }
+ return profit;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/125 Valid Palindrome.md b/ChenXiaoxu/125 Valid Palindrome.md
new file mode 100644
index 0000000..210d8ec
--- /dev/null
+++ b/ChenXiaoxu/125 Valid Palindrome.md
@@ -0,0 +1,46 @@
+# 125. Valid Palindrome
+
+## Problem
+
+Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
+
+For example,
+"A man, a plan, a canal: Panama" is a palindrome.
+"race a car" is not a palindrome.
+
+Note:
+Have you consider that the string might be empty? This is a good question to ask during an interview.
+
+For the purpose of this problem, we define empty string as valid palindrome.
+
+tag:
+- two pointers
+- string
+
+## Solution
+
+**java**
+```java
+ public boolean isPalindrome(String s) {
+ if (s == null || s.isEmpty())
+ return true;
+ int l = 0, r = s.length() - 1;
+ while (l < r) {
+ if (!Character.isLetterOrDigit(s.charAt(l))) {
+ l++;
+ continue;
+ }
+ if (!Character.isLetterOrDigit(s.charAt(r))) {
+ r--;
+ continue;
+ }
+ if (Character.toLowerCase(s.charAt(l)) == Character.toLowerCase(s.charAt(r))) {
+ l++;
+ r--;
+ } else {
+ return false;
+ }
+ }
+ return true;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/136. Single Number.md b/ChenXiaoxu/136. Single Number.md
new file mode 100644
index 0000000..7cbf461
--- /dev/null
+++ b/ChenXiaoxu/136. Single Number.md
@@ -0,0 +1,25 @@
+# 136. Single Number
+
+## Problem
+Given an array of integers, every element appears twice except for one. Find that single one.
+
+tag:
+- 位运算
+
+## Solution
+
+**java**
+```java
+ public int singleNumber(int[] nums) {
+ int res = nums[0];
+ for(int i=1; i>i)&1;
+ }
+ res |= ((c[i]%3)< 1, 00 => 0.
+ return a|b;
+
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/139. Word Break.md b/ChenXiaoxu/139. Word Break.md
new file mode 100644
index 0000000..2927e99
--- /dev/null
+++ b/ChenXiaoxu/139. Word Break.md
@@ -0,0 +1,61 @@
+# 139. Word Break
+
+## Problem
+Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
+
+For example, given
+s = "leetcode",
+dict = ["leet", "code"].
+
+Return true because "leetcode" can be segmented as "leet code".
+tag:
+
+## Solution
+
+方法一: 分支限界法(记忆化搜索)
+
+**java**
+```java
+ public boolean wordBreak(String s, Set wordDict) {
+ return helper(s, wordDict, new HashMap());
+ }
+
+ boolean helper(String s, Set wordDict, Map map) {
+ if(map.containsKey(s)) return map.get(s);
+ if(s.length()==0) return true;
+ boolean res = false;
+ for(String word : wordDict) {
+ if(s.startsWith(word)) {
+ res |= helper(s.substring(word.length()), wordDict, map);
+ }
+ }
+ map.put(s, res);
+ return res;
+ }
+```
+
+**go**
+```go
+
+```
+
+方法二: 动态规划(效果不太好,可能与数据集有关)
+
+设dp[i]表示长度i的字串是否能够划分, 则:
+
+dp[i] = dp[i-j] 如果字串ij在词典中(0= wordDict) {
+ boolean[] dp = new boolean[s.length()+1];
+ dp[0] = true;
+ for(int i=1; i=0&&!dp[i]; j--){
+ if(wordDict.contains(s.substring(i-j,i))) dp[i] = dp[i-j];
+ }
+ }
+ return dp[dp.length-1];
+}
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/140. Word Break II.md b/ChenXiaoxu/140. Word Break II.md
new file mode 100644
index 0000000..45d78e8
--- /dev/null
+++ b/ChenXiaoxu/140. Word Break II.md
@@ -0,0 +1,54 @@
+# 140. Word Break II
+
+## Problem
+Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
+
+Return all such possible sentences.
+
+For example, given
+s = "catsanddog",
+dict = ["cat", "cats", "and", "sand", "dog"].
+
+A solution is ["cats and dog", "cat sand dog"].
+
+tag:
+- backtracking
+- dp
+- 记忆化搜索
+
+## Solution
+
+直接DFS, 会TLE :(
+
+采用记忆化搜索进行剪枝:在搜索的过程中,用map纪录已经搜索过的结果
+
+**java**
+```java
+ public List wordBreak(String s, Set wordDict) {
+ return memSearch(s, wordDict, new HashMap>());
+ }
+
+ List memSearch(String s, Set dict, HashMap> map) {
+ if (map.containsKey(s))
+ return map.get(s);
+ LinkedList res = new LinkedList();
+ if (s.length() == 0) {
+ res.add("");
+ return res;
+ }
+ for (String word : dict) {
+ if (s.startsWith(word)) {
+ List subList = memSearch(s.substring(word.length()), dict, map);
+ for (String sub : subList)
+ res.add(word + (sub.isEmpty() ? "" : " ") + sub);
+ }
+ }
+ map.put(s, res);
+ return res;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/141. Linked List Cycle.md b/ChenXiaoxu/141. Linked List Cycle.md
new file mode 100644
index 0000000..014926e
--- /dev/null
+++ b/ChenXiaoxu/141. Linked List Cycle.md
@@ -0,0 +1,53 @@
+# 141. Linked List Cycle
+
+## Problem
+
+Given a linked list, determine if it has a cycle in it.
+
+Follow up:
+Can you solve it without using extra space?
+
+tag:
+
+## Solution
+
+快慢指针,slow一次移动一步,fast一次移动两步, 如果两个指针相遇,则存在环。
+
+**证明:**
+如图, 设交点为M,环的起点为E,|HE| = L1, |EM|=L2, 环的长度为 C,
+
+1. 第一个(速度慢的)指针在环里转满一圈之前,两个指针必然相遇
+设 slow第一次进入环时,fast在slow前方 a处, 经过x次移动后两指针相遇, 则```x = (2x+a)%L => x=L-a```
+
+2. 相遇位置
+```2*(L1+L2) = L1+L2+nC => L1+L2=nC```
+即, 两指针相遇时,令fast指针重新从H点开始,一次移动一步,则fast, slow必定在环起点相遇
+
+
+
+
+**java**
+
+```java
+ public ListNode detectCycle(ListNode head) {
+ if(head==null||head.next==null) return null;
+ ListNode f = head, s = head;
+ while(f!=null && f.next!=null) {
+ f = f.next.next;
+ s = s.next;
+ if(f==s) break;
+ }
+ if(f!=s) return null;
+ f = head;
+ while(f!=s) {
+ f = f.next;
+ s = s.next;
+ }
+ return f;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/142. Linked List Cycle II.md b/ChenXiaoxu/142. Linked List Cycle II.md
new file mode 100644
index 0000000..658d685
--- /dev/null
+++ b/ChenXiaoxu/142. Linked List Cycle II.md
@@ -0,0 +1,36 @@
+# [142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)
+
+## Problem
+
+Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
+
+Note: Do not modify the linked list.
+
+Follow up:
+Can you solve it without using extra space?
+
+tag:
+
+## Solution
+
+参考,[141. Linked List Cycle]() 解题报告
+
+
+**java**
+
+```java
+ public boolean hasCycle(ListNode head) {
+ ListNode f = head, s = head;
+ while(s!=null&&s.next!=null) {
+ s = s.next.next;
+ f = f.next;
+ if(s==f) return true;
+ }
+ return false;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/15. 3Sum.md b/ChenXiaoxu/15. 3Sum.md
new file mode 100644
index 0000000..2c9d4e1
--- /dev/null
+++ b/ChenXiaoxu/15. 3Sum.md
@@ -0,0 +1,51 @@
+# [15. 3Sum](https://leetcode.com/problems/3sum/)
+
+## Problem
+Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
+
+Note:
+- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
+- The solution set must not contain duplicate triplets.
+
+ For example, given array S = {-1 0 1 2 -1 -4},
+
+ A solution set is:
+ (-1, 0, 1)
+ (-1, -1, 2)
+
+tag:
+- two pointers
+- sort
+
+## Solution
+
+固定一个数,然后采用two sum方法求解, 注意去除重复元素。
+
+```java
+ public List> threeSum(int[] nums) {
+ List> res = new ArrayList>();
+ Arrays.sort(nums);
+ for (int i = 0; i < nums.length; i++) {
+ //skip duplicate elements
+ if (i - 1 >= 0 && nums[i] == nums[i - 1])
+ continue;
+ int p1 = i + 1, p2 = nums.length - 1;
+ while (p1 < p2) {
+ if (nums[p1] + nums[p2] == 0 - nums[i]) {
+ res.add(Arrays.asList(nums[i], nums[p1], nums[p2]));
+ //skip duplicate elements
+ while (p1 + 1 < p2 && nums[p1] == nums[p1 + 1])
+ p1++;
+ while (p2 - 1 > p1 && nums[p2] == nums[p2 - 1])
+ p2--;
+ p1++;
+ p2--;
+ } else if (nums[p1] + nums[p2] < 0 - nums[i])
+ p1++;
+ else
+ p2--;
+ }
+ }
+ return res;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/151. Reverse Words in a String.md b/ChenXiaoxu/151. Reverse Words in a String.md
new file mode 100644
index 0000000..f3f99ae
--- /dev/null
+++ b/ChenXiaoxu/151. Reverse Words in a String.md
@@ -0,0 +1,44 @@
+# 151. Reverse Words in a String
+
+## Problem
+Given an input string, reverse the string word by word.
+
+For example,
+Given s = "the sky is blue",
+return "blue is sky the".
+
+tag:
+- string
+- StringBuffer, StringBuilder, String
+
+## Solution
+
+一种简单的方法是两边遍历, 第一遍遍历分离出字符串中的word, 保存在数组中, 第二遍遍历对字符串反转
+
+从后向前迭代可以做到一次遍历, 纪录每个单词的起始位置,append到反转字符串
+
+时间复杂度O(n), 空间负载都O(n)
+
+**java**
+```java
+ public String reverseWords(String s) {
+ StringBuilder reversed = new StringBuilder();
+ int j = s.length();
+ for (int i = s.length() - 1; i >= 0; i--) {
+ if (s.charAt(i) == ' ') {
+ j = i;
+ } else if (i == 0 || s.charAt(i - 1) == ' ') {
+ if (reversed.length() != 0) {
+ reversed.append(' ');
+ }
+ reversed.append(s.substring(i, j));
+ }
+ }
+ return reversed.toString();
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/165. Compare Version Numbers.md b/ChenXiaoxu/165. Compare Version Numbers.md
new file mode 100644
index 0000000..7233ab6
--- /dev/null
+++ b/ChenXiaoxu/165. Compare Version Numbers.md
@@ -0,0 +1,43 @@
+# 165. Compare Version Numbers
+
+
+## Problem
+
+Compare two version numbers version1 and version2.
+If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
+
+You may assume that the version strings are non-empty and contain only digits and the . character.
+The . character does not represent a decimal point and is used to separate number sequences.
+For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
+
+Here is an example of version numbers ordering:
+
+0.1 < 1.1 < 1.2 < 13.37
+
+## Solution
+
+把字符串按点号拆分转换为整数,从高位到低位逐次比较,低位不存在补零比较。
+
+**java**
+```java
+ public int compareVersion(String version1, String version2) {
+ String[] v1 = version1.split("\\."), v2 = version2.split("\\.");
+ int maxLen = Math.max(v1.length, v2.length);
+ for (int i = 0; i < maxLen; i++) {
+ int n1 = i >= v1.length ? 0 : Integer.parseInt(v1[i]);
+ int n2 = i >= v2.length ? 0 : Integer.parseInt(v2[i]);
+ if (n1 > n2)
+ return 1;
+ else if (n1 < n2)
+ return -1;
+ else
+ continue;
+ }
+ return 0;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/166. Fraction to Recurring Decimal.md b/ChenXiaoxu/166. Fraction to Recurring Decimal.md
new file mode 100644
index 0000000..f79584e
--- /dev/null
+++ b/ChenXiaoxu/166. Fraction to Recurring Decimal.md
@@ -0,0 +1,48 @@
+# [166. Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/)
+
+## Problem
+
+tag:
+
+## [Solution](https://leetcode.com/discuss/31521/short-java-solution)
+
+1. 判断符号,转化为正数处理
+分子、分母异号,为负,同号为正(**注意转换时,INT_MIN取反结果会超出范围,为了防止溢出,转换为long类型**)
+
+2. 分子除以分母
+- 如果恰好整除,返回结果
+- 如果不能整除,则商是最终结果的整数部分,小数部分由余数运算取得:
+ > 用哈希表纪录余数对应小数的位置,如果出现相同余数,则代表出现循环,在对应位置插入括号,计算结束
+ > (余数*10)/分母, 对应小数的第一位
+ >(余数*10)%分母,对应新的余数
+
+3. 输出结果,去除末尾结果为零的情况
+
+**java**
+```java
+ public String fractionToDecimal(int numerator, int denominator) {
+ StringBuffer res = new StringBuffer();
+ String sign = (numerator < 0 == denominator < 0 || numerator == 0) ? "" : "-";
+ long num = Math.abs((long)numerator), den = Math.abs((long)denominator);
+ res.append(sign);
+ res.append(num / den);
+ long remainder = num % den;
+ if (remainder == 0) return res.toString();
+ res.append(".");
+ HashMap map = new HashMap();
+ while (!map.containsKey(remainder)) {
+ map.put(remainder, res.length());
+ res.append(10 * remainder / den);
+ remainder = 10 * remainder % den;
+ }
+ int index = map.get(remainder);
+ res.insert(index, "(");
+ res.append(")");
+ return res.toString().replace("(0)", "");
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/18. 4Sum.md b/ChenXiaoxu/18. 4Sum.md
new file mode 100644
index 0000000..34437c0
--- /dev/null
+++ b/ChenXiaoxu/18. 4Sum.md
@@ -0,0 +1,50 @@
+# [18. 4Sum](https://leetcode.com/problems/4sum/)
+
+## Problem
+Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
+
+Note:
+Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
+The solution set must not contain duplicate quadruplets.
+ For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
+
+ A solution set is:
+ (-1, 0, 0, 1)
+ (-2, -1, 1, 2)
+ (-2, 0, 0, 2)
+
+tag:
+- two pointers
+- sort
+
+## Solution
+
+类似于3sum问题, 固定两个数,然后采用two sum方法求解, 注意去除重复元素。
+
+```java
+public List> fourSum(int[] nums, int target) {
+ List> res = new ArrayList>();
+ Arrays.sort(nums);
+ for(int i=0; i0 && nums[i-1]==nums[i]) continue;
+ for(int j=i+1; jp1 && nums[p2]==nums[p2-1]) p2--;
+ p2--;
+ p1++;
+ } else if(nums[p1]+nums[p2] 4 -> 3) + (5 -> 6 -> 4)
+Output: 7 -> 0 -> 8
+
+tag:
+- linked list
+
+
+## Solution
+链表模拟加法操作,高位补零简化代码
+**java**
+```java
+ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
+ int carry = 0, res = 0;
+ ListNode dummy = new ListNode(0);
+ ListNode p = dummy;
+ while (l1 != null || l2 != null || carry!=0) {
+ res = ((l1==null)?0:l1.val) + ((l2==null)?0:l2.val) + carry;
+ carry = res / 10;
+ p.next = new ListNode(res % 10);
+ p = p.next;
+ l1 = (l1==null)?l1:l1.next;
+ l2 = (l2==null)?l2:l2.next;
+ }
+ return dummy.next;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/20. Valid Parentheses.md b/ChenXiaoxu/20. Valid Parentheses.md
new file mode 100644
index 0000000..53ef4b1
--- /dev/null
+++ b/ChenXiaoxu/20. Valid Parentheses.md
@@ -0,0 +1,34 @@
+# 20. Valid Parentheses
+
+## Problem
+
+Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
+
+The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
+
+tag:
+
+- String
+- stack
+
+## Solution
+
+**java**
+```java
+ public boolean isValid(String s) {
+ Stack stack = new Stack();
+ String left = "([{", right = ")]}";
+ for (int i = 0; i < s.length(); i++) {
+ if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
+ stack.push(s.charAt(i));
+ } else {
+ if (stack.isEmpty() || s.charAt(i) != right.charAt(left.indexOf(stack.peek())))
+ return false;
+ stack.pop();
+ }
+ }
+ if (!stack.isEmpty())
+ return false;
+ return true;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/204. Count Primes.md b/ChenXiaoxu/204. Count Primes.md
new file mode 100644
index 0000000..20d2052
--- /dev/null
+++ b/ChenXiaoxu/204. Count Primes.md
@@ -0,0 +1,34 @@
+# [204. Count Primes](https://leetcode.com/problems/count-primes/)
+
+## Problem
+Count the number of prime numbers less than a non-negative number, n.
+
+tag:
+
+## Solution
+参考该题的 [Hint](https://leetcode.com/problems/count-primes/)
+
+**java**
+```java
+ public int countPrimes(int n) {
+ boolean[] isPrimes = new boolean[n];
+ for (int i = 2; i < n; i++) isPrimes[i] = true;
+ for (int i = 2; i * i < n; i++) {
+ if (isPrimes[i]) {
+ for (int j = i * i; j < n; j += i)
+ isPrimes[j] = false;
+ }
+ }
+ int count = 0;
+ for (int i = 2; i < n; i++) {
+ if (isPrimes[i])
+ count++;
+ }
+ return count;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/206. Reverse Linked List.md b/ChenXiaoxu/206. Reverse Linked List.md
new file mode 100644
index 0000000..4acd8a3
--- /dev/null
+++ b/ChenXiaoxu/206. Reverse Linked List.md
@@ -0,0 +1,36 @@
+# 206. Reverse Linked List
+
+## Problem
+Reverse a singly linked list.
+
+tag:
+
+## Solution
+遍历链表,然后修改链表的指针,指向其前驱节点。注意当修改链表的next指针时,后继结点会丢失,因此需要保存后继结点。
+如图, 定义```pre, cur, next```分别指向当前,前驱,后继节点, 则:
+```
+while(cur不为空)
+ 纪录后继节点: next = cur.next;
+ 修改当前结点指针: cur.next = pre;
+ 更新pre,cur节点位置: pre = cur; cur = next;
+```
+
+
+**java**
+```java
+ public ListNode reverseList(ListNode head) {
+ ListNode pre = null, next = null;
+ while(head!=null) {
+ next = head.next;
+ head.next = pre;
+ pre = head;
+ head = next;
+ }
+ return pre;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/twolist/Solution.java b/ChenXiaoxu/21 Merge Two Sorted Lists.md
similarity index 64%
rename from ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/twolist/Solution.java
rename to ChenXiaoxu/21 Merge Two Sorted Lists.md
index fac9939..214cf6d 100644
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/twolist/Solution.java
+++ b/ChenXiaoxu/21 Merge Two Sorted Lists.md
@@ -1,7 +1,17 @@
-package linkedlist.merge.twolist;
+#69. Sqrt(x)
-import linkedlist.ListNode;
-public class Solution {
+## Problem
+
+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.
+
+tag:
+- linked list
+
+## Solution
+
+**java**
+
+```java
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = dummyHead;
@@ -21,8 +31,4 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
p.next = l2;
return dummyHead.next;
}
-}
-
-
-
-
+```
diff --git a/ChenXiaoxu/217. Contains Duplicate.md b/ChenXiaoxu/217. Contains Duplicate.md
new file mode 100644
index 0000000..70cc63b
--- /dev/null
+++ b/ChenXiaoxu/217. Contains Duplicate.md
@@ -0,0 +1,24 @@
+# 217. Contains Duplicate
+
+## Problem
+Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
+tag:
+- hash table
+
+## Solution
+
+**java**
+```java
+ public boolean containsDuplicate(int[] nums) {
+ Set hashSet = new HashSet();
+ for(int num: nums) {
+ if(!hashSet.add(num)) return true;
+ }
+ return false;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/219. Contains Duplicate II.md b/ChenXiaoxu/219. Contains Duplicate II.md
new file mode 100644
index 0000000..991fcdd
--- /dev/null
+++ b/ChenXiaoxu/219. Contains Duplicate II.md
@@ -0,0 +1,27 @@
+# 219. Contains Duplicate II
+
+## Problem
+Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
+
+tag:
+
+- hash table
+
+## Solution
+
+**java**
+```java
+ public boolean containsNearbyDuplicate(int[] nums, int k) {
+ Map map = new HashMap();
+ for(int i=0; i stack = new Stack();
+ for (int i = 0; i < len; i++) {
+ if (Character.isDigit(s.charAt(i))) {
+ int sum = s.charAt(i) - '0';
+ while (i + 1 < len && Character.isDigit(s.charAt(i + 1))) {
+ sum = sum * 10 + s.charAt(i + 1) - '0';
+ i++;
+ }
+ res += sum*sign;
+ } else if (s.charAt(i) == '+') {
+ sign = 1;
+ } else if (s.charAt(i) == '-') {
+ sign = -1;
+ } else if (s.charAt(i) == '(') {
+ stack.push(res);
+ stack.push(sign);
+ res = 0;
+ sign = 1;
+ } else if (s.charAt(i) == ')') {
+ res = res * stack.pop() + stack.pop();
+ }
+ }
+ return res;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/226. Invert Binary Tree.md b/ChenXiaoxu/226. Invert Binary Tree.md
new file mode 100644
index 0000000..3d7d9d3
--- /dev/null
+++ b/ChenXiaoxu/226. Invert Binary Tree.md
@@ -0,0 +1,42 @@
+# 226. Invert Binary Tree
+
+## Problem
+nvert a binary tree.
+
+ 4
+ / \
+ 2 7
+ / \ / \
+1 3 6 9
+to
+ 4
+ / \
+ 7 2
+ / \ / \
+9 6 3 1
+
+tag:
+
+## Solution
+
+>如果节点为空,返回
+>否则
+- 递归转换左子树
+- 递归转换右子树
+- 对换左右子树
+
+**java**
+```java
+ public TreeNode invertTree(TreeNode root) {
+ if(root==null) return root;
+ TreeNode tmp = root.left;
+ root.left = invertTree(root.right);
+ root.right = invertTree(tmp);
+ return root;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/klist/Solution.java b/ChenXiaoxu/23 Merge k Sorted Lists.md
similarity index 80%
rename from ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/klist/Solution.java
rename to ChenXiaoxu/23 Merge k Sorted Lists.md
index 40e0207..b91b19f 100644
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/klist/Solution.java
+++ b/ChenXiaoxu/23 Merge k Sorted Lists.md
@@ -1,9 +1,19 @@
-package linkedlist.merge.klist;
+#69. Sqrt(x)
-import java.util.List;
+## Problem
-import linkedlist.ListNode;
+Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
+tag:
+- divide and conquer
+- linked list
+- heap
+
+## Solution
+
+**java**
+
+```java
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
@@ -45,3 +55,4 @@ private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
return dummyHead.next;
}
}
+```
diff --git a/ChenXiaoxu/234. Palindrome Linked List.md b/ChenXiaoxu/234. Palindrome Linked List.md
new file mode 100644
index 0000000..c5330d3
--- /dev/null
+++ b/ChenXiaoxu/234. Palindrome Linked List.md
@@ -0,0 +1,47 @@
+# [234. Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)
+
+## Problem
+Given a singly linked list, determine if it is a palindrome.
+
+Follow up:
+Could you do it in O(n) time and O(1) space?
+
+tag:
+
+## Solution
+
+采用快慢指针找到中间节点,然后反转后半部分链表, 判断两部分链表是否相等,注意奇偶节点。
+
+**java**
+```java
+ public boolean isPalindrome(ListNode head) {
+ ListNode fast = head,slow = head;
+ while(fast!=null&&fast.next!=null) {
+ fast = fast.next.next;
+ slow = slow.next;
+ }
+ if(fast!=null) slow = slow.next;
+ slow = reverseList(slow);
+ while(slow!=null && slow.val==head.val) {
+ slow = slow.next;
+ head = head.next;
+ }
+ return slow==null;
+ }
+
+ ListNode reverseList(ListNode head) {
+ ListNode pre = null, next = null;
+ while(head!=null) {
+ next = head.next;
+ head.next = pre;
+ pre = head;
+ head = next;
+ }
+ return pre;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/257. Binary Tree Paths.md b/ChenXiaoxu/257. Binary Tree Paths.md
new file mode 100644
index 0000000..7b1a2df
--- /dev/null
+++ b/ChenXiaoxu/257. Binary Tree Paths.md
@@ -0,0 +1,49 @@
+# 257. Binary Tree Paths
+
+## Problem
+
+Given a binary tree, return all root-to-leaf paths.
+
+For example, given the following binary tree:
+
+ 1
+ / \
+2 3
+ \
+ 5
+All root-to-leaf paths are:
+
+["1->2->5", "1->3"]
+
+tag:
+- Tree
+
+## Solution
+
+**java**
+```java
+ public List binaryTreePaths(TreeNode root) {
+ List res = new ArrayList();
+ helper(res, new StringBuilder(), root);
+ return res;
+ }
+
+ void helper(List res, StringBuilder str, TreeNode root) {
+ if(root==null) return;
+ int len = str.length();
+ str.append(root.val);
+ if(root.left==null && root.right==null) {
+ res.add(str.toString());
+ } else {
+ str.append("->");
+ helper(res, str, root.left);
+ helper(res, str, root.right);
+ }
+ str.setLength(len);
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/26 Remove Duplicates from Sorted Array.md b/ChenXiaoxu/26 Remove Duplicates from Sorted Array.md
new file mode 100644
index 0000000..5bb1baf
--- /dev/null
+++ b/ChenXiaoxu/26 Remove Duplicates from Sorted Array.md
@@ -0,0 +1,46 @@
+# 26. Remove Duplicates from Sorted Array
+
+## Problem
+
+Given a sorted array, 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 in place with constant memory.
+
+For example,
+Given input array 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 new length.
+
+
+
+---
+
+title: 26. Remove Duplicates from Sorted Array
+description: ""
+tags:
+- Arrary
+- Two Pointers
+
+categories:
+- week7
+
+---
+
+
+## Solution
+
+**java**
+
+```java
+ public int removeDuplicates(int[] nums) {
+ if (nums.length == 0 || nums.length == 1)
+ return nums.length;
+ int cur = 0;
+ for (int i = 1; i < nums.length; i++) {
+ if (nums[i] != nums[cur]) {
+ nums[++cur] = nums[i];
+ }
+ }
+ return cur + 1;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/264. Ugly Number II.md b/ChenXiaoxu/264. Ugly Number II.md
new file mode 100644
index 0000000..25bd4b7
--- /dev/null
+++ b/ChenXiaoxu/264. Ugly Number II.md
@@ -0,0 +1,51 @@
+# 264. Ugly Number II
+
+## Problem
+
+Write a program to find the n-th ugly number.
+
+Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
+
+Note that 1 is typically treated as an ugly number.
+
+Hint:
+
+1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
+2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
+3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
+4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
+
+## Solution
+
+**暴力求解**
+
+直观的做法是对整数进行遍历,逐步判断每个数是否是丑数
+
+**动态规划**
+
+丑数序列可以拆为以下子列表:
+1x2,2x2,3x2,4x2...nextUgly*2...
+1x3,2x3,3x3,4x3...nextUgly*3...
+1x5,2x5,3x5,4x5...nextUgly*5...
+采用合并有序链表的方法,从三个子列表中逐步获取最小的丑数。
+```java
+ public int nthUglyNumber(int n) {
+ List ugly = new ArrayList(n);
+ ugly.add(1);
+ int p2 = 0, p3=0, p5=0;
+ while (ugly.size()mid) right=mid;
+ else left = mid+1;
+ }
+ return left;
+ }
+```
+
+**go**
+```go
+
+```
+### 方法二
+
+参考 : https://leetcode.com/discuss/61086/java-time-and-space-solution-similar-find-loop-in-linkedlist
\ No newline at end of file
diff --git a/ChenXiaoxu/3. Longest Substring Without Repeating Characters.md b/ChenXiaoxu/3. Longest Substring Without Repeating Characters.md
new file mode 100644
index 0000000..1b49482
--- /dev/null
+++ b/ChenXiaoxu/3. Longest Substring Without Repeating Characters.md
@@ -0,0 +1,76 @@
+# [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
+
+## Problem
+Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
+
+tag:
+
+## Solution
+
+最简单的方法是暴力破解,找出以每一个字母开头的最大字串, 时间复杂度为O(N2)
+
+假设,在一次计算的过程中,字串开始位置为p,在j位置遇到重复字母b, 重复的位置为i, i=i) {
+ i = charMap[s.charAt(j)]+1;
+ }
+ charMap[s.charAt(j)] = j;
+ maxLen = Math.max(j-i+1, maxLen);
+ }
+ return maxLen;
+ }
+```
diff --git a/ChenXiaoxu/33 Search in Rotated Sorted Array.md b/ChenXiaoxu/33 Search in Rotated Sorted Array.md
new file mode 100644
index 0000000..474e656
--- /dev/null
+++ b/ChenXiaoxu/33 Search in Rotated Sorted Array.md
@@ -0,0 +1,49 @@
+# 46. Permutations
+
+## Problem
+
+Suppose a sorted array is rotated at some pivot unknown to you beforehand.
+
+(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
+
+You are given a target value to search. If found in the array return its index, otherwise return -1.
+
+You may assume no duplicate exists in the array.
+
+tags:
+- array
+- binary search
+
+similar problems:
+
+- (M) Search in Rotated Sorted Array II
+- (M) Find Minimum in Rotated Sorted Array
+
+
+## Solution
+
+```java
+public int search(int[] nums, int target) {
+ if (nums == null || nums.length == 0)
+ return -1;
+ int left = 0, right = nums.length - 1;
+ while (left <= right) {
+ int mid = left + (right - left) / 2;
+ if (target == nums[mid])
+ return mid;
+
+ if (nums[mid] >= nums[left]) {
+ if (nums[left] <= target && target <= nums[mid])
+ right = mid - 1;
+ else
+ left = mid + 1;
+ } else {
+ if (nums[mid] < target && target <= nums[right])
+ left = mid + 1;
+ else
+ right = mid - 1;
+ }
+ }
+ return -1;
+}
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/bs/Solution.java b/ChenXiaoxu/34 Search for a Range.md
similarity index 53%
rename from ChenXiaoxu/LeetCode/src.main.java/bs/Solution.java
rename to ChenXiaoxu/34 Search for a Range.md
index 6b798b7..57de799 100644
--- a/ChenXiaoxu/LeetCode/src.main.java/bs/Solution.java
+++ b/ChenXiaoxu/34 Search for a Range.md
@@ -1,8 +1,26 @@
-package bs;
+#34. Search for a Range
-public class Solution {
+## Problem
- // 34 search for range
+Given a sorted array of integers, find the starting and ending position of a given target value.
+
+Your algorithm's runtime complexity must be in the order of O(log n).
+
+If the target is not found in the array, return [-1, -1].
+
+For example,
+Given [5, 7, 7, 8, 8, 10] and target value 8,
+return [3, 4].
+
+tag:
+- array
+- binary search
+
+## Solution
+
+**java**
+
+```java
public int[] searchRange(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l <= r) {
@@ -22,4 +40,4 @@ public int[] searchRange(int[] nums, int target) {
}
return new int[] { -1, -1 };
}
-}
+```
diff --git a/ChenXiaoxu/38. Count and Say.md b/ChenXiaoxu/38. Count and Say.md
new file mode 100644
index 0000000..d824bd3
--- /dev/null
+++ b/ChenXiaoxu/38. Count and Say.md
@@ -0,0 +1,48 @@
+# 38. Count and Say
+
+
+## Problem
+
+The count-and-say sequence is the sequence of integers beginning as follows:
+1, 11, 21, 1211, 111221, ...
+
+1 is read off as "one 1" or 11.
+11 is read off as "two 1s" or 21.
+21 is read off as "one 2, then one 1" or 1211.
+Given an integer n, generate the nth sequence.
+
+tag:
+- string
+
+## Solution
+
+**java**
+```java
+ public String countAndSay(int n) {
+ if (n <= 0)
+ return "";
+ String pre = "1";
+ for (int i = 2; i <= n; i++) {
+ StringBuffer next = new StringBuffer();
+ int count = 1;
+ for (int j = 1; j < pre.length(); j++) {
+ if (pre.charAt(j) == pre.charAt(j - 1))
+ count++;
+ else {
+ next.append(count);
+ next.append(pre.charAt(j - 1));
+ count = 1;
+ }
+ }
+ next.append(count);
+ next.append(pre.charAt(pre.length() - 1));
+ pre = next.toString();
+ }
+ return pre;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/42. Trapping Rain Water.md b/ChenXiaoxu/42. Trapping Rain Water.md
new file mode 100644
index 0000000..6ff7eff
--- /dev/null
+++ b/ChenXiaoxu/42. Trapping Rain Water.md
@@ -0,0 +1,50 @@
+# 42. Trapping Rain Water
+
+## Problem
+
+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.
+
+For example,
+Given ```[0,1,0,2,1,0,1,3,2,1,2,1]```, return``` 6```.
+
+
+
+The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
+
+tag:
+- two pointers
+
+## Solution
+
+思路很简单,逐个分析每个槽能trap water的容量,然后进行累加即为总容量。关键在于算法的优化。
+
+每个槽的容量为其左边最高与右边最高中较低的一个与该槽高度的差值。
+
+常规的做法是进行遍历,对每个槽的容量计算,时间复杂度为O(N2).
+
+优化: 从左到右遍历纪录每个槽左边的最大值,从右到左遍历纪录每个槽最右边的最大值,时间复杂度O(2N)
+
+
+**java**
+```java
+ public int trap(int[] height) {
+ int[] mostHeight = new int[height.length];
+ int max = 0, sum = 0;
+ for(int i=0; i=0; i--) {
+ mostHeight[i] = Math.min(max, mostHeight[i]);
+ if(max0 ? (mostHeight[i]-height[i]) :0 ;
+ }
+ return sum;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/43. Multiply Strings.md b/ChenXiaoxu/43. Multiply Strings.md
new file mode 100644
index 0000000..35233a1
--- /dev/null
+++ b/ChenXiaoxu/43. Multiply Strings.md
@@ -0,0 +1,39 @@
+# [43. Multiply Strings](https://leetcode.com/problems/multiply-strings/)
+
+## Question
+Given two numbers represented as strings, return multiplication of the numbers as a string.
+
+Note: The numbers can be arbitrarily large and are non-negative.
+tag:
+- 模拟
+
+## [Solution](https://leetcode.com/discuss/71593/easiest-java-solution-with-graph-explanation)
+从右到做扫描num1,num2, 对每一对数字执行乘法操作, 逐步累加。 处理过程如图所示,
+
+ ```num1[i]*num2[j] 相乘结果的下标为[i+j, i+j+1]```
+
+
+
+
+**java**
+```java
+ public String multiply(String num1, String num2) {
+ int len1 = num1.length(), len2 = num2.length();
+ int[] res = new int[len1 + len2];
+ for (int i = 0; i < len1; i++)
+ for (int j = 0; j < len2; j++) {
+ int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
+ int sum = mul + res[i + j + 1];
+ res[i + j] += sum / 10;
+ res[i + j + 1] = sum % 10;
+ }
+ StringBuffer sb = new StringBuffer();
+ for (int p : res) if (!(sb.length()==0 && p==0)) sb.append(p);
+ return sb.length() == 0 ? "0" : sb.toString();
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/46 Permutations.md b/ChenXiaoxu/46 Permutations.md
new file mode 100644
index 0000000..4ccab50
--- /dev/null
+++ b/ChenXiaoxu/46 Permutations.md
@@ -0,0 +1,79 @@
+# 46. Permutations
+
+## Problem
+
+Given a collection of distinct numbers, return all possible permutations.
+
+For example,
+[1,2,3] have the following permutations:
+[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
+
+tags:
+- backtracking
+- 排列与组合
+
+
+
+## Solution
+
+- 直接生成(递归/回溯)
+- 邻位互换
+- 序数法
+- 字典序法
+- 轮转法
+
+### 递归
+n个不重复数的全排列:
+n=1 即为当前排列
+n>1 先排第一位 则Pn = iPn-1 (i=1,2,3,...n)
+
+**go**
+```go
+func Permtuate(res *[][]int, nums []int, begin int, end int) {
+ if begin == end {
+ *res = append(*res, nums)
+ } else {
+ for i := begin; i < end; i++ {
+ nums[begin], nums[i] = nums[i], nums[begin]
+ Permtuate(res, nums, begin+1, end)
+ nums[begin], nums[i] = nums[i], nums[begin]
+ }
+ }
+}
+```
+**java**
+```java
+public class Solution {
+
+ //46. Permutations
+ public List> permute(int[] nums) {
+ List> res = new ArrayList>();
+ recursive(res, nums, 0, nums.length);
+ return res;
+ }
+
+ void recursive(List> res, int[] nums, int begin, int end) {
+ if (end == begin) {
+ List li = new ArrayList(nums.length);
+ for(Integer num:nums){
+ li.add(num);
+ }
+ res.add(li);
+ } else {
+ for (int i = begin; i < end; i++) {
+ swap(nums, begin, i);
+ recursive(res, nums, begin + 1, end);
+ swap(nums, i, begin);
+ }
+ }
+ }
+
+ void swap(int[] nums, int i, int j) {
+ int tmp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = tmp;
+ }
+
+ //47. Permutations II
+}
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/47 Permutations II.md b/ChenXiaoxu/47 Permutations II.md
new file mode 100644
index 0000000..14156f0
--- /dev/null
+++ b/ChenXiaoxu/47 Permutations II.md
@@ -0,0 +1,91 @@
+# 47. Permutations II
+
+##Problem
+
+Given a collection of numbers that might contain duplicates, return all possible unique permutations.
+
+For example,
+[1,1,2] have the following unique permutations:
+[1,1,2], [1,2,1], and [2,1,1].
+
+tags:
+- backtracking
+- 包含重复元素的全排列
+
+## Solution
+
+参考:《组合数学》
+
+- 直接生成(递归/回溯)
+- 邻位互换
+- 序数法
+- 字典序法
+- 轮转法
+
+### 递归
+
+参考全排列的递归公式(46 Permutations.md),以重复元素作为前缀的均算作一个排列
+即:
+
+n=1 排列为自身
+n>1 Pn = iPi (i为序列中不重复的元素,Pi表示以i作为前缀的全排列)
+
+**java**
+```java
+public class Solution {
+ public List> permuteUnique(int[] nums) {
+ List> res = new ArrayList>();
+ recur(res, nums, 0, nums.length);
+ return res;
+ }
+
+ void recur(List> res, int[] nums, int begin, int end) {
+ if(begin==end) {
+ List li = new ArrayList(nums.length);
+ for(Integer num:nums) {
+ li.add(num);
+ }
+ res.add(li);
+ } else {
+ for(int i=begin;ibegin; i--) {
+ if(nums[end]==nums[i-1]) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
+```
+
+### 字典序法
+
+**字典序:**
+对于两个序列 a1...ak和b1...bk,若存在t,使得ai=bi,at < bt,则序列
+a1...ak < b1...bk
+
+**生成算法**
+
+设P是1...n的一个全排列,P = P1P2...Pj-1PjPj+1...Pk-1PkPk+1...Pn
+
+1. 找出j=max{i|Pi < Pi+1}, k=max{i|Pi <=> Pj}
+2. 对换Pj,Pk
+3. Pj+1...Pk-1PkPk+1...Pn
+
+**未完待续。。。**
\ No newline at end of file
diff --git a/ChenXiaoxu/49. Group Anagrams.md b/ChenXiaoxu/49. Group Anagrams.md
new file mode 100644
index 0000000..422ef94
--- /dev/null
+++ b/ChenXiaoxu/49. Group Anagrams.md
@@ -0,0 +1,62 @@
+# 49. Group Anagrams
+
+
+## Problem
+
+Given an array of strings, group anagrams together.
+
+For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
+Return:
+
+```
+[
+ ["ate", "eat","tea"],
+ ["nat","tan"],
+ ["bat"]
+]
+```
+
+Note:
+For the return value, each inner list's elements must follow the lexicographic order.
+All inputs will be in lower-case.
+
+tag:
+
+## Solution
+
+Anagram,回文构词法,指由颠倒字母顺序构成的单词,字母的种类和数目没有改变,只是改变了字母顺序,因此排序后相同。
+
+用哈希表存储同构变形词的词根,对单词表进行遍历,在哈希表中查找同构的词根,并添到列表中。
+
+注意结果按字母序输出,Java可采用Collections.sort实现。
+
+
+**java**
+```java
+ public List> groupAnagrams(String[] strs) {
+ List> res = new ArrayList>();
+ Map> map = new HashMap>();
+ for (String str : strs) {
+ char[] tmp = str.toCharArray();
+ Arrays.sort(tmp);
+ String key = new String(tmp);
+ List li = map.get(key);
+ if (li == null) {
+ li = new ArrayList();
+ }
+ li.add(str);
+ map.put(key, li);
+ }
+ for (String key : map.keySet()) {
+ List value = map.get(key);
+ Collections.sort(value);
+ res.add(value);
+ }
+ return res;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/backtracking/nqueens_51/Solution.java b/ChenXiaoxu/51 N-Queens.md
similarity index 65%
rename from ChenXiaoxu/LeetCode/src.main.java/backtracking/nqueens_51/Solution.java
rename to ChenXiaoxu/51 N-Queens.md
index 88bc9a0..3aab5b1 100644
--- a/ChenXiaoxu/LeetCode/src.main.java/backtracking/nqueens_51/Solution.java
+++ b/ChenXiaoxu/51 N-Queens.md
@@ -1,8 +1,39 @@
-package backtracking.nqueens_51;
+# 51. N-Queens
-import java.util.ArrayList;
-import java.util.List;
+## Problem
+The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
+
+
+
+Given an integer n, return all distinct solutions to the n-queens puzzle.
+
+Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
+
+For example,
+There exist two distinct solutions to the 4-queens puzzle:
+
+[
+ [".Q..", // Solution 1
+ "...Q",
+ "Q...",
+ "..Q."],
+
+ ["..Q.", // Solution 2
+ "Q...",
+ "...Q",
+ ".Q.."]
+]
+
+tag:
+- math
+- binary search
+
+## Solution
+
+**java**
+
+```java
public class Solution {
public List> solveNQueens(int n) {
@@ -53,4 +84,5 @@ public static void main(String[] args) {
int n = 4;
}
-}
\ No newline at end of file
+}
+```
diff --git a/ChenXiaoxu/6. ZigZag Conversion.md b/ChenXiaoxu/6. ZigZag Conversion.md
new file mode 100644
index 0000000..4b7b98f
--- /dev/null
+++ b/ChenXiaoxu/6. ZigZag Conversion.md
@@ -0,0 +1,63 @@
+# 6. ZigZag Conversion
+
+## Problem
+
+The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
+
+```
+P A H N
+A P L S I I G
+Y I R
+```
+And then read line by line: "PAHNAPLSIIGYIR"
+Write the code that will take a string and make this conversion given a number of rows:
+
+```string convert(string text, int nRows);```
+
+convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
+
+tag:
+- String
+- 归纳推理
+
+## Solution
+
+我们用数字表示每一行字母的下标
+
+```
+ 0 2n-2
+ 1 2n-3 2n-1
+ 2 2n-4 2n-0
+... ... ...
+n-3 n+1
+n-2 n
+n-1
+```
+该排列具有周期性, 周期为```2n-2```, 设为```m```;
+在每一个周期内,第一行和最后一行均有一个元素,其余每行有两个元素,具有以下规律:
+第i行第一个元素为```j=km+i(k=0,1,2,3...)```, 第二个元素和第一个元素相差m-2i, 故第二个元素为j+m-2i。
+
+具体实现如下:
+
+**java**
+```java
+ public String convert(String s, int numRows) {
+ if (numRows <= 1)
+ return s;
+ int cycle = 2 * numRows - 2;
+ StringBuffer res = new StringBuffer();
+ for (int i = 0; i < numRows; i++) {
+ for (int j = i; j < s.length(); j += cycle) {
+ if (j < s.length())
+ res.append(s.charAt(j));
+ if (i < numRows-1 && i > 0) {
+ int p = j + cycle - 2 * i;
+ if (p < s.length())
+ res.append(s.charAt(p));
+ }
+ }
+ }
+ return res.toString();
+ }
+```
+
diff --git a/ChenXiaoxu/61. Rotate List.md b/ChenXiaoxu/61. Rotate List.md
new file mode 100644
index 0000000..0bda297
--- /dev/null
+++ b/ChenXiaoxu/61. Rotate List.md
@@ -0,0 +1,70 @@
+# [61. Rotate List](https://leetcode.com/problems/rotate-list/)
+
+## Problem
+
+Given a list, rotate the list to the right by k places, where k is non-negative.
+
+For example:
+Given ```1->2->3->4->5->NULL``` and ```k = 2```,
+return``` 4->5->1->2->3->NULL```.
+
+tag:
+- linkedlist
+- two pointers
+
+## Solution
+
+把链表后K个节点旋转到前面; 研究了半天才搞懂题意 :(
+
+采用快慢指针
+
+```
+
+1. 定义两个指针p,q, 初始化分别指向dummyHead, 然后移动p,q开始旋转的位置和链表尾
+注意k可能超出链表长度, 因此要对链表长度取余, 即p移动```len-k%len```步,
+dummyHead->1->2->3->4->5->null
+ ^ ^
+ | |
+ p q
+
+
+2. 对链表进行旋转操作
+
+表尾指向表头: q-next = dummyHead->next;
+表头指向新的表头: dummyHead->next = p->next;
+新的表尾指向空: p-next = null
+
+dummyHead-4->5->1->2->3->null
+ ^ ^
+ | |
+ q p
+```
+
+
+**java**
+```java
+ public ListNode rotateRight(ListNode head, int k) {
+ if(head==null) return head;
+ ListNode dummyHead = new ListNode(0);
+ dummyHead.next = head;
+ ListNode p=dummyHead,q=dummyHead;
+
+ int len = 0;
+ while(q.next!=null) {
+ q = q.next;
+ len++;
+ }
+ for(int i=0; i1?1:0
+```
+
+**java**
+```java
+ public String addBinary(String a, String b) {
+ StringBuffer res = new StringBuffer();
+ int carry = 0;
+ int i, j;
+ for (i = a.length() - 1, j = b.length() - 1; i >= 0 && j >= 0; i--, j--) {
+ int m = a.charAt(i) - '0', n = b.charAt(j) - '0';
+ int r = m ^ n ^ carry;
+ carry = (m + n + carry) > 1 ? 1 : 0;
+ res.append(r);
+ }
+ if (i < 0 && j >= 0) {
+ while (j >= 0) {
+ res.append((b.charAt(j) - '0') ^ carry);
+ carry = (b.charAt(j) - '0') & carry;
+ j--;
+ }
+ if (carry == 1)
+ res.append(carry);
+ } else if (j < 0 && i >= 0) {
+ while (i >= 0) {
+ res.append((a.charAt(i) - '0') ^ carry);
+ carry = (a.charAt(i) - '0') & carry;
+ i--;
+ }
+ if (carry == 1)
+ res.append(carry);
+ } else {
+ if (carry > 0)
+ res.append(1);
+ }
+ return res.reverse().toString();
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/math/Solution.java b/ChenXiaoxu/69 Sqrt(x).md
similarity index 57%
rename from ChenXiaoxu/LeetCode/src.main.java/math/Solution.java
rename to ChenXiaoxu/69 Sqrt(x).md
index 7dde53b..a5872d2 100644
--- a/ChenXiaoxu/LeetCode/src.main.java/math/Solution.java
+++ b/ChenXiaoxu/69 Sqrt(x).md
@@ -1,6 +1,22 @@
-package math;
+#69. Sqrt(x)
-public class Solution {
+## Problem
+
+Implement int sqrt(int x).
+
+Compute and return the square root of x.
+
+Subscribe to see which companies asked this question
+
+tag:
+- math
+- binary search
+
+## Solution
+
+**java**
+
+```java
public int mySqrt(int x) {
long l = 0, r = x / 2 + 1;
@@ -15,4 +31,4 @@ else if (m * m > x)
}
return (int) r;
}
-}
+```
diff --git a/ChenXiaoxu/75. Sort Colors.md b/ChenXiaoxu/75. Sort Colors.md
new file mode 100644
index 0000000..aeee1ce
--- /dev/null
+++ b/ChenXiaoxu/75. Sort Colors.md
@@ -0,0 +1,43 @@
+# [75. Sort Colors](https://leetcode.com/problems/sort-colors/)
+
+## Problem
+
+Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
+
+Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
+
+tag:
+- two pointers
+
+## Solution
+
+### 通用的解法
+
+通用的解法是遍历一次数组,统计各种颜色的个数,然后重写数组。该方法需要遍历两边数组,但能决绝超过三种颜色问题。
+
+### 一次遍历
+
+定义两个指针,分隔三种颜色, 一次遍历, 把第一种颜色移到数组最前面, 把第三种颜色移到最后面。
+
+**java**
+```java
+ public void sortColors(int[] nums) {
+ int p1 = 0, p2 = nums.length-1;
+ for(int i=0; i<=p2; ) {
+ if(nums[i]==0) swap(nums, i++, p1++);
+ else if(nums[i]==2) swap(nums, i, p2--);
+ else i++;
+ }
+ }
+
+ public void swap(int[] nums, int i, int j) {
+ int tmp = nums[i];
+ nums[i] = nums[j];
+ nums[j] = tmp;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/79 Word Search.md b/ChenXiaoxu/79 Word Search.md
new file mode 100644
index 0000000..2884651
--- /dev/null
+++ b/ChenXiaoxu/79 Word Search.md
@@ -0,0 +1,26 @@
+# 79. Word Search
+
+## Probmel
+
+Given a 2D board and a word, find if the word exists in the grid.
+
+The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
+
+For example,
+Given board =
+
+ [
+ ['A','B','C','E'],
+ ['S','F','C','S'],
+ ['A','D','E','E']
+ ]
+
+word = "ABCCED", -> returns true,
+word = "SEE", -> returns true,
+word = "ABCB", -> returns false.
+
+tag:
+- array
+- backtracking
+
+## Solution
\ No newline at end of file
diff --git a/ChenXiaoxu/79. Word Search.md b/ChenXiaoxu/79. Word Search.md
new file mode 100644
index 0000000..5db1de6
--- /dev/null
+++ b/ChenXiaoxu/79. Word Search.md
@@ -0,0 +1,59 @@
+# 79. Word Search
+
+## Problem
+
+Given a 2D board and a word, find if the word exists in the grid.
+
+The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
+
+For example,
+Given board =
+```
+[
+ ['A','B','C','E'],
+ ['S','F','C','S'],
+ ['A','D','E','E']
+]
+```
+word = "ABCCED", -> returns true,
+word = "SEE", -> returns true,
+word = "ABCB", -> returns false.
+
+tag:
+- bfs
+
+## Solution
+
+以面板中的任一字符作为起始节点,进行深度优先搜索,如果匹配,返回True,如果所有节点的搜索结果均不匹配,返回false。
+
+**java**
+```java
+ public boolean exist(char[][] board, String word) {
+ boolean isVisit[][] = new boolean[board.length][board[0].length];
+ for (int i = 0; i < board.length; i++)
+ for (int j = 0; j < board[0].length; j++)
+ if (search(board, isVisit, word, 0, i, j))
+ return true;
+ return false;
+ }
+
+ private boolean search(char[][] board, boolean[][] isVisit, String word, int index, int i, int j) {
+ if (index == word.length())
+ return true;
+ if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || isVisit[i][j]
+ || board[i][j] != word.charAt(index))
+ return false;
+ isVisit[i][j] = true;
+ boolean result = search(board, isVisit, word, index + 1, i + 1, j)
+ || search(board, isVisit, word, index + 1, i - 1, j)
+ || search(board, isVisit, word, index + 1, i, j + 1)
+ || search(board, isVisit, word, index + 1, i, j - 1);
+ isVisit[i][j] = false;
+ return result;
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/8 String to Integer (atoi).md b/ChenXiaoxu/8 String to Integer (atoi).md
new file mode 100644
index 0000000..a70506f
--- /dev/null
+++ b/ChenXiaoxu/8 String to Integer (atoi).md
@@ -0,0 +1,66 @@
+# 8. String to Integer (atoi)
+
+## Problem
+
+Implement atoi to convert a string to an integer.
+
+Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
+
+Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
+
+Update (2015-02-10):
+The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
+
+spoilers alert... click to show requirements for atoi.
+
+Requirements for atoi:
+The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
+
+The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
+
+If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
+
+If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
+
+tag:
+- math
+- string
+
+
+## Solution
+
+**java**
+
+```java
+ public int myAtoi(String str) {
+ if (str == null || str.isEmpty())
+ return 0;
+
+ str = str.trim();
+
+ int index = 0;
+ int sign = 1;
+ long result = 0;
+ if (str.charAt(index) == '-') {
+ sign = -1;
+ index++;
+ } else if (str.charAt(index) == '+') {
+ index++;
+ }
+ for (; index < str.length(); index++) {
+ if (!Character.isDigit(str.charAt(index)))
+ break;
+ result = result * 10 + (str.charAt(index) - '0');
+ if (result > Integer.MAX_VALUE)
+ break;
+ }
+
+ result = sign * result;
+ if (result >= Integer.MAX_VALUE)
+ return Integer.MAX_VALUE;
+ if (result <= Integer.MIN_VALUE)
+ return Integer.MIN_VALUE;
+
+ return (int) result;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/80 Remove Duplicates from Sorted Array II.md b/ChenXiaoxu/80 Remove Duplicates from Sorted Array II.md
new file mode 100644
index 0000000..af2e6dc
--- /dev/null
+++ b/ChenXiaoxu/80 Remove Duplicates from Sorted Array II.md
@@ -0,0 +1,35 @@
+# 80. Remove Duplicates from Sorted Array II
+
+## Problem
+
+Follow up for "Remove Duplicates":
+What if duplicates are allowed at most twice?
+
+For example,
+Given sorted array nums = [1,1,1,2,2,3],
+
+Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
+
+
+tags:
+- Array
+- Two Pointers
+
+## Solution
+
+**java**
+
+```java
+ public int removeDuplicates(int[] nums) {
+ if (nums.length <= 2)
+ return nums.length;
+ int cur = 2, pre = 1;
+ while (cur < nums.length) {
+ if (nums[cur] == nums[pre] && nums[cur] == nums[pre - 1])
+ cur++;
+ else
+ nums[++pre] = nums[cur++];
+ }
+ return pre + 1;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/81 Search in Rotated Sorted Array II.md b/ChenXiaoxu/81 Search in Rotated Sorted Array II.md
new file mode 100644
index 0000000..5dc7ef5
--- /dev/null
+++ b/ChenXiaoxu/81 Search in Rotated Sorted Array II.md
@@ -0,0 +1,47 @@
+# 46. Permutations
+
+## Problem
+
+Follow up for "Search in Rotated Sorted Array":
+What if duplicates are allowed?
+
+Would this affect the run-time complexity? How and why?
+
+Write a function to determine if a given target is in the array.
+
+tags:
+- array
+- binary search
+
+similar problems:
+
+- (M) Search in Rotated Sorted Array
+
+
+## Solution
+
+```java
+public int search(int[] nums, int target) {
+ if (nums == null || nums.length == 0)
+ return -1;
+ int left = 0, right = nums.length - 1;
+ while (left <= right) {
+ int mid = left + (right - left) / 2;
+ if (target == nums[mid])
+ return mid;
+
+ if (nums[mid] >= nums[left]) {
+ if (nums[left] <= target && target <= nums[mid])
+ right = mid - 1;
+ else
+ left = mid + 1;
+ } else {
+ if (nums[mid] < target && target <= nums[right])
+ left = mid + 1;
+ else
+ right = mid - 1;
+ }
+ }
+ return -1;
+}
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/duplicates/II/Solution.java b/ChenXiaoxu/82 Remove Duplicates from Sorted List II.md
similarity index 57%
rename from ChenXiaoxu/LeetCode/src.main.java/linkedlist/duplicates/II/Solution.java
rename to ChenXiaoxu/82 Remove Duplicates from Sorted List II.md
index a89c787..9321419 100644
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/duplicates/II/Solution.java
+++ b/ChenXiaoxu/82 Remove Duplicates from Sorted List II.md
@@ -1,8 +1,21 @@
-package linkedlist.duplicates.II;
+# 83. Remove Duplicates from Sorted List
-import linkedlist.ListNode;
+## Problem
-public class Solution {
+Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
+
+For example,
+Given 1->2->3->3->4->4->5, return 1->2->5.
+Given 1->1->1->2->3, return 2->3.
+
+tag:
+- linked list
+
+## Solution
+
+**java**
+
+```java
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
@@ -21,4 +34,4 @@ public ListNode deleteDuplicates(ListNode head) {
}
return dummyNode.next;
}
-}
+```
diff --git a/ChenXiaoxu/83 Remove Duplicates from Sorted List.md b/ChenXiaoxu/83 Remove Duplicates from Sorted List.md
new file mode 100644
index 0000000..f2a869e
--- /dev/null
+++ b/ChenXiaoxu/83 Remove Duplicates from Sorted List.md
@@ -0,0 +1,31 @@
+# 83. Remove Duplicates from Sorted List
+
+## Problem
+
+Given a sorted linked list, delete all duplicates such that each element appear only once.
+
+For example,
+Given 1->1->2, return 1->2.
+Given 1->1->2->3->3, return 1->2->3.
+
+tag:
+- linked list
+
+## Solution
+
+**java**
+
+```java
+ public ListNode deleteDuplicates(ListNode head) {
+ if (head==null||head.next==null)
+ return head;
+ ListNode p = head;
+ while (p.next != null) {
+ if (p.val == p.next.val)
+ p.next = p.next.next;
+ else
+ p = p.next;
+ }
+ return head;
+ }
+```
diff --git a/ChenXiaoxu/86. Partition List.md b/ChenXiaoxu/86. Partition List.md
new file mode 100644
index 0000000..444855c
--- /dev/null
+++ b/ChenXiaoxu/86. Partition List.md
@@ -0,0 +1,44 @@
+# [86. Partition List](https://leetcode.com/problems/partition-list/)
+
+## Problem
+
+Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
+
+You should preserve the original relative order of the nodes in each of the two partitions.
+
+For example,
+Given 1->4->3->2->5->2 and x = 3,
+return 1->2->2->4->3->5.
+
+tag:
+- two pointers
+
+## Solution
+
+
+
+**java**
+```java
+ public ListNode partition(ListNode head, int x) {
+ ListNode dummySmallHead = new ListNode(0), dummyBigHead = new ListNode(0);
+ ListNode p1 = dummySmallHead, p2 = dummyBigHead;
+ while(head!=null) {
+ if(head.val= 0){
+ if(p2<0 || (p1>=0 && nums1[p1]>nums2[p2])) nums1[p--] = nums1[p1--];//注意p1>=0写在nums1[p1]>nums2[p2]之前
+ else nums1[p--] = nums2[p2--];
+ }
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/91. Decode Ways.md b/ChenXiaoxu/91. Decode Ways.md
new file mode 100644
index 0000000..a31c9af
--- /dev/null
+++ b/ChenXiaoxu/91. Decode Ways.md
@@ -0,0 +1,56 @@
+# 91. Decode Ways
+
+## Problem
+
+A message containing letters from A-Z is being encoded to numbers using the following mapping:
+
+'A' -> 1
+'B' -> 2
+...
+'Z' -> 26
+Given an encoded message containing digits, determine the total number of ways to decode it.
+
+For example,
+Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
+
+The number of ways decoding "12" is 2.
+
+tag:
+
+- dp
+
+## Solution
+
+满足最优子结构和子问题重叠, 采用动态规划算法:
+
+设```f[i]```表示字串长度为i的解码方式数量,```f[0]=0```, 则:
+
+设```f[i]=0```
+
+最后一位字符作为一个编码(```s.charAt(i-1)>'0'```): ```f[i] += f[i-1]```
+最后两位字符作为一个编码(```i>1 且 "10"= '0')
+ res[i] += res[i-1];
+ if (i>1 && Integer.parseInt(s.substring(i - 2, i)) >= 10 && Integer.parseInt(s.substring(i - 2, i)) <= 26)
+ res[i] += res[i - 2];
+ }
+ return res[length];
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/94. Binary Tree Inorder Traversal.md b/ChenXiaoxu/94. Binary Tree Inorder Traversal.md
new file mode 100644
index 0000000..02288f9
--- /dev/null
+++ b/ChenXiaoxu/94. Binary Tree Inorder Traversal.md
@@ -0,0 +1,63 @@
+# 94. Binary Tree Inorder Traversal
+
+## Problem
+
+Given a binary tree, return the inorder traversal of its nodes' values.
+
+```
+/**
+ * Definition for a binary tree node.
+ * public class TreeNode {
+ * int val;
+ * TreeNode left;
+ * TreeNode right;
+ * TreeNode(int x) { val = x; }
+ * }
+ */
+```
+
+tag:
+
+## Solution
+
+### 递归
+
+**java**
+```java
+ public List inorderTraversal(TreeNode root, List res) {
+ if(root!=null) {
+ inorderTraversal(root.left, res);
+ res.add(root.val);
+ inorderTraversal(root.right, res);
+ }
+ return res;
+ }
+```
+
+**go**
+```go
+
+```
+
+### 迭代
+
+``` java
+ public List inorderTraversal(TreeNode root) {
+ List res = new LinkedList();
+ Stack s = new Stack();
+ TreeNode p = root;
+ while(p!=null || !s.isEmpty()) {
+ //push root to stack, traverse left subtree
+ if(p!=null) {
+ s.push(p);
+ p=p.left;
+ } else {
+ //pop root, visit root node, and travese right subtree
+ p = s.pop();
+ res.add(p.val);
+ p = p.right;
+ }
+ }
+ return res;
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/96. Unique Binary Search Trees.md b/ChenXiaoxu/96. Unique Binary Search Trees.md
new file mode 100644
index 0000000..f00256c
--- /dev/null
+++ b/ChenXiaoxu/96. Unique Binary Search Trees.md
@@ -0,0 +1,43 @@
+# 96. Unique Binary Search Trees
+
+## Problem
+
+Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
+
+For example,
+Given n = 3, there are a total of 5 unique BST's.
+```
+ 1 3 3 2 1
+ \ / / / \ \
+ 3 2 1 1 3 2
+ / / \ \
+ 2 1 2 3
+```
+
+tag:
+- 组合问题
+- [卡特兰数](https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0)
+- dp
+
+## Solution
+
+该题即求解异构BST的个数:
+有序数列1...n中以i为根的BST的左子树包含[1...i-1],用右子树包含[i+1...n],以i为根的异构BST个数为左子树与右子树的乘积,故异构BST总数为
+```
+ f(n) = f(0)*f(n-1)+f(1)f(n-2)...f(n-1)*f(0)
+```
+
+**java**
+
+```java
+ public int numTrees(int n) {
+ int[] res = new int[n + 1];
+ res[0] = res[1] = 1;
+ for (int i = 2; i <= n; i++) {
+ for (int k = 1; k <= i; k++) {
+ res[i] += res[k - 1] * res[i - k];
+ }
+ }
+ return res[n];
+ }
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/98. Validate Binary Search Tree.md b/ChenXiaoxu/98. Validate Binary Search Tree.md
new file mode 100644
index 0000000..16428b4
--- /dev/null
+++ b/ChenXiaoxu/98. Validate Binary Search Tree.md
@@ -0,0 +1,40 @@
+# 98. Validate Binary Search Tree
+
+## Problem
+Given a binary tree, determine if it is a valid binary search tree (BST).
+
+Assume a BST is defined as follows:
+
+The left subtree of a node contains only nodes with keys less than the node's key.
+The right subtree of a node contains only nodes with keys greater than the node's key.
+Both the left and right subtrees must also be binary search trees.
+
+tag:
+
+## Solution
+中序遍历,然后判断是否为升序
+
+**java**
+```java
+ public boolean isValidBST(TreeNode root) {
+ List res = new ArrayList();
+ inOrderTraverse(root, res);
+ for(int i=1; i res) {
+ if(root!=null){
+ inOrderTraverse(root.left, res);
+ res.add(root.val);
+ inOrderTraverse(root.right, res);
+ }
+ }
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/.classpath b/ChenXiaoxu/LeetCode/.classpath
deleted file mode 100644
index 0757c68..0000000
--- a/ChenXiaoxu/LeetCode/.classpath
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
diff --git a/ChenXiaoxu/LeetCode/.project b/ChenXiaoxu/LeetCode/.project
deleted file mode 100644
index 4fdb69a..0000000
--- a/ChenXiaoxu/LeetCode/.project
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
- LeetCode
-
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
-
- org.eclipse.jdt.core.javanature
-
-
diff --git a/ChenXiaoxu/LeetCode/.settings/org.eclipse.jdt.core.prefs b/ChenXiaoxu/LeetCode/.settings/org.eclipse.jdt.core.prefs
deleted file mode 100644
index 3a21537..0000000
--- a/ChenXiaoxu/LeetCode/.settings/org.eclipse.jdt.core.prefs
+++ /dev/null
@@ -1,11 +0,0 @@
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
-org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
-org.eclipse.jdt.core.compiler.compliance=1.8
-org.eclipse.jdt.core.compiler.debug.lineNumber=generate
-org.eclipse.jdt.core.compiler.debug.localVariable=generate
-org.eclipse.jdt.core.compiler.debug.sourceFile=generate
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.8
diff --git a/ChenXiaoxu/LeetCode/bin/.gitignore b/ChenXiaoxu/LeetCode/bin/.gitignore
deleted file mode 100644
index 93b90f8..0000000
--- a/ChenXiaoxu/LeetCode/bin/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-/math/
-/bs/
diff --git a/ChenXiaoxu/LeetCode/bin/array/duplicates/I/Solution.class b/ChenXiaoxu/LeetCode/bin/array/duplicates/I/Solution.class
deleted file mode 100644
index 173b2e8..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/array/duplicates/I/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/array/duplicates/II/Solution.class b/ChenXiaoxu/LeetCode/bin/array/duplicates/II/Solution.class
deleted file mode 100644
index 6368963..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/array/duplicates/II/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/array/rotated/I/Solution.class b/ChenXiaoxu/LeetCode/bin/array/rotated/I/Solution.class
deleted file mode 100644
index e69ac1d..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/array/rotated/I/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/backtracking/nqueens_51/Solution.class b/ChenXiaoxu/LeetCode/bin/backtracking/nqueens_51/Solution.class
deleted file mode 100644
index c3980bc..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/backtracking/nqueens_51/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/List.class b/ChenXiaoxu/LeetCode/bin/linkedlist/List.class
deleted file mode 100644
index 68c6e7b..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/List.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/ListNode.class b/ChenXiaoxu/LeetCode/bin/linkedlist/ListNode.class
deleted file mode 100644
index 363e4a0..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/ListNode.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/I/Solution.class b/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/I/Solution.class
deleted file mode 100644
index 41d0ea3..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/I/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/II/Solution.class b/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/II/Solution.class
deleted file mode 100644
index b93642b..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/II/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/SolutionTest.class b/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/SolutionTest.class
deleted file mode 100644
index 99ebf3f..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/duplicates/SolutionTest.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/Solution.class b/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/Solution.class
deleted file mode 100644
index 8ca6f80..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/Solution.md b/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/Solution.md
deleted file mode 100644
index c140e7b..0000000
--- a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/Solution.md
+++ /dev/null
@@ -1,10 +0,0 @@
-# Merge k Sorted Lists
-
-## Naive
-
-**O(nk^2)**
-
-
-## Divide and Conquer
-
-## Heap
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/SolutionTest.class b/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/SolutionTest.class
deleted file mode 100644
index 239fb82..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/klist/SolutionTest.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/twolist/Solution.class b/ChenXiaoxu/LeetCode/bin/linkedlist/merge/twolist/Solution.class
deleted file mode 100644
index d9a99d1..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/twolist/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/twolist/SolutionTest.class b/ChenXiaoxu/LeetCode/bin/linkedlist/merge/twolist/SolutionTest.class
deleted file mode 100644
index e96502c..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/linkedlist/merge/twolist/SolutionTest.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/str/atoi/README.md b/ChenXiaoxu/LeetCode/bin/str/atoi/README.md
deleted file mode 100644
index b8ff215..0000000
--- a/ChenXiaoxu/LeetCode/bin/str/atoi/README.md
+++ /dev/null
@@ -1,20 +0,0 @@
-# String to Integer (atoi)
-
-## Problem
-
-The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
-
-The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
-
-If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
-
-If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
-
-## Method
-
-Traverse the string and convert it to integer, mainly pay attention to **corner case**.
-
-- whitespace
-- plus or minus sign
-- range
-- non-numerical character
diff --git a/ChenXiaoxu/LeetCode/bin/str/atoi/Solution.class b/ChenXiaoxu/LeetCode/bin/str/atoi/Solution.class
deleted file mode 100644
index 05cde77..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/str/atoi/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/str/palindrome/Solution.class b/ChenXiaoxu/LeetCode/bin/str/palindrome/Solution.class
deleted file mode 100644
index 2f7ddcf..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/str/palindrome/Solution.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/bin/str/palindrome/SolutionTest.class b/ChenXiaoxu/LeetCode/bin/str/palindrome/SolutionTest.class
deleted file mode 100644
index 94eb345..0000000
Binary files a/ChenXiaoxu/LeetCode/bin/str/palindrome/SolutionTest.class and /dev/null differ
diff --git a/ChenXiaoxu/LeetCode/src.main.java/array/duplicates/I/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/array/duplicates/I/Solution.java
deleted file mode 100644
index 2bfc64e..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/array/duplicates/I/Solution.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package array.duplicates.I;
-
-public class Solution {
-
- public int removeDuplicates(int[] nums) {
- if (nums.length == 0 || nums.length == 1)
- return nums.length;
- int cur = 0;
- for (int i = 1; i < nums.length; i++) {
- if (nums[i] != nums[cur]) {
- nums[++cur] = nums[i];
- }
- }
- return cur + 1;
- }
-
- public static void main(String[] args) {
- Solution so = new Solution();
- int[] nums = { 1, 2, 2, 3, 3, 4 };
- System.out.println(so.removeDuplicates(nums));
- }
-}
diff --git a/ChenXiaoxu/LeetCode/src.main.java/array/duplicates/II/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/array/duplicates/II/Solution.java
deleted file mode 100644
index 122c38b..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/array/duplicates/II/Solution.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package array.duplicates.II;
-
-public class Solution {
-
- public int removeDuplicates(int[] nums) {
- if (nums.length <= 2)
- return nums.length;
- int cur = 2, pre = 1;
- while (cur < nums.length) {
- if (nums[cur] == nums[pre] && nums[cur] == nums[pre - 1])
- cur++;
- else
- nums[++pre] = nums[cur++];
- }
- return pre + 1;
- }
-}
diff --git a/ChenXiaoxu/LeetCode/src.main.java/array/rotated/I/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/array/rotated/I/Solution.java
deleted file mode 100644
index 7394b29..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/array/rotated/I/Solution.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package array.rotated.I;
-
-public class Solution {
-
- public int search(int[] nums, int target) {
- if (nums == null || nums.length == 0)
- return -1;
- int left = 0, right = nums.length - 1;
- while (left <= right) {
- int mid = left + (right - left) / 2;
- if (target == nums[mid])
- return mid;
-
- if (nums[mid] >= nums[left]) {
- if (nums[left] <= target && target <= nums[mid])
- right = mid - 1;
- else
- left = mid + 1;
- } else {
- if (nums[mid] < target && target <= nums[right])
- left = mid + 1;
- else
- right = mid - 1;
- }
- }
- return -1;
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
-
-}
diff --git a/ChenXiaoxu/LeetCode/src.main.java/array/rotated/II/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/array/rotated/II/Solution.java
deleted file mode 100644
index e69de29..0000000
diff --git a/ChenXiaoxu/LeetCode/src.main.java/backtracking/nqueens_51/Solution.md b/ChenXiaoxu/LeetCode/src.main.java/backtracking/nqueens_51/Solution.md
deleted file mode 100644
index e69de29..0000000
diff --git a/ChenXiaoxu/LeetCode/src.main.java/backtracking/wordsearch/I/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/backtracking/wordsearch/I/Solution.java
deleted file mode 100644
index e69de29..0000000
diff --git a/ChenXiaoxu/LeetCode/src.main.java/bs/README.md b/ChenXiaoxu/LeetCode/src.main.java/bs/README.md
deleted file mode 100644
index e69de29..0000000
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/List.java b/ChenXiaoxu/LeetCode/src.main.java/linkedlist/List.java
deleted file mode 100644
index 21a7cdb..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/List.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package linkedlist;
-
-public class List {
- public ListNode head;
-
- public List() {
- head = null;
- }
-
- public void insert(int val) {
- ListNode tmp = new ListNode(val);
- if (head != null) {
- tmp.next = head;
- head = tmp;
- } else {
- head = tmp;
- }
- }
-
- void delete(ListNode node) {
-
- }
-}
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/ListNode.java b/ChenXiaoxu/LeetCode/src.main.java/linkedlist/ListNode.java
deleted file mode 100644
index 0a7a07e..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/ListNode.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package linkedlist;
-
-public class ListNode {
- public int val;
- public ListNode next;
-
- public ListNode(int x) {
- val = x;
- }
-}
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/duplicates/I/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/linkedlist/duplicates/I/Solution.java
deleted file mode 100644
index c1167a1..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/duplicates/I/Solution.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package linkedlist.duplicates.I;
-
-import linkedlist.ListNode;
-
-public class Solution {
- public ListNode deleteDuplicates(ListNode head) {
- if (head==null||head.next==null)
- return head;
- ListNode p = head;
- while (p.next != null) {
- if (p.val == p.next.val)
- p.next = p.next.next;
- else
- p = p.next;
- }
- return head;
- }
-}
diff --git a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/klist/Solution.md b/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/klist/Solution.md
deleted file mode 100644
index c140e7b..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/linkedlist/merge/klist/Solution.md
+++ /dev/null
@@ -1,10 +0,0 @@
-# Merge k Sorted Lists
-
-## Naive
-
-**O(nk^2)**
-
-
-## Divide and Conquer
-
-## Heap
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.main.java/math/README.md b/ChenXiaoxu/LeetCode/src.main.java/math/README.md
deleted file mode 100644
index f5d0089..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/math/README.md
+++ /dev/null
@@ -1,18 +0,0 @@
-#��ֵ��������Ŀ(NUMERIC CALCULATION)
-
-Ӧע�����¼������⣺
-
-1. һ����ö��ַ����н���
-2. ע���������͡����Ⱥͱ߽�����
-
-# Problem set
-
-## 69. Sqrt(x)
-
-�Ǹ�����n��ƽ������ȡֵ��ΧΪ0~n������ȷn/2+1), �ڴ˷�Χ�ڽ��ж�������������⣬��ע��ȡ���ͱ߽����⣨��������֮���п��ܳ���int��Χ����
-ʱ�临�Ӷ� O(logn), �ռ临�Ӷ�O(1)��
-
-## 50. Pow(x, n)
-
-
-
diff --git a/ChenXiaoxu/LeetCode/src.main.java/str/atoi/README.md b/ChenXiaoxu/LeetCode/src.main.java/str/atoi/README.md
deleted file mode 100644
index b8ff215..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/str/atoi/README.md
+++ /dev/null
@@ -1,20 +0,0 @@
-# String to Integer (atoi)
-
-## Problem
-
-The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
-
-The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
-
-If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
-
-If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
-
-## Method
-
-Traverse the string and convert it to integer, mainly pay attention to **corner case**.
-
-- whitespace
-- plus or minus sign
-- range
-- non-numerical character
diff --git a/ChenXiaoxu/LeetCode/src.main.java/str/atoi/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/str/atoi/Solution.java
deleted file mode 100644
index 02ebe70..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/str/atoi/Solution.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package str.atoi;
-
-public class Solution {
- public int myAtoi(String str) {
- if (str == null || str.isEmpty())
- return 0;
-
- str = str.trim();
-
- int index = 0;
- int sign = 1;
- long result = 0;
- if (str.charAt(index) == '-') {
- sign = -1;
- index++;
- } else if (str.charAt(index) == '+') {
- index++;
- }
- for (; index < str.length(); index++) {
- if (!Character.isDigit(str.charAt(index)))
- break;
- result = result * 10 + (str.charAt(index) - '0');
- if (result > Integer.MAX_VALUE)
- break;
- }
-
- result = sign * result;
- if (result >= Integer.MAX_VALUE)
- return Integer.MAX_VALUE;
- if (result <= Integer.MIN_VALUE)
- return Integer.MIN_VALUE;
-
- return (int) result;
- }
-}
diff --git a/ChenXiaoxu/LeetCode/src.main.java/str/palindrome/Solution.java b/ChenXiaoxu/LeetCode/src.main.java/str/palindrome/Solution.java
deleted file mode 100644
index a4e6303..0000000
--- a/ChenXiaoxu/LeetCode/src.main.java/str/palindrome/Solution.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package str.palindrome;
-
-public class Solution {
- public boolean isPalindrome(String s) {
- if (s == null || s.isEmpty())
- return true;
- int l = 0, r = s.length() - 1;
- while (l < r) {
- if (!Character.isLetterOrDigit(s.charAt(l))) {
- l++;
- continue;
- }
- if (!Character.isLetterOrDigit(s.charAt(r))) {
- r--;
- continue;
- }
- if (Character.toLowerCase(s.charAt(l)) == Character.toLowerCase(s.charAt(r))) {
- l++;
- r--;
- } else {
- return false;
- }
- }
- return true;
- }
-}
\ No newline at end of file
diff --git a/ChenXiaoxu/LeetCode/src.test.java/linkedlist/duplicates/SolutionTest.java b/ChenXiaoxu/LeetCode/src.test.java/linkedlist/duplicates/SolutionTest.java
deleted file mode 100644
index 87e5ebd..0000000
--- a/ChenXiaoxu/LeetCode/src.test.java/linkedlist/duplicates/SolutionTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package linkedlist.duplicates;
-
-import static org.junit.Assert.*;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import linkedlist.List;
-import linkedlist.ListNode;
-import linkedlist.duplicates.I.Solution;
-
-public class SolutionTest {
-
- private Solution solution = new Solution();
-
- @Before
- public void setUp() throws Exception {
- }
-
- @Test
- public void testDeleteDuplicates() {
- List li = new List();
- li.insert(3);
- li.insert(3);
- li.insert(2);
- li.insert(1);
- li.insert(1);
- solution.deleteDuplicates(li.head);
- assertEquals(li.head.val, 1);
- assertEquals(li.head.next.val, 2);
- }
-
-}
diff --git a/ChenXiaoxu/LeetCode/src.test.java/linkedlist/merge/klist/SolutionTest.java b/ChenXiaoxu/LeetCode/src.test.java/linkedlist/merge/klist/SolutionTest.java
deleted file mode 100644
index 82e5b91..0000000
--- a/ChenXiaoxu/LeetCode/src.test.java/linkedlist/merge/klist/SolutionTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package linkedlist.merge.klist;
-
-import static org.junit.Assert.*;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import linkedlist.List;
-import linkedlist.ListNode;
-
-public class SolutionTest {
-
- private Solution solution = new Solution();
-
- @Before
- public void setUp() throws Exception {
-
- }
-
- @Test
- public void testMergeKLists() {
-
- ListNode[] lists0 = null;
- ListNode[] lists1 = new ListNode[3];
- List l1 = new List();
- List l2 = new List();
- List l3 = new List();
- l1.insert(11);
- l1.insert(8);
- l1.insert(8);
- l1.insert(3);
-
- l2.insert(20);
- l2.insert(15);
- l2.insert(11);
- l2.insert(9);
- l2.insert(8);
- l2.insert(6);
- l2.insert(2);
-
- l3.insert(20);
- l3.insert(1);
- lists1[0] = l1.head;
- lists1[1] = l2.head;
- lists1[2] = l3.head;
-
- ListNode[] list2 = new ListNode[2];
- List l20 = new List();
- List l21 = new List();
- l21.insert(1);
- list2[0] = l20.head;
- list2[1] = l21.head;
-
- assertEquals(null, solution.mergeKLists(lists0));
-
- assertEquals(1, solution.mergeKLists(lists1).val);
-
- assertEquals(1, solution.mergeKLists(list2).val);
- }
-
-}
diff --git a/ChenXiaoxu/LeetCode/src.test.java/linkedlist/merge/twolist/SolutionTest.java b/ChenXiaoxu/LeetCode/src.test.java/linkedlist/merge/twolist/SolutionTest.java
deleted file mode 100644
index f8ac98f..0000000
--- a/ChenXiaoxu/LeetCode/src.test.java/linkedlist/merge/twolist/SolutionTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package linkedlist.merge.twolist;
-
-import static org.junit.Assert.*;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import linkedlist.List;
-import linkedlist.merge.twolist.Solution;
-
-public class SolutionTest {
-
- private Solution solution = new Solution();
- private List l1 = new List();
- private List l2 = new List();
-
- @Before
- public void setUp() throws Exception {
- }
-
- @Test
- public void testMergeTwoLists() {
- // null, null
- assertEquals(null, solution.mergeTwoLists(l1.head, l2.head));
- // 3,5,8,11
- // 2,6,8,9,11,15,20
-
- l1.insert(11);
-
- assertEquals(11, solution.mergeTwoLists(l1.head, l2.head).val);
- l1.insert(8);
- l1.insert(8);
- l1.insert(3);
-
- l2.insert(20);
- l2.insert(15);
- l2.insert(11);
- l2.insert(9);
- l2.insert(8);
- l2.insert(6);
- l2.insert(2);
-
- assertEquals(2, solution.mergeTwoLists(l1.head, l2.head).val);
-
- System.out.println();
-
- }
-
-}
diff --git a/ChenXiaoxu/LeetCode/src.test.java/math/sqrt/SolutionTest.java b/ChenXiaoxu/LeetCode/src.test.java/math/sqrt/SolutionTest.java
deleted file mode 100644
index 57e050b..0000000
--- a/ChenXiaoxu/LeetCode/src.test.java/math/sqrt/SolutionTest.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package math.sqrt;
-
-import static org.junit.Assert.*;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-import math.Solution;
-
-public class SolutionTest {
-
- private Solution s = new Solution();
-
- @Before
- public void setUp() throws Exception {
- }
-
- @Test
- public void testMySqrt() {
- int[] testCase = {0,1,2,3,4,5,6,7,8,9};
- int[] expect = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
- for (int i=0; i ugly = new ArrayList(n);
+ ugly.add(1);
+ int p2 = 0, p3 = 0, p5 = 0;
+ while (ugly.size() < n) {
+ int min = min(ugly.get(p2) * 2, ugly.get(p3) * 3, ugly.get(p5) * 5);
+ if (min == ugly.get(p2) * 2)
+ p2 += 1;
+ if (min == ugly.get(p3) * 3)
+ p3 += 1;
+ if (min == ugly.get(p5) * 5)
+ p5 += 1;
+ ugly.add(min);
+ }
+ return ugly.get(n - 1);
+ }
+
+ int min(int a, int b, int c) {
+ int min = Math.min(a, b);
+ min = Math.min(min, c);
+ return min;
+ }
+
+ // 62. Unique Paths
+ public int uniquePaths(int m, int n) {
+ int[][] res = new int[m][n];
+ for (int i = 0; i < m; i++)
+ res[i][0] = 1;
+ for (int j = 0; j < n; j++)
+ res[0][j] = 1;
+ for (int i = 1; i < m; i++)
+ for (int j = 1; j < n; j++)
+ res[i][j] = res[i - 1][j] + res[i][j - 1];
+ return res[m - 1][n - 1];
+ }
+
+ // 63. Unique Paths II
+ public int uniquePathsWithObstacles(int[][] obstacleGrid) {
+ int m = obstacleGrid.length, n = obstacleGrid[0].length;
+ int[][] res = new int[m][n];
+ res[0][0] = (obstacleGrid[0][0] == 1) ? 0 : 1;
+ for (int i = 1; i < m; i++) {
+ res[i][0] = (obstacleGrid[i][0] == 1) ? 0 : res[i - 1][0];
+ }
+ for (int j = 1; j < n; j++) {
+ res[0][j] = (obstacleGrid[0][j] == 1) ? 0 : res[0][j - 1];
+ }
+ for (int i = 1; i < m; i++)
+ for (int j = 1; j < n; j++) {
+ if (obstacleGrid[i][j] == 1)
+ res[i][j] = 0;
+ else
+ res[i][j] = res[i - 1][j] + res[i][j - 1];
+ res[i][j] = (obstacleGrid[i][j] == 1) ? 0 : (res[i - 1][j] + res[i][j - 1]);
+ }
+ return res[m - 1][n - 1];
+ }
+
+ // 115. Distinct Subsequences
+ public int numDistinct(String s, String t) {
+ int m = s.length(), n = t.length();
+ int[][] f = new int[m][n];
+ for (int i = 0; i < m; i++)
+ f[i][0] = 1;
+ for (int j = 0; j < n; j++)
+ for (int i = 0; i < m && i < j; i++)
+ f[i][j] = 0;
+ for (int i = 1; i < m; i++)
+ for (int j = 1; j < n; j++) {
+ if (i == j)
+ f[i][j] = s.substring(0, i) == t.substring(0, j) ? 1 : 0;
+ else {
+ if (s.charAt(i) == s.charAt(j))
+ f[i][j] = f[i - 1][j - 1] + f[i - 1][j];
+ else
+ f[i][j] = f[i - 1][j];
+ }
+ }
+ return f[m - 1][n - 1];
+ }
+
+ // 6. ZigZag Conversion
+ public String convert(String s, int numRows) {
+ if (numRows <= 1)
+ return s;
+ int cycle = 2 * numRows - 2;
+ StringBuffer res = new StringBuffer();
+ for (int i = 0; i < numRows; i++) {
+ for (int j = i; j < s.length(); j += cycle) {
+ if (j < s.length())
+ res.append(s.charAt(j));
+ if (i < numRows - 1 && i > 0) {
+ int p = j + cycle - 2 * i;
+ if (p < s.length())
+ res.append(s.charAt(p));
+ }
+ }
+ }
+ return res.toString();
+ }
+
+ // 20. Valid Parentheses
+ public boolean isValid(String s) {
+ Stack stack = new Stack();
+ String left = "([{", right = ")]}";
+ for (int i = 0; i < s.length(); i++) {
+ if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
+ stack.push(s.charAt(i));
+ } else {
+ if (stack.isEmpty() || s.charAt(i) != right.charAt(left.indexOf(stack.peek())))
+ return false;
+ stack.pop();
+ }
+ }
+ if (!stack.isEmpty())
+ return false;
+ return true;
+ }
+
+ // 67. Add Binary
+ public String addBinary(String a, String b) {
+ StringBuffer res = new StringBuffer();
+ int carry = 0;
+ int i, j;
+ for (i = a.length() - 1, j = b.length() - 1; i >= 0 && j >= 0; i--, j--) {
+ int m = a.charAt(i) - '0', n = b.charAt(j) - '0';
+ int r = m ^ n ^ carry;
+ carry = (m + n + carry) > 1 ? 1 : 0;
+ res.append(r);
+ }
+ if (i < 0 && j >= 0) {
+ while (j >= 0) {
+ res.append((b.charAt(j) - '0') ^ carry);
+ carry = (b.charAt(j) - '0') & carry;
+ j--;
+ }
+ if (carry == 1)
+ res.append(carry);
+ } else if (j < 0 && i >= 0) {
+ while (i >= 0) {
+ res.append((a.charAt(i) - '0') ^ carry);
+ carry = (a.charAt(i) - '0') & carry;
+ i--;
+ }
+ if (carry == 1)
+ res.append(carry);
+ } else {
+ if (carry > 0)
+ res.append(1);
+ }
+ return res.reverse().toString();
+ }
+
+ // 49. Group Anagrams
+ public List> groupAnagrams(String[] strs) {
+ List> res = new ArrayList>();
+ Map> map = new HashMap>();
+ for (String str : strs) {
+ char[] tmp = str.toCharArray();
+ Arrays.sort(tmp);
+ String key = new String(tmp);
+ List li = map.get(key);
+ if (li == null) {
+ li = new ArrayList();
+ }
+ li.add(str);
+ map.put(key, li);
+ }
+ for (String key : map.keySet()) {
+ List value = map.get(key);
+ Collections.sort(value);
+ res.add(value);
+ }
+ return res;
+ }
+
+ // 165. Compare Version Numbers
+ public int compareVersion(String version1, String version2) {
+ String[] v1 = version1.split("\\."), v2 = version2.split("\\.");
+ int maxLen = Math.max(v1.length, v2.length);
+ for (int i = 0; i < maxLen; i++) {
+ int n1 = i >= v1.length ? 0 : Integer.parseInt(v1[i]);
+ int n2 = i >= v2.length ? 0 : Integer.parseInt(v2[i]);
+ if (n1 > n2)
+ return 1;
+ else if (n1 < n2)
+ return -1;
+ else
+ continue;
+ }
+ return 0;
+ }
+
+ // 38. Count and Say
+ public String countAndSay(int n) {
+ if (n <= 0)
+ return "";
+ String pre = "1";
+ for (int i = 2; i <= n; i++) {
+ StringBuffer next = new StringBuffer();
+ int count = 1;
+ for (int j = 1; j < pre.length(); j++) {
+ if (pre.charAt(j) == pre.charAt(j - 1))
+ count++;
+ else {
+ next.append(count);
+ next.append(pre.charAt(j - 1));
+ count = 1;
+ }
+ }
+ next.append(count);
+ next.append(pre.charAt(pre.length() - 1));
+ pre = next.toString();
+ }
+ return pre;
+ }
+
+ // 1. Two Sum
+ public int[] twoSum(int[] nums, int target) {
+ for (int i = 0; i < nums.length; i++) {
+ for (int j = i; j < nums.length; j++) {
+ if (nums[j] + nums[i] == target)
+ return new int[] { i, j };
+ }
+ }
+ return null;
+ }
+
+ // 91. Decode Ways
+ public int numDecodings(String s) {
+ if (s.length() == 0)
+ return 0;
+ int[] res = new int[s.length() + 1];
+ res[0] = 1;
+ for (int i = 1; i <= s.length(); i++) {
+ if (s.charAt(i - 1) > '0')
+ res[i] += res[i - 1];
+ if (i > 1 && Integer.parseInt(s.substring(i - 2, i)) >= 10 && Integer.parseInt(s.substring(i - 2, i)) <= 26)
+ res[i] += res[i - 2];
+ }
+ return res[s.length()];
+ }
+
+ // 79. Word Search
+ public boolean exist(char[][] board, String word) {
+ boolean isVisit[][] = new boolean[board.length][board[0].length];
+ for (int i = 0; i < board.length; i++)
+ for (int j = 0; j < board[0].length; j++)
+ if (search(board, isVisit, word, 0, i, j))
+ return true;
+ return false;
+ }
+
+ private boolean search(char[][] board, boolean[][] isVisit, String word, int index, int i, int j) {
+ if (index == word.length())
+ return true;
+ if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || isVisit[i][j]
+ || board[i][j] != word.charAt(index))
+ return false;
+ isVisit[i][j] = true;
+ boolean result = search(board, isVisit, word, index + 1, i + 1, j)
+ || search(board, isVisit, word, index + 1, i - 1, j)
+ || search(board, isVisit, word, index + 1, i, j + 1)
+ || search(board, isVisit, word, index + 1, i, j - 1);
+ isVisit[i][j] = false;
+ return result;
+ }
+
+ // 319. Bulb Switcher
+ public int bulbSwitch(int n) {
+ boolean[] bulbs = new boolean[n];
+ for (int i = 0; i < n; i++) {
+ for (int j = i; j < n; j = j + i + 1)
+ bulbs[j] = !bulbs[j];
+ }
+ int count = 0;
+ for (int i = 0; i < n; i++) {
+ if (bulbs[i])
+ count++;
+ }
+ return count;
+ }
+
+ // 224. Basic Calculator
+ public int calculate(String s) {
+ int sign = 1, res = 0, len = s.length();
+ Stack stack = new Stack();
+ for (int i = 0; i < len; i++) {
+ if (Character.isDigit(s.charAt(i))) {
+ int sum = s.charAt(i) - '0';
+ while (i + 1 < len && Character.isDigit(s.charAt(i + 1))) {
+ sum = sum * 10 + s.charAt(i + 1) - '0';
+ i++;
+ }
+ res += sum * sign;
+ } else if (s.charAt(i) == '+') {
+ sign = 1;
+ } else if (s.charAt(i) == '-') {
+ sign = -1;
+ } else if (s.charAt(i) == '(') {
+ stack.push(res);
+ stack.push(sign);
+ res = 0;
+ sign = 1;
+ } else if (s.charAt(i) == ')') {
+ res = res * stack.pop() + stack.pop();
+ }
+ }
+ return res;
+ }
+
+ // 43. Multiply Strings
+ public String multiply(String num1, String num2) {
+ int len1 = num1.length(), len2 = num2.length();
+ int[] res = new int[len1 + len2];
+ for (int i = 0; i < len1; i++)
+ for (int j = 0; j < len2; j++) {
+ int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
+ int sum = mul + res[i + j + 1];
+ res[i + j] += sum / 10;
+ res[i + j + 1] = sum % 10;
+ }
+ StringBuffer sb = new StringBuffer();
+ for (int p : res)
+ if (!(sb.length() == 0 && p == 0))
+ sb.append(p);
+ return sb.length() == 0 ? "0" : sb.toString();
+ }
+
+ // 166. Fraction to Recurring Decimal
+ public String fractionToDecimal(int numerator, int denominator) {
+ StringBuffer res = new StringBuffer();
+ String sign = (numerator < 0 == denominator < 0 || numerator == 0) ? "" : "-";
+ long num = Math.abs((long) numerator), den = Math.abs((long) denominator);
+ res.append(sign);
+ res.append(num / den);
+ long remainder = num % den;
+ if (remainder == 0)
+ return res.toString();
+ res.append(".");
+ HashMap map = new HashMap();
+ while (!map.containsKey(remainder)) {
+ map.put(remainder, res.length());
+ res.append(10 * remainder / den);
+ remainder = 10 * remainder % den;
+ }
+ int index = map.get(remainder);
+ res.insert(index, "(");
+ res.append(")");
+ return res.toString().replace("(0)", "");
+ }
+
+ // 204. Count Primes
+ public int countPrimes(int n) {
+ boolean[] isPrimes = new boolean[n];
+ for (int i = 2; i < n; i++)
+ isPrimes[i] = true;
+ for (int i = 2; i * i < n; i++) {
+ if (isPrimes[i]) {
+ for (int j = i * i; j < n; j += i)
+ isPrimes[j] = false;
+ }
+ }
+ int count = 0;
+ for (int i = 2; i < n; i++) {
+ if (isPrimes[i])
+ count++;
+ }
+ return count;
+ }
+
+ public ListNode rotateRight(ListNode head, int k) {
+ if (head == null)
+ return head;
+ ListNode dummyHead = new ListNode(0);
+ dummyHead.next = head;
+ ListNode p = head, q = head;
+
+ int len = 1;
+ while (q.next != null) {
+ q = q.next;
+ len++;
+ }
+ for (int i = 0; i < len - k % len; i++)
+ p = p.next;
+
+ q.next = dummyHead.next;
+ dummyHead.next = p.next;
+ p.next = null;
+
+ return dummyHead.next;
+ }
+
+ // 151. Reverse Words in a String
+ public String reverseWords(String s) {
+ StringBuilder reversed = new StringBuilder();
+ int j = s.length();
+ for (int i = s.length() - 1; i >= 0; i--) {
+ if (s.charAt(i) == ' ') {
+ j = i;
+ } else if (i == 0 || s.charAt(i - 1) == ' ') {
+ if (reversed.length() != 0) {
+ reversed.append(' ');
+ }
+ reversed.append(s.substring(i, j));
+ }
+ }
+ return reversed.toString();
+ }
+
+ // 3. Longest Substring Without Repeating Characters
+ public int lengthOfLongestSubstring(String s) {
+ boolean[] exists = new boolean[256];
+ int i = 0, maxLen = 0, len = s.length();
+ for (int j = 0; j < len; j++) {
+ while (exists[s.charAt(j)]) {
+ exists[s.charAt(i++)] = false;
+ }
+ exists[s.charAt(j)] = true;
+ maxLen = Math.max(j - i + 1, maxLen);
+ }
+ return maxLen;
+ }
+
+ public static void main(String[] args) {
+ Solution s = new Solution();
+ System.out.println(s.uniquePaths(2, 3));
+ int obstacleGrid[][] = new int[][] { { 0 }, { 1 } };
+ System.out.println(s.uniquePathsWithObstacles(obstacleGrid));
+
+ System.out.println(s.numDistinct("abbc", "abc"));
+
+ System.out.println(s.convert("PAYPALISHIRING", 3));
+
+ System.out.println(s.isValid("["));
+
+ System.out.println(s.addBinary("1", "1"));
+
+ System.out.println(s.compareVersion("1.0", "1"));
+
+ System.out.println(s.twoSum(new int[] { 2, 3, 4 }, 6).toString());
+
+ System.out.println(s.numDecodings("101"));
+
+ char[][] board = { "ABCE".toCharArray(), "SFES".toCharArray(), "ADEE".toCharArray() };
+ System.out.println(s.exist(board, "ABCESEEEFS"));
+ char[][] board1 = { "bb".toCharArray(), "ab".toCharArray(), "ba".toCharArray() };
+ System.out.println(s.exist(board1, "a"));
+
+ for (int i = 0; i < 50; i++) {
+ System.out.println(i + ": " + s.bulbSwitch(i));
+ }
+
+ System.out.println(s.fractionToDecimal(-1, -2147483648));
+
+ ListNode head = new ListNode(1);
+ ListNode node2 = new ListNode(2);
+ ListNode node3 = new ListNode(3);
+ ListNode node4 = new ListNode(4);
+ ListNode node5 = new ListNode(5);
+ head.next = node2;
+ node2.next = node3;
+ node3.next = node4;
+ node4.next = node5;
+ node5.next = null;
+
+ s.rotateRight(head, 4).toString();
+
+ System.out.println(s.reverseWords("the sky is blue"));
+ }
+
+}
+
+class ListNode {
+ int val;
+ ListNode next;
+
+ ListNode(int x) {
+ val = x;
+ };
+}
\ No newline at end of file
diff --git "a/ChenXiaoxu/template - \345\211\257\346\234\254 (3).md" "b/ChenXiaoxu/template - \345\211\257\346\234\254 (3).md"
new file mode 100644
index 0000000..9a69efa
--- /dev/null
+++ "b/ChenXiaoxu/template - \345\211\257\346\234\254 (3).md"
@@ -0,0 +1,17 @@
+# Title
+
+## Problem
+
+tag:
+
+## Solution
+
+**java**
+```java
+
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git "a/ChenXiaoxu/template - \345\211\257\346\234\254 (4).md" "b/ChenXiaoxu/template - \345\211\257\346\234\254 (4).md"
new file mode 100644
index 0000000..9a69efa
--- /dev/null
+++ "b/ChenXiaoxu/template - \345\211\257\346\234\254 (4).md"
@@ -0,0 +1,17 @@
+# Title
+
+## Problem
+
+tag:
+
+## Solution
+
+**java**
+```java
+
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/ChenXiaoxu/template.md b/ChenXiaoxu/template.md
new file mode 100644
index 0000000..9a69efa
--- /dev/null
+++ b/ChenXiaoxu/template.md
@@ -0,0 +1,17 @@
+# Title
+
+## Problem
+
+tag:
+
+## Solution
+
+**java**
+```java
+
+```
+
+**go**
+```go
+
+```
\ No newline at end of file
diff --git a/CuiGuangfan/src/com/xingkong/BinaryTreeZigzagLevelOrderTraversal_103.java b/CuiGuangfan/src/com/xingkong/BinaryTreeZigzagLevelOrderTraversal_103.java
new file mode 100644
index 0000000..049f266
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/BinaryTreeZigzagLevelOrderTraversal_103.java
@@ -0,0 +1,109 @@
+package com.xingkong;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.Stack;
+
+/**
+ * @author cuiguangfan 736068048@qq.com:
+ * @version create time��2016��1��6�� ����10:29:04 class description
+ */
+public class BinaryTreeZigzagLevelOrderTraversal_103 {
+ private static class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+ TreeNode(int x) {
+ val = x;
+ }
+ @Override
+ public String toString() {
+ return "TreeNode [val=" + val+ "]";
+ }
+ }
+
+ public List> zigzagLevelOrder(TreeNode root) {
+ List> list=new ArrayList>();
+ if(root==null) return list;
+ Queue queue=new LinkedList();
+ Stack stack=new Stack();
+ queue.add(root);
+ stack.push(root);
+ int flag=0;
+ //ż���ζ����У������ζ�ջ
+ while(!queue.isEmpty()&&!stack.isEmpty()){
+ List tempList=new ArrayList();
+ Queue currentQueue=new LinkedList();
+ Stack currentStack=new Stack();
+ if(flag%2==0){
+ while(!queue.isEmpty()){
+ TreeNode tnValue = queue.poll();
+ tempList.add(tnValue.val);
+ TreeNode tnNext =tnValue;
+ if(tnNext.left!=null){
+ currentQueue.add(tnNext.left);
+ currentStack.push(tnNext.left);
+ }
+ if(tnNext.right!=null){
+ currentQueue.add(tnNext.right);
+ currentStack.push(tnNext.right);
+ }
+ }
+ }else{
+ while(!stack.isEmpty()){
+ TreeNode tnValue = stack.pop();
+ tempList.add(tnValue.val);
+ TreeNode tnNext =queue.poll();
+ if(tnNext.left!=null){
+ currentQueue.add(tnNext.left);
+ currentStack.push(tnNext.left);
+ }
+ if(tnNext.right!=null){
+ currentQueue.add(tnNext.right);
+ currentStack.push(tnNext.right);
+ }
+
+
+ }
+ }
+ list.add(tempList);
+ queue=currentQueue;
+ stack=currentStack;
+ flag++;
+ }
+ return list;
+ }
+
+ public static void main(String[] args) {
+// TreeNode tn1=new TreeNode(3);
+// TreeNode tn2=new TreeNode(9);
+// TreeNode tn3=new TreeNode(20);
+// TreeNode tn4=new TreeNode(11);
+// TreeNode tn5=new TreeNode(4);
+// TreeNode tn6=new TreeNode(15);
+// TreeNode tn7=new TreeNode(7);
+// TreeNode tn8=new TreeNode(8);
+// TreeNode tn9=new TreeNode(6);
+// TreeNode tn10=new TreeNode(13);
+// TreeNode tn11=new TreeNode(14);
+// TreeNode tn12=new TreeNode(22);
+// TreeNode tn13=new TreeNode(23);
+// tn1.left=tn2;
+// tn1.right=tn3;
+// tn2.left=tn4;
+// tn2.right=tn5;
+// tn3.left=tn6;
+// tn3.right=tn7;
+// tn4.left=tn8;
+// tn4.right=tn9;
+// tn5.left=tn10;
+// tn6.right=tn11;
+// tn10.left=tn12;
+// tn11.right=tn13;
+ BinaryTreeZigzagLevelOrderTraversal_103 btzlo=new BinaryTreeZigzagLevelOrderTraversal_103();
+ System.out.println(btzlo.zigzagLevelOrder(null));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/ConstructBinaryTreeFromPreorderandInorderTraversal_105.java b/CuiGuangfan/src/com/xingkong/ConstructBinaryTreeFromPreorderandInorderTraversal_105.java
new file mode 100644
index 0000000..5597c4a
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/ConstructBinaryTreeFromPreorderandInorderTraversal_105.java
@@ -0,0 +1,65 @@
+package com.xingkong;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author cuiguangfan 736068048@qq.com:
+ * @version create time��2016��1��7�� ����9:32:10
+ * class description
+ */
+public class ConstructBinaryTreeFromPreorderandInorderTraversal_105 {
+// private Map preOrderMap=new HashMap();
+ private Map inOrderMap=new HashMap();
+ public static class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+ TreeNode(int x) {
+ val = x;
+ }
+ @Override
+ public String toString() {
+ return "TreeNode [val=" + val+ "]";
+ }
+ }
+
+ /**
+ * @param preorder
+ * @param inorder
+ * @param rootIndex preorder�е���һ��Ԫ��
+ * @param startIndex ���εݹ��inorder�еĿ�ʼ�±�
+ * @param endIndex ���εݹ��inorder�еĽ����±�
+ * @return
+ */
+ public TreeNode backTrace(int[] preorder, int[] inorder,int rootIndex,int startIndex,int endIndex){
+ if(startIndex>endIndex) return null;
+ int midIndex=inOrderMap.get(preorder[rootIndex]);
+ TreeNode root=new TreeNode(inorder[midIndex]);
+ if(startIndex==endIndex)
+ return root;
+ int leftLength=midIndex-startIndex;//ͨ�����������ȡ������
+ int rightLength=endIndex-midIndex;//ͨ�����������ȡ������
+ //��ȡ����������
+ root.left=backTrace(preorder,inorder,rootIndex+1,midIndex-leftLength,midIndex-1);
+ //��ȡ����������
+ root.right=backTrace(preorder,inorder,rootIndex+leftLength+1,midIndex+1,midIndex+rightLength);
+ return root;
+ }
+ public TreeNode buildTree(int[] preorder, int[] inorder) {
+ for(int i=0;i countSmaller(int[] nums) {
+ List returnList=new ArrayList();
+ if(nums.length==0) return returnList;
+ returnList.add(0);
+ TreeNode treeRoot = new TreeNode(nums[nums.length - 1]);
+ for (int i = nums.length - 2; i >= 0; i--) {
+ TreeNode currentRoot = treeRoot;
+ Integer count=0;
+ while (currentRoot!=null) {
+ if (nums[i] > currentRoot.value) {
+ count+=currentRoot.leftCount+currentRoot.thisCount;
+ if (currentRoot.right != null) {
+ currentRoot = currentRoot.right;
+ } else {
+ currentRoot.right = new TreeNode(nums[i]);
+ break;
+ }
+ } else if(nums[i] < currentRoot.value){
+ currentRoot.leftCount++;
+ if (currentRoot.left != null) {
+ currentRoot = currentRoot.left;
+ } else {
+ currentRoot.left = new TreeNode(nums[i]);
+ break;
+ }
+ }else{
+ count+=currentRoot.leftCount;
+ currentRoot.thisCount++;
+ break;
+ }
+ }
+ returnList.add(count);
+ }
+ Collections.reverse(returnList);
+ return returnList;
+ }
+
+ public static void main(String[] args) {
+ CountOfSmallerNumbersAfterSelf_315 csn=new CountOfSmallerNumbersAfterSelf_315();
+ System.out.println(csn.countSmaller(new int[]{26,78,27,100,33,67,90,23,66,5,38,7,35,23,52,22,83,51,98,69,81,32,78,28,94,13,2,97,3,76,99,51,9,21,84,66,65,36,100,41}));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/HouseRobber_198.java b/CuiGuangfan/src/com/xingkong/HouseRobber_198.java
new file mode 100644
index 0000000..f979080
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/HouseRobber_198.java
@@ -0,0 +1,33 @@
+package com.xingkong;
+
+/**
+ * @author cuiguangfan 736068048@qq.com:
+ * @version create time��2016��1��20�� ����8:44:09 class description
+ */
+public class HouseRobber_198 {
+
+ /**
+ * ת�Ʒ��̣�money[(i)=max{money(i-1),money(i-2)+nums[i]}
+ * @param nums
+ * @return
+ */
+ public int rob(int[] nums) {
+ if (nums.length == 0)
+ return 0;
+ int[] money = new int[nums.length];
+ money[0] = nums[0];
+ if (nums.length >= 2)
+ money[1] = nums[1] > nums[0] ? nums[1] : nums[0];
+ for (int i = 2; i < nums.length; i++) {
+ money[i] = money[i - 1] >= (money[i - 2] + nums[i]) ? money[i - 1] : (money[i - 2] + nums[i]);
+ }
+ return money[nums.length - 1];
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ HouseRobber_198 hr = new HouseRobber_198();
+ System.out.println(hr.rob(new int[] {}));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/LongestPalindromicSubstring.java b/CuiGuangfan/src/com/xingkong/LongestPalindromicSubstring.java
new file mode 100644
index 0000000..163cb58
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/LongestPalindromicSubstring.java
@@ -0,0 +1,74 @@
+package com.xingkong;
+
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��1��4�� ����8:35:38
+* class description
+*/
+public class LongestPalindromicSubstring {
+ //�����ִ�ͳ�������ᵼ�³�ʱ�����Ӷ�Ϊo(n3)
+ public String longestPalindrome1(String s) {
+ int maxPalinLength = 0;
+ String longestPalindrome = null;
+ int length = s.length();
+
+ // check all possible sub strings
+ for (int i = 0; i < length; i++) {
+ for (int j = i + 1; j < length; j++) {
+ int len = j - i;
+ String curr = s.substring(i, j + 1);
+ if (isPalindrome(curr)) {
+ if (len > maxPalinLength) {
+ longestPalindrome = curr;
+ maxPalinLength = len;
+ }
+ }
+ }
+ }
+
+ return longestPalindrome;
+ }
+ private static boolean isPalindrome(String s) {
+ for (int i = 0; i < s.length() - 1; i++) {
+ if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+ public String longestPalindrome(String s){
+ char[] chars=s.toCharArray();
+ boolean[][] records=new boolean[chars.length][chars.length];
+ for(int i=0;i=j) records[i][j]=true;//������=����Ϊ[i][i]Ҳ�ǻ���
+ else records[i][j]=false;
+ }
+ }
+ int maxLengthOfPalind=1;
+ int begin = 0;
+ int end=chars.length-1;
+ for (int j = 1; j < chars.length; j++) {
+ for (int i = 0; i < j; i++) {
+ if(chars[i]==chars[j]){
+ records[i][j]=records[i+1][j-1];
+ if(records[i][j]==true){
+ if(j-i+1>maxLengthOfPalind){
+ maxLengthOfPalind=j-i+1;
+ begin=i;
+ end=j;
+ }
+ }
+ }else{
+ records[i][j]=false;
+ }
+ }
+ }
+ return s.substring(begin,end+1);//end+1����Ϊbegin��end�ǻ��ģ����Խ�ȡ��ĩβӦ��+1
+ }
+ public static void main(String[] args){
+ LongestPalindromicSubstring lps=new LongestPalindromicSubstring();
+ System.out.println(lps.longestPalindrome("aaabaaaa"));
+ }
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_11_Container_With_Most_Water.java b/CuiGuangfan/src/com/xingkong/N_11_Container_With_Most_Water.java
new file mode 100644
index 0000000..92ee4e4
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_11_Container_With_Most_Water.java
@@ -0,0 +1,34 @@
+package com.xingkong;
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��6�� ����10:48:22
+* class description
+*/
+public class N_11_Container_With_Most_Water {
+ public static int maxArea(int[] height) {
+ int left=0;
+ int right=height.length-1;
+ int k=0;
+ int result=0;
+ while(leftnewResult?result:newResult;
+ if(height[left]left&&height[k]<=height[right])k--;
+ right=k;
+ }
+ }
+ return result;
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ System.out.println(N_11_Container_With_Most_Water.maxArea(new int[]{1,1}));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_12_Integer_to_Roman.java b/CuiGuangfan/src/com/xingkong/N_12_Integer_to_Roman.java
new file mode 100644
index 0000000..7de14d6
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_12_Integer_to_Roman.java
@@ -0,0 +1,27 @@
+package com.xingkong;
+
+/**
+ * @author cuiguangfan 736068048@qq.com:
+ * @version create time��2016��3��7�� ����9:47:33 class description
+ */
+public class N_12_Integer_to_Roman {
+ public static String intToRoman(int num) {
+ String c[][]=new String[][]{{"0","I","II","III","IV","V","VI","VII","VIII","IX"},{"0","X","XX","XXX","XL","L","LX"
+ ,"LXX","LXXX","XC"},{"0","C","CC","CCC","CD","D",
+ "DC","DCC","DCCC","CM"},{"0","M","MM","MMM"}};
+ int t=1;
+ int tmp=num;
+ String st = "";
+ if(tmp/1000!=0) st+=c[3][tmp/1000];
+ if((tmp%1000)/100!=0) st+=c[2][(tmp%1000)/100];
+ if((tmp%100)/10!=0) st+=c[1][(tmp%100)/10];
+ if(tmp%10!=0) st+=c[0][tmp%10];
+ return st;
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ System.out.println(N_12_Integer_to_Roman.intToRoman(1980));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_133_Clone_Graph.java b/CuiGuangfan/src/com/xingkong/N_133_Clone_Graph.java
new file mode 100644
index 0000000..09cc434
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_133_Clone_Graph.java
@@ -0,0 +1,89 @@
+package com.xingkong;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+
+/**
+ * @author cuiguangfan 736068048@qq.com:
+ * @version create time��2016��3��16�� ����10:21:42 class description
+ */
+public class N_133_Clone_Graph {
+ private static class UndirectedGraphNode {
+ int label;
+ List neighbors;
+ UndirectedGraphNode(int x) {
+ label = x;
+ neighbors = new ArrayList();
+ }
+ };
+
+ /**
+ * ����map�������õ��ص������ģ�û����һ���½ڵ㣬����map��ţ�ÿ������ͬlabel�Ľڵ㣬�������mapȡ��������������������
+ * @param node
+ * @return
+ */
+ public UndirectedGraphNode cloneGraph_0(UndirectedGraphNode node) {//20ms 1.7%
+ if(node==null) return null;
+ Set labelSet=new HashSet();
+ Queue queue=new LinkedList();
+ queue.add(node);
+ Map map=new HashMap();
+ map.put(node.label, new UndirectedGraphNode(node.label));
+ while(!queue.isEmpty()){
+ UndirectedGraphNode tempNode=queue.poll();
+ labelSet.add(tempNode.label);
+ List resultList=new ArrayList();
+ List tempList=tempNode.neighbors;
+ if(tempList!=null){
+ for(UndirectedGraphNode temp:tempList){
+ if(!labelSet.contains(temp.label)){
+ queue.add(temp);
+
+ }
+ if(!map.containsKey(temp.label)) map.put(temp.label, new UndirectedGraphNode(temp.label));
+ resultList.add(map.get(temp.label));
+ }
+ //if(!map.containsKey(tempNode.label)) map.put(tempNode.label, new UndirectedGraphNode(tempNode.label));
+ map.get(tempNode.label).neighbors=resultList;
+ }
+ }
+ return map.get(node.label);
+ }
+ public UndirectedGraphNode cloneGraph_1(UndirectedGraphNode node) {// 10ms 31.35% ȥ����set����Ϊset��map����������ͬ��
+ if(node==null) return null;
+ Queue queue=new LinkedList();
+ queue.add(node);
+ Map map=new HashMap();
+ map.put(node.label, new UndirectedGraphNode(node.label));
+ while(!queue.isEmpty()){
+ UndirectedGraphNode tempNode=queue.poll();
+ List resultList=new ArrayList();
+ List tempList=tempNode.neighbors;
+ if(tempList!=null){
+ for(UndirectedGraphNode temp:tempList){
+ if(!map.containsKey(temp.label)){
+ queue.add(temp);
+ map.put(temp.label, new UndirectedGraphNode(temp.label));
+ }
+ resultList.add(map.get(temp.label));
+ }
+ map.get(tempNode.label).neighbors=resultList;
+ }
+ }
+ return map.get(node.label);
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ N_133_Clone_Graph n=new N_133_Clone_Graph();
+ UndirectedGraphNode node=new UndirectedGraphNode(-1);
+ System.out.println(n.cloneGraph_1(node));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_136_Single_Number.java b/CuiGuangfan/src/com/xingkong/N_136_Single_Number.java
new file mode 100644
index 0000000..f20fde3
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_136_Single_Number.java
@@ -0,0 +1,20 @@
+package com.xingkong;
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��9�� ����11:29:29
+* class description
+*/
+public class N_136_Single_Number {
+ public static int singleNumber(int[] nums) {
+ int result=nums[0];
+ for(int i=1;i>i)&1;
+ }
+ if(array[i]%3!=0)
+ result+=1< result=new ArrayList();
+ public List wordBreak(String s, Set wordDict) {//timeout
+ Map> map=new HashMap>();
+ for(String word:wordDict){
+ int pos=s.indexOf(word);
+ while(pos!=-1){
+ Integer start=pos;
+ Integer end=pos+word.length();
+ if(!map.containsKey(start))
+ map.put(start, new ArrayList());
+ map.get(start).add(end);
+ pos=s.indexOf(word,start+1);
+ }
+ }
+ recusive(0,map,s,"");
+ return this.result;
+ }
+
+ private void recusive(int key,Map> map,String orginal,String s){
+ if(key>=orginal.length()) {
+ if(!s.trim().equals(""))
+ result.add(s.trim());
+ return;
+ }
+ if(!map.containsKey(key)) return ;
+ List list=map.get(key);
+ for(Integer i:list){
+ //System.out.println(i);
+ recusive(i,map,orginal,orginal.substring(key,i)+" "+s);
+ }
+ }
+ HashMap> map = new HashMap>();
+ public List wordBreak1(String s, Set wordDict) {//15ms
+ List res = new ArrayList();
+ if(s.isEmpty())
+ return res; // return empty list upon empty string
+
+ if(map.containsKey(s))
+ return map.get(s);
+
+ for(int i=0; i rest = wordBreak1(s.substring(i+1),wordDict);
+ if(rest.isEmpty()) {
+ if(i==s.length()-1)
+ res.add(firstWord);
+ } else {
+ for(String str : rest)
+ res.add(firstWord + " " + str);
+ }
+ }
+ }
+
+ map.put(s,res);
+
+ return res;
+ }
+ public static void main(String[] args){
+ N_140_Word_Break_II n=new N_140_Word_Break_II();
+ Set set=new HashSet();
+ set.add("a");
+ set.add("aa");
+ set.add("aaa");
+ set.add("aaaa");
+ set.add("aaaaa");
+ set.add("aaaaaa");
+ set.add("aaaaaaa");
+ set.add("aaaaaaaa");
+ set.add("aaaaaaaaa");
+ System.out.println(n.wordBreak("aaaaaaa" //7
+ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"//36
+ + "aaaaaaaaaabaaaaaaaaaaaaaaaaaa"//28
+ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ + "aaaaaaaaaaaaaaa", set));
+ }
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_14_Longest_Common_Prefix.java b/CuiGuangfan/src/com/xingkong/N_14_Longest_Common_Prefix.java
new file mode 100644
index 0000000..2e08e32
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_14_Longest_Common_Prefix.java
@@ -0,0 +1,34 @@
+package com.xingkong;
+
+import java.util.Arrays;
+
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��7�� ����10:07:26
+* class description
+*/
+public class N_14_Longest_Common_Prefix {
+ public static String longestCommonPrefix(String[] strs) {
+ if(strs==null||strs.length==0) return "";
+ char[] array=strs[0].toCharArray();
+ char[] tempArray;
+ for(int i=1;i> threeSum_0(int[] nums) {//��һ�泬ʱ����Ϊ��ʹ����һ��list
+ Set> set=new HashSet>();
+ List> result=new ArrayList>();
+ Arrays.sort(nums);
+ for(int p1=0;p10&&nums[p1]==nums[p1-1]) continue;
+ int p2=p1+1;
+ int p3=nums.length-1;
+ while(p2 list=new ArrayList();
+ list.add(nums[p1]);
+ list.add(nums[p2]);
+ list.add(nums[p3]);
+ if(!set.contains(list)){
+ set.add(list);
+ result.add(list);
+ }
+ p2++;
+ p3--;
+ }else if(nums[p1]+nums[p2]+nums[p3]>0){
+ p3--;
+ }else{
+ p2++;
+ }
+ }
+ }
+ return result;
+ }
+ public List> threeSum_1(int[] nums) {//ȥ����result��7%��49ms
+ Set> set=new HashSet>();
+ //List> result=new ArrayList>();
+ Arrays.sort(nums);
+ for(int p1=0;p1 list=new ArrayList();
+ list.add(nums[p1]);
+ list.add(nums[p2]);
+ list.add(nums[p3]);
+ if(!set.contains(list)){
+ set.add(list);
+ //result.add(list);
+ }
+ p2++;
+ p3--;
+ }else if(nums[p1]+nums[p2]+nums[p3]>0){
+ p3--;
+ }else{
+ p2++;
+ }
+ }
+ }
+ return new ArrayList>(set);
+ }
+ public static List> threeSum_2(int[] nums) {//9ms
+ //Set> set=new HashSet>();
+ List> result=new ArrayList>();//����1
+ Arrays.sort(nums);
+ for(int p1=0;p10&&nums[p1]==nums[p1-1]) continue;//����2
+ int p2=p1+1;
+ int p3=nums.length-1;
+ while(p2 list=new ArrayList();
+ list.add(nums[p1]);
+ list.add(nums[p2]);
+ list.add(nums[p3]);
+// if(!set.contains(list)){
+// set.add(list);
+// //result.add(list);
+// }
+ result.add(list);//�����Ż�������ã����ԣ�����Ҫset��
+ while (p2 < p3 && nums[p3] == nums[p3 - 1]){//�Ż���3�������ظ���
+ p3--;
+ }
+ p3--;
+ while (p2 < p3 && nums[p2] == nums[p2+1]){//����4
+ p2++;
+ }
+ p2++;
+ }else if(nums[p1]+nums[p2]+nums[p3]>0){
+ while (p2 < p3 && nums[p3] == nums[p3 - 1]){//����5
+ p3--;
+ }
+ p3--;
+ }else{
+ while (p2 < p3 && nums[p2] == nums[p2+1]){//����6
+ p2++;
+ }
+ p2++;
+ }
+ }
+ }
+ return result;
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ System.out.println(N_15_3Sum.threeSum_2(new int[]{-2,0,1,1,2}));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_166_Fraction_to_Recurring_Decimal.java b/CuiGuangfan/src/com/xingkong/N_166_Fraction_to_Recurring_Decimal.java
new file mode 100644
index 0000000..9664e38
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_166_Fraction_to_Recurring_Decimal.java
@@ -0,0 +1,65 @@
+package com.xingkong;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��3�� ����2:24:34
+* class description
+* û�н��
+*/
+public class N_166_Fraction_to_Recurring_Decimal {
+ public static String fractionToDecimal(int numerator, int denominator) {
+ if (numerator == 0) return "0";
+ if (denominator == 0) return "";
+
+ String ans = "";
+
+ //����������
+ if ((numerator < 0) ^ (denominator < 0)) {
+ ans += "-";
+ }
+
+ //����Ҫ����������תΪ������Ϊ���������intתΪlong
+ long num = numerator, den = denominator;
+ num = Math.abs(num);
+ den = Math.abs(den);
+
+ //�������������
+ long res = num / den;
+ ans += String.valueOf(res);
+
+ //����ܹ����������ؽ��
+ long rem = (num % den) * 10;
+ if (rem == 0) return ans;
+
+ //�����������
+ HashMap map = new HashMap();
+ ans += ".";
+ while (rem != 0) {
+ //���ǰ���Ѿ����ֹ�����������ô���Ὺʼѭ��
+ if (map.containsKey(rem)) {
+ int beg = map.get(rem); //ѭ���忪ʼ��λ��
+ String part1 = ans.substring(0, beg);
+ String part2 = ans.substring(beg, ans.length());
+ ans = part1 + "(" + part2 + ")";
+ return ans;
+ }
+
+ //�������³�
+ map.put(rem, ans.length());
+ res = rem / den;
+ ans += String.valueOf(res);
+ rem = (rem % den) * 10;
+ }
+
+ return ans;
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ System.out.println(N_166_Fraction_to_Recurring_Decimal.fractionToDecimal(1, 99));
+ //System.out.println(1*1.0/90);
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_16_3Sum_Closest.java b/CuiGuangfan/src/com/xingkong/N_16_3Sum_Closest.java
new file mode 100644
index 0000000..e3d864a
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_16_3Sum_Closest.java
@@ -0,0 +1,48 @@
+package com.xingkong;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��15�� ����10:21:00
+* class description
+*/
+public class N_16_3Sum_Closest {
+ public int threeSumClosest(int[] nums, int target) {
+ Arrays.sort(nums);
+ int min=Integer.MAX_VALUE;
+ int finalMin=0;
+ for(int p1=0;p10&&nums[p1]==nums[p1-1]) continue;//����2
+ int p2=p1+1;
+ int p3=nums.length-1;
+ while(p2> resultMap=new HashMap>();
+ Map> dictMap=new HashMap>();
+ private void init(){
+ dictMap.put("2", Arrays.asList(new String[]{"a","b","c"}));
+ dictMap.put("3", Arrays.asList(new String[]{"d","e","f"}));
+ dictMap.put("4", Arrays.asList(new String[]{"g","h","i"}));
+ dictMap.put("5", Arrays.asList(new String[]{"j","k","l"}));
+ dictMap.put("6", Arrays.asList(new String[]{"m","n","o"}));
+ dictMap.put("7", Arrays.asList(new String[]{"p","q","r","s"}));
+ dictMap.put("8", Arrays.asList(new String[]{"t","u","v"}));
+ dictMap.put("9", Arrays.asList(new String[]{"w","x","y","z"}));
+ }
+ public List letterCombinations(String digits) {//����������鷳��
+ if(dictMap.isEmpty()) init();
+ List list=new ArrayList();
+ if(digits.trim().equals(""))
+ return list;
+ if(resultMap.containsKey(digits))
+ return resultMap.get(digits);
+ //for(int i=0;i tempList=letterCombinations(digits.substring(1));
+ List mapStr=dictMap.get(tempStr);
+ for(String tempMapStr:mapStr){
+ if(tempList.isEmpty()){
+ //if(i==digits.length()-1)
+ list.add(tempMapStr);
+ }else{
+ for(String temp:tempList){
+ list.add(tempMapStr+temp);
+ }
+ }
+ }
+ //}
+ resultMap.put(digits, list);
+ return list;
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ N_17_Letter_Combinations_of_a_Phone_Number n=new N_17_Letter_Combinations_of_a_Phone_Number();
+ System.out.println(n.letterCombinations("23"));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_19_Remove_Nth_Node_From_End_of_List.java b/CuiGuangfan/src/com/xingkong/N_19_Remove_Nth_Node_From_End_of_List.java
new file mode 100644
index 0000000..51947a1
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_19_Remove_Nth_Node_From_End_of_List.java
@@ -0,0 +1,34 @@
+package com.xingkong;
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��7�� ����10:28:04
+* class description
+*/
+public class N_19_Remove_Nth_Node_From_End_of_List {
+ private class ListNode{
+ int val;
+ ListNode next;
+ ListNode(int x){val=x;}
+ }
+ public ListNode removeNthFromEnd(ListNode head, int n) {
+
+ ListNode first=head;
+ ListNode second=first;
+ while(n-->0) {
+ second=second.next;
+ if(second==null){
+ return head.next;
+ }
+ }
+ while(second.next!=null){
+ first=first.next;
+ second=second.next;
+ }
+ first.next=first.next!=null?first.next.next:null;
+ return head;
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ //һ�β���ͨ��������д����
+ }
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_204_Count_Primes.java b/CuiGuangfan/src/com/xingkong/N_204_Count_Primes.java
new file mode 100644
index 0000000..d712988
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_204_Count_Primes.java
@@ -0,0 +1,32 @@
+package com.xingkong;
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��2��29�� ����8:49:52
+* class description
+*/
+public class N_204_Count_Primes {
+ public static int countPrimes(int n) {
+ boolean[]isPrime=new boolean[n];
+ for(int i=2;i v=new Stack();
+ for(int i=0;i> adjLists = new ArrayList>();
+ for (int i = 0; i < numCourses; i++) {
+ adjLists.add(new HashSet());
+ }
+
+ for (int i = 0; i < prerequisites.length; i++) {
+ adjLists.get(prerequisites[i][1]).add(prerequisites[i][0]);
+ }
+
+ int[] indegrees = new int[numCourses];
+ for (int i = 0; i < numCourses; i++) {
+ for (int x : adjLists.get(i)) {
+ indegrees[x]++;
+ }
+ }
+
+ Queue queue = new LinkedList();
+ for (int i = 0; i < numCourses; i++) {
+ if (indegrees[i] == 0) {
+ queue.offer(i);
+ }
+ }
+
+ int[] res = new int[numCourses];
+ int count = 0;
+ while (!queue.isEmpty()) {
+ int cur = queue.poll();
+ for (int x : adjLists.get(cur)) {
+ indegrees[x]--;
+ if (indegrees[x] == 0) {
+ queue.offer(x);
+ }
+ }
+ res[count++] = cur;
+ }
+
+ if (count == numCourses) return res;
+ return new int[0];
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ N_210_Course_Schedule_II n=new N_210_Course_Schedule_II();
+ int[] temp=n.findOrder(2, new int[][]{{0,1},{1,0}});//{1,0},{2,0},{3,1},{3,2}
+ for(int i=0;i stack,int count){
+ //�ȼ�����������
+ if(y+count>matrix[0].length-1||x+count>matrix.length-1||matrix[x][y+count]!='1'||matrix[x+count][y]!='1'){
+ return count;
+ }
+ Stack thisStack=new Stack();
+ //���ջ�е�
+ while(!stack.isEmpty()){
+ Entity e=stack.pop();
+ if(matrix[e.x+1][e.y+1]!='1'){
+ return count;
+ }
+ thisStack.push(new Entity(e.x+1,e.y+1));
+ }
+ thisStack.push(new Entity(x,y+count));
+ thisStack.push(new Entity(x+count,y));
+ return recursion(matrix,x,y,thisStack,count+1);
+ }
+ public static int maximalSquare(char[][] matrix) {//53ms 1.22%
+ int max=0;
+ for(int i=0;i stack=new Stack();
+ stack.push(new Entity(i,j));
+ int tempMax=recursion(matrix,i,j,stack,1);
+ if(tempMax>max){
+ max=tempMax;
+ }
+ }
+ }
+ return max*max;
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ System.out.println(N_221_Maximal_Square.maximalSquare(new char[][]{
+ {'1','0','1','0','0'},
+ {'1','0','1','1','1'},
+ {'1','1','1','1','1'},
+ {'1','0','0','1','1'}
+ }));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_224_Basic_Calculator.java b/CuiGuangfan/src/com/xingkong/N_224_Basic_Calculator.java
new file mode 100644
index 0000000..31e0461
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_224_Basic_Calculator.java
@@ -0,0 +1,69 @@
+package com.xingkong;
+
+import java.util.Stack;
+
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��2��29�� ����10:14:26
+* class description
+*/
+public class N_224_Basic_Calculator {
+ public static int calculate(String s) {
+ char[] array=s.trim().replaceAll(" ", "").toCharArray();
+ Stack numberStack=new Stack();
+ Stack operatorStack=new Stack();
+ for(int i=0;i reverseNumberStack=new Stack();
+ while(!numberStack.isEmpty()){
+ reverseNumberStack.push(numberStack.pop());
+ }
+ Stack resultStack=new Stack();
+ while(!reverseNumberStack.isEmpty()){
+ String temp=reverseNumberStack.pop();
+ if(temp.equals("+")||temp.equals("-")){
+ Integer number1=Integer.valueOf(resultStack.pop());
+ Integer number2=Integer.valueOf(resultStack.pop());
+ if(temp.equals("+")){
+ resultStack.push(String.valueOf(number1+number2));
+ }else{
+ resultStack.push(String.valueOf(number2-number1));
+ }
+ }else{
+ resultStack.push(temp);
+ }
+ }
+ return Integer.valueOf(resultStack.pop());
+ }
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ System.out.println(N_224_Basic_Calculator.calculate(" 30"));
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_26_Remove_Duplicates_from_Sorted_Array.java b/CuiGuangfan/src/com/xingkong/N_26_Remove_Duplicates_from_Sorted_Array.java
new file mode 100644
index 0000000..a7ab73b
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_26_Remove_Duplicates_from_Sorted_Array.java
@@ -0,0 +1,25 @@
+package com.xingkong;
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��9�� ����8:56:42
+* class description
+*/
+public class N_26_Remove_Duplicates_from_Sorted_Array {
+ public static int removeDuplicates(int[] nums) {
+ int count=0;
+ int i=0;
+ while(i> mapRow=new HashMap>();
+ Map> mapColumn=new HashMap>();
+ Map> mapRect=new HashMap>();
+ for(int i=0;i());
+ if(!mapRow.get(i).contains(board[i][j])) mapRow.get(i).add(board[i][j]);
+ else
+ return false;
+ if(!mapColumn.containsKey(j)) mapColumn.put(j, new HashSet());
+ if(!mapColumn.get(j).contains(board[i][j])) mapColumn.get(j).add(board[i][j]);
+ else
+ return false;
+ if(!mapRect.containsKey(3*(i/3)+j/3)) mapRect.put(3*(i/3)+j/3, new HashSet());
+ if(!mapRect.get(3*(i/3)+j/3).contains(board[i][j])) mapRect.get(3*(i/3)+j/3).add(board[i][j]);
+ else
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ public static void main(String[] args) {
+ N_36_Valid_Sudoku n=new N_36_Valid_Sudoku();
+ System.out.println(n.isValidSudoku(new char[][]{
+ {'.','8','7','6','5','4','3','2','1'},{'2','.','.','.','.','.','.','.','.'},{'3','.','.','.','.','.','.','.','.'},
+ {'4','.','.','.','.','.','.','.','.'},{'5','.','.','.','.','.','.','.','.'},{'6','.','.','.','.','.','.','.','.'},
+ {'7','.','.','.','.','.','.','.','.'},{'8','.','.','.','.','.','.','.','.'},{'9','.','.','.','.','.','.','.','.'}
+ }));
+
+ }
+
+}
diff --git a/CuiGuangfan/src/com/xingkong/N_43_Multiply_Strings.java b/CuiGuangfan/src/com/xingkong/N_43_Multiply_Strings.java
new file mode 100644
index 0000000..f912ba4
--- /dev/null
+++ b/CuiGuangfan/src/com/xingkong/N_43_Multiply_Strings.java
@@ -0,0 +1,48 @@
+package com.xingkong;
+/**
+* @author cuiguangfan 736068048@qq.com:
+* @version create time��2016��3��3�� ����8:42:55
+* class description
+*/
+public class N_43_Multiply_Strings {
+
+ public static String multiply(String num1, String num2) {
+ char[] array1=num1.toCharArray();
+ char[] array2=num2.toCharArray();
+ char[] num1Array=array1;
+ char[] num2Array=array2;
+
+ if(array1.length=0;i--){
+ int addition=0;
+ int j=num1Array.length-1;
+ for(;j>=0;j--){
+ int temp=result[i+j+1]-48+(num2Array[i]-48)*(num1Array[j]-48)+addition;
+ result[i+j+1]=(char) (temp%10+48);
+ addition=temp/10;
+ }
+ if(addition>0&&jnums2[n2Mid]){
+ return recursion(nums1,nums2,n1Begin,n1Mid,n2Mid,n2End,n1Sum+n1End-n1Mid,n2Sum+n2Mid-n2Begin);
+ }else if(nums1[n1Mid]0){
+ char firstChar=str.charAt(0);
+ if(firstChar<'0'){
+ if(firstChar=='-'){
+ negative=true;
+ limit=Integer.MIN_VALUE;
+ }else if(firstChar!='+'){
+ return 0;
+ }
+ if(str.length()==1){
+ return 0;
+ }
+ i++;
+ }
+ int multmin=limit/radix;
+ int digit=0;
+ while(i=multmin���������result*=radix����overflow
+ return negative?Integer.MIN_VALUE:Integer.MAX_VALUE;
+ }
+ result*=radix;
+ if(resultInteger.MAX_VALUE||result