From 302225a4b829ec97d3f2cc3368c8ab13900313ba Mon Sep 17 00:00:00 2001
From: zhugaoping <zhugaoping@gmail.com>
Date: Thu, 11 Apr 2013 16:22:18 +1000
Subject: [PATCH] 04112013

---
 3Sum Closeest.txt                             | 37 +++----
 3Sum.txt                                      | 37 +++----
 4Sum.txt                                      | 45 ++++-----
 Add Binary.txt                                | 39 +++-----
 Add Two Numbers.txt                           | 47 +++------
 Anagrams.txt                                  | 31 +++---
 Balanced Binary Tree.txt                      | 15 ++-
 Best Time to Buy and Sell Stock II.txt        | 23 ++---
 Best Time to Buy and Sell Stock III.txt       | 24 ++---
 Best Time to Buy and Sell Stock.txt           | 14 +--
 Binary Tree Inorder Traversal.txt             | 17 ++--
 Binary Tree Level Order Traversal II.txt      | 31 +++---
 Binary Tree Level Order Traversal.txt         | 75 +++++++++++---
 Binary Tree Zigzag Level Order Traversal.txt  | 49 ++++++----
 Combination Sum.txt                           | 33 +++----
 Combination Sum2.txt                          | 34 +++----
 Combinations.txt                              | 16 ++-
 ...e from Inorder and Postorder Traversal.txt | 16 ++-
 ...ee from Preorder and Inorder Traversal.txt | 12 +--
 Container With Most Water.txt                 | 37 ++-----
 ...ert Sorted Array to Binary Search Tree.txt | 12 +--
 Count and Say.txt                             | 22 ++---
 Decode Ways.txt                               | 29 ++----
 Distinct Subsequences.txt                     | 21 ++--
 Divide Two Integers.txt                       | 16 ++-
 Edit Distance.txt                             | 27 +++---
 First Missing Positive.txt                    | 33 +++----
 Generate Parentheses.txt                      | 57 +++++------
 Grey Code.txt                                 | 18 ++--
 Implement strStr().txt                        | 37 +++----
 Insert Interval.txt                           | 34 +++----
 Integer to Roman.txt                          | 60 ++++++------
 Interleaving String.txt                       | 52 +++-------
 Jump Game 2.txt                               | 17 ++--
 Jump Game.txt                                 | 10 +-
 Largest Rectangle in Histogram.txt            | 35 ++++---
 Length of Last Word.txt                       | 17 ++--
 Longest Common Prefix.txt                     | 38 +++-----
 Longest Consecutive Sequence.txt              | 21 ++++
 Longest Palindromic Substring.txt             | 36 +++----
 ...Substring Without Repeating Characters.txt | 32 +++---
 Longest Valid Parentheses.txt                 | 45 ++++-----
 Maximal Rectangle.txt                         | 75 ++++++--------
 Maximum Subarray.txt                          | 10 +-
 Merge Intervals.txt                           | 42 +++-----
 Merge Sorted Array.txt                        | 27 ++----
 Merge Two Sorted Lists.txt                    | 40 ++++----
 Merge k Sorted Lists.txt                      | 34 +++----
 Minimum Depth of Binary Tree.txt              |  6 +-
 Minimum Path Sum.txt                          | 30 +++---
 Minimum Window Substring.txt                  | 83 ++++++----------
 Multiply Strings.txt                          | 70 ++++++-------
 N-Queens.txt                                  | 61 ++++++------
 Next Permutation.txt                          | 26 +++--
 Palindrome Number.txt                         | 14 +--
 Palindrome Partitioning.txt                   | 38 ++++++++
 Palindrome PartitioningII.txt                 | 28 ++++++
 Partition List.txt                            | 33 +++----
 Pascal's Triangle II.txt                      |  9 +-
 Pascal's Triangle.txt                         | 16 ++-
 Path Sum II.txt                               | 50 +++-------
 Path Sum.txt                                  | 26 ++---
 Permutation Sequence.txt                      | 21 ++--
 Permutations II.txt                           | 39 +++-----
 Permutations.txt                              | 27 +++---
 Plus One.txt                                  | 11 ++-
 ...latig Next Right Pointers in Each Node.txt | 38 ++++----
 ...ng Next Right Pointers in Each Node II.txt | 31 +++---
 Pow(x,n).txt                                  | 18 ++--
 Recover Binary Search Tree.txt                | 50 +++++-----
 Regular Expression Matching.txt               | 22 ++---
 Remove Duplicates form Sorted Array II.txt    | 12 +--
 Remove Duplicates from Sorted Array.txt       | 12 +--
 Remove Duplicates from Sorted List II.txt     | 26 +++--
 Remove Duplicates from Sorted List.txt        | 19 ++--
 Remove Element.txt                            | 13 +--
 Restore IP Addresses.txt                      | 46 +++++----
 Reverse Integer.txt                           | 20 ++--
 Reverse Linked List II.txt                    | 17 ++--
 Reverse Nodes in K-Group.txt                  | 29 +++---
 Roman to Integer.txt                          | 26 ++---
 Rotate List.txt                               | 30 +++---
 Same Tree.txt                                 | 11 +--
 Scramble String.txt                           | 36 +++----
 Search Insert Position.txt                    | 16 ++-
 Search for a Range.txt                        | 61 +++++-------
 Search in Rotated Sorted Array II.txt         | 60 ++++--------
 Set Matrix Zeroes.txt                         | 63 +++++-------
 Spiral Matrix II.txt                          | 20 ++--
 Sqrt.txt                                      | 13 +++
 Srounded Regions.txt                          | 51 ++++++++++
 Subsets II.txt                                | 36 +++----
 Subsets.txt                                   | 20 ++--
 Sudoku Solver.txt                             | 45 +++++++++
 Sum Root to Leaf Numbers.txt                  | 30 ++++++
 Swap Nodes in Pairs.txt                       | 33 +++----
 Symmetric Tree.txt                            | 24 ++---
 Text Justification.txt                        | 75 ++++++--------
 Trapping Rain Water.txt                       | 19 ++--
 Triangle.txt                                  | 25 ++---
 Two Sum.txt                                   | 25 +++--
 Unique Binary Search Tree II.txt              | 36 +++----
 Unique Binary Search Tree.txt                 | 12 +--
 Unique Path II.txt                            | 39 +++-----
 Unique Path.txt                               | 18 ++--
 Valid Number.txt                              | 97 +++++++++----------
 Valid Palindrome.txt                          | 23 +++++
 Valid Parentheses.txt                         | 42 ++++----
 Validate Binary Search Tree.txt               | 13 +--
 Wildcard Matching.txt                         | 41 ++++----
 Word Ladder II.txt                            | 61 ++++++++++++
 Word Ladder.txt                               | 36 +++++++
 Word Search.txt                               | 57 +++++------
 Zigzag Conversion.txt                         | 15 ++-
 114 files changed, 1784 insertions(+), 1895 deletions(-)
 create mode 100644 Longest Consecutive Sequence.txt
 create mode 100644 Palindrome Partitioning.txt
 create mode 100644 Palindrome PartitioningII.txt
 create mode 100644 Sqrt.txt
 create mode 100644 Srounded Regions.txt
 create mode 100644 Sum Root to Leaf Numbers.txt
 create mode 100644 Valid Palindrome.txt
 create mode 100644 Word Ladder II.txt
 create mode 100644 Word Ladder.txt

diff --git a/3Sum Closeest.txt b/3Sum Closeest.txt
index 1c08343..fbda05d 100644
--- a/3Sum Closeest.txt	
+++ b/3Sum Closeest.txt	
@@ -3,35 +3,22 @@ public:
     int threeSumClosest(vector<int> &num, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int ret = -1;
-        int size = num.size();
-        if (size < 3)
-            return ret;
+        long long dist = LLONG_MAX;
         sort(num.begin(), num.end());
-        for (int i = 0; i < size - 2; i++) {
-            while ((i > 0) && (i < (size - 2)) && (num[i] == num[i-1]))
-                i++;
-            int l = i + 1, u = size - 1, sum = 0;
+        for (int i = 0; i < num.size() - 2; i++) {
+            if ((i > 0) && (num[i] == num[i-1]))
+                continue;
+            int l = i + 1, u = num.size() - 1;
             while (l < u) {
-                sum = num[i] + num[l] + num[u];
-                if (ret == -1)
-                    ret = sum;
+                int sum = num[i] + num[l] + num[u];
+                if (llabs((long long)sum - target) < llabs(dist))
+                    dist = sum - target;
+                if (sum < target)
+                    while ((++l < u) && (num[l] == num[l-1]));
                 else
-                    ret = (abs(sum - target) < abs(ret - target))?sum:ret;
-                if (ret == target)
-                    return ret;
-                else if (sum > target) {
-                    u--;
-                    while ((l < u) && (num[u] == num[u+1]))
-                        u--;
-                }
-                else {
-                    l++;
-                    while ((l < u) && (num[l] == num[l-1]))
-                        l++;
-                }
+                    while ((--u > l) && (num[u] == num[u+1]));
             }
         }
-        return ret;
+        return dist + target;
     }
 };
\ No newline at end of file
diff --git a/3Sum.txt b/3Sum.txt
index 5ebfa6e..53427c0 100644
--- a/3Sum.txt
+++ b/3Sum.txt
@@ -4,33 +4,24 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         vector<vector<int> > rets;
-        int size = num.size();
-        if (size < 3)
+        if (num.size() < 3)
             return rets;
         sort(num.begin(), num.end());
-        for (int i = 0; i < size - 2; i++) {
-            while ((i > 0) && (i < (size - 2)) && (num[i] == num[i-1]))
-                i++;
-            int l = i + 1, u = size - 1, sum = 0;
+        for (size_t i = 0; i < num.size() - 2; i++) {
+            if ((i > 0) && (num[i] == num[i-1]))
+                continue;
+            size_t l = i + 1, u = num.size() - 1;
             while (l < u) {
-                sum = num[i] + num[l] + num[u];
-                if (sum > 0) {
-                    u--;
-                    while ((l < u) && (num[u] == num[u+1]))
-                        u--;
-                }
-                else {
-                    if (sum == 0) {
-                        vector<int> ret;
-                        rets.push_back(ret);
-                        rets.back().push_back(num[i]);
-                        rets.back().push_back(num[l]);
-                        rets.back().push_back(num[u]);
-                    }
-                    l++;
-                    while ((l < u) && (num[l] == num[l-1]))
-                        l++;
+                long long sum = (long long)num[i] + num[l] + num[u];
+                if (sum == 0) {
+                    rets.push_back(vector<int> (3, num[i]));
+                    rets.back()[1] = num[l];
+                    rets.back()[2] = num[u];
                 }
+                if (sum < 0)
+                    while ((++l < u) && (num[l] == num[l-1]));
+                else
+                    while ((--u > l) && (num[u] == num[u+1]));
             }
         }
         return rets;
diff --git a/4Sum.txt b/4Sum.txt
index 335a4b4..22f96a2 100644
--- a/4Sum.txt
+++ b/4Sum.txt
@@ -4,37 +4,28 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         vector<vector<int> > rets;
-        int size = num.size();
-        if (size < 4)
+        if (num.size() < 4)
             return rets;
         sort(num.begin(), num.end());
-        for (int i = 0; i < size - 3; i++) {
-            while ((i > 0) && (i < size - 3) && (num[i] == num[i-1]))
-                i++;
-            for (int j = i+1; j < size - 2; j++) {
-                while ((j > i+1) && (j < size - 2) && (num[j] == num[j-1]))
-                    j++;
-                int l = j + 1, u = size - 1, sum = 0;
+        for (size_t i = 0; i < num.size() - 3; i++) {
+            if ((i > 0) && (num[i] == num[i-1]))
+                continue;
+            for (size_t j = i + 1; j < num.size() - 2; j++) {
+                if ((j > i + 1) && (num[j] == num[j-1]))
+                    continue;
+                size_t l = j + 1, u = num.size() - 1;
                 while (l < u) {
-                    sum = num[i] + num[j] + num[l] + num[u];
-                    if (sum > target) {
-                        u--;
-                        while ((l < u) && (num[u] == num[u+1]))
-                            u--;
-                    }
-                    else {
-                        if (sum == target) {
-                            vector<int> ret;
-                            rets.push_back(ret);
-                            rets.back().push_back(num[i]);
-                            rets.back().push_back(num[j]);
-                            rets.back().push_back(num[l]);
-                            rets.back().push_back(num[u]);
-                        }
-                        l++;
-                        while ((l < u) && (num[l] == num[l-1]))
-                            l++;
+                    long long sum = (long long)num[i] + num[j] + num[l] + num[u];
+                    if (sum == target) {
+                        rets.push_back(vector<int> (4, num[i]));
+                        rets.back()[1] = num[j];
+                        rets.back()[2] = num[l];
+                        rets.back()[3] = num[u];
                     }
+                    if (sum < target)
+                        while ((++l < u) && (num[l] == num[l-1]));
+                    else
+                        while ((--u > l) && (num[u] == num[u+1]));
                 }
             }
         }
diff --git a/Add Binary.txt b/Add Binary.txt
index 0159289..ebc7541 100644
--- a/Add Binary.txt	
+++ b/Add Binary.txt	
@@ -3,33 +3,26 @@ public:
     string addBinary(string a, string b) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (a == "")
+        if (a.empty())
             return b;
-        if (b == "")
+        if (b.empty())
             return a;
-        int ap = a.size() - 1, bp = b.size() - 1, ca = 0, sum = 0;
-        string ret;
-        while ((ap >= 0) && (bp >= 0)) {
-            sum = (a[ap] - '0') + (b[bp] - '0') + ca;
-            ret = (char)('0' + sum%2) + ret;
-            ca = (sum >= 2);
-            ap--;
-            bp--;
-        }
-        while ((bp < 0) && (ap >= 0)) {
-            sum = (a[ap] - '0') + ca;
-            ret = (char)('0' + sum%2) + ret;
-            ca = (sum >= 2);
-            ap--;
-        }
-        while ((ap < 0) && (bp >= 0)) {
-            sum = (b[bp] - '0') + ca;
-            ret = (char)('0' + sum%2) + ret;
-            ca = (sum >= 2);
-            bp--;
+        string ret = "";
+        int ap = a.size() - 1, bp = b.size() - 1, ca = 0;
+        while ((ap >= 0) || (bp >= 0)) {
+            int sum = ((ap >= 0)?a[ap] - '0':0) + ((bp >= 0)?b[bp] - '0':0) + ca;
+            ret = ret + (char)(sum%2 + '0');
+            ca = sum/2;
+            ap = (ap >= 0)?ap-1:ap;
+            bp = (bp >= 0)?bp-1:bp;
         }
         if (ca > 0)
-            ret = '1' + ret;
+            ret = ret + '1';
+        for (int i = 0; i < ret.size()/2; i++) {
+            char c = ret[i];
+            ret[i] = ret[ret.size()-1-i];
+            ret[ret.size()-1-i] = c;
+        }
         return ret;
     }
 };
\ No newline at end of file
diff --git a/Add Two Numbers.txt b/Add Two Numbers.txt
index 3b4097d..6ad9d85 100644
--- a/Add Two Numbers.txt	
+++ b/Add Two Numbers.txt	
@@ -15,41 +15,20 @@ public:
             return l2;
         if (!l2)
             return l1;
-        ListNode* head = new ListNode(0), *tail = head;
-        int sum = 0, car = 0;
-        while (l1 && l2) {
-            sum = l1->val + l2->val + car;
-            ListNode* node = new ListNode(sum%10);
-            car = sum/10;
-            tail->next = node;
+        ListNode *head = new ListNode(0), *tail = head;
+        int c = 0;
+        while (l1 || l2) {
+            int s = (l1?l1->val:0) + (l2?l2->val:0) + c;
+            tail->next = new ListNode(s%10);
             tail = tail->next;
-            l1 = l1->next;
-            l2 = l2->next;
+            c = s/10;
+            l1 = l1?l1->next:l1;
+            l2 = l2?l2->next:l2;
         }
-        while (!l2 && l1) {
-            sum = l1->val + car;
-            ListNode* node = new ListNode(sum%10);
-            car = sum/10;
-            tail->next = node;
-            tail = tail->next;
-            l1 = l1->next;
-        }
-        while (!l1 && l2) {
-            sum = l2->val + car;
-            ListNode* node = new ListNode(sum%10);
-            car = sum/10;
-            tail->next = node;
-            tail = tail->next;
-            l2 = l2->next;
-        }
-        if (car > 0) {
-            ListNode* node = new ListNode(car);
-            tail->next = node;
-            tail = tail->next;
-        }
-        ListNode* temp = head;
-        head = head->next;
-        delete temp;
-        return head;
+        if (c > 0)
+            tail->next = new ListNode(1);
+        tail = head->next;
+        delete head;
+        return tail;
     }
 };
\ No newline at end of file
diff --git a/Anagrams.txt b/Anagrams.txt
index 9b0b7c0..318fd40 100644
--- a/Anagrams.txt
+++ b/Anagrams.txt
@@ -3,28 +3,19 @@ public:
     vector<string> anagrams(vector<string> &strs) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<string> anagrams;
-        map<string, vector<string> > maps;
-        for (int i = 0; i < strs.size(); i++)
-            maps[sorts(strs[i])].push_back(strs[i]);
-        map<string,vector<string> >::iterator it = maps.begin();
+        vector<string> rets;
+        unordered_map<string, vector<string>> maps;
+        for (size_t i = 0; i < strs.size(); i++) {
+            string r = strs[i];
+            sort(r.begin(), r.end());
+            maps[r].push_back(strs[i]);
+        }
+        unordered_map<string, vector<string>>::iterator it = maps.begin();
         while (it != maps.end()) {
-            if (it->second.size() > 1) {
-                for (int i = 0; i < it->second.size(); i++)
-                    anagrams.push_back((it->second)[i]);
-            }
+            if (it->second.size() > 1)
+                rets.insert(rets.end(), it->second.begin(), it->second.end());
             it++;
         }
-        return anagrams;
-    }
-    string sorts(string& s) {
-        vector<char> r;
-        for (int i = 0; i < s.size(); i++)
-            r.push_back(s[i]);
-        sort(r.begin(), r.end());
-        string ret;
-        for (int i = 0; i < r.size(); i++)
-            ret+= r[i];
-        return ret;
+        return rets;
     }
 };
\ No newline at end of file
diff --git a/Balanced Binary Tree.txt b/Balanced Binary Tree.txt
index fc5d58b..89416fb 100644
--- a/Balanced Binary Tree.txt	
+++ b/Balanced Binary Tree.txt	
@@ -12,19 +12,16 @@ public:
     bool isBalanced(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (!root)
-            return true;
         bool ret = true;
         height(root, ret);
         return ret;
     }
-    int height(TreeNode* root, bool& ret) {
-        if (root == NULL)
+    int height(TreeNode* root, bool &ret) {
+        if (!root)
             return 0;
-        int lh = height(root->left, ret);
-        int rh = height(root->right, ret);
-        if (abs(lh - rh) > 1)
-            ret = false;
-        return max(lh, rh) + 1;
+        int l = height(root->left, ret);
+        int r = height(root->right, ret);
+        ret = (abs(l - r) > 1)?false:ret;
+        return max(l, r)+1;
     }
 };
\ No newline at end of file
diff --git a/Best Time to Buy and Sell Stock II.txt b/Best Time to Buy and Sell Stock II.txt
index 515b32f..6f02602 100644
--- a/Best Time to Buy and Sell Stock II.txt	
+++ b/Best Time to Buy and Sell Stock II.txt	
@@ -3,22 +3,13 @@ public:
     int maxProfit(vector<int> &prices) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size = prices.size();
-        if (size < 2)
-            return 0;
-        int prof = 0;
-        vector<int> b(1, prices[0]);
-        for (int i = 1; i < size; i++) {
-            if (prices[i] > b.back())
-                b.push_back(prices[i]);
-            else {
-                prof += b.back() - b[0];
-                while (b.size() > 0)
-                    b.pop_back();
-                b.push_back(prices[i]);
+        int ret = 0, beg = 0;
+        for (int i = 1; i < prices.size(); i++)
+            if (prices[i] < prices[i-1]) {
+                ret += prices[i-1] - prices[beg];
+                beg = i;
             }
-        }
-        prof += b.back() - b[0];
-        return prof;
+        ret += prices.empty()?0:prices.back() - prices[beg];
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Best Time to Buy and Sell Stock III.txt b/Best Time to Buy and Sell Stock III.txt
index ee787d7..c5cf824 100644
--- a/Best Time to Buy and Sell Stock III.txt	
+++ b/Best Time to Buy and Sell Stock III.txt	
@@ -5,25 +5,21 @@ public:
         // DO NOT write int main() function
         if (prices.size() < 2)
             return 0;
-        int size = prices.size();
-        int l[size];
-        int r[size];
-        int minp = prices[0];
+        int size = prices.size(), l[size], r[size];
+        int miv = prices[0], mav = prices.back();
         l[0] = 0;
         for (int i = 1; i < size; i++) {
-            l[i] = max((prices[i] - minp), l[i-1]);
-            minp = min(prices[i], minp);
+            miv = min(miv, prices[i]);
+            l[i] = max(l[i-1], prices[i] - miv);
         }
         r[size-1] = 0;
-        int maxp = prices[size-1];
         for (int i = size - 2; i >= 0; i--) {
-            r[i] = max((maxp - prices[i]), r[i+1]);
-            maxp = max(prices[i], maxp);    
+            mav = max(mav, prices[i]);
+            r[i] = max(r[i+1], mav - prices[i]);
         }
-        int m = l[size - 1];
-        for (int i = 0; i < size - 2; i++) {
-            m = max(l[i]+r[i+1], m);
-        }
-        return m;
+        int ret = r[0];
+        for (int i = 0; i < size - 1; i++)
+            ret = max(ret, l[i] + r[i+1]);
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Best Time to Buy and Sell Stock.txt b/Best Time to Buy and Sell Stock.txt
index 3c9d68a..4aaf2b9 100644
--- a/Best Time to Buy and Sell Stock.txt	
+++ b/Best Time to Buy and Sell Stock.txt	
@@ -3,15 +3,11 @@ public:
     int maxProfit(vector<int> &prices) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size = prices.size();
-        if (size < 2)
-            return 0;
-        int minp = prices[0];
-        int bene = 0;
-        for (int i = 1; i < size; i++) {
-            bene = max((prices[i] - minp), bene);
-            minp = (prices[i] < minp)?prices[i]:minp;
+        int ret = 0, miv = INT_MAX;
+        for (int i = 0; i < prices.size(); i++) {
+            miv = min(prices[i], miv);
+            ret = max(ret, prices[i] - miv);
         }
-        return bene;
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Binary Tree Inorder Traversal.txt b/Binary Tree Inorder Traversal.txt
index 4943aaa..4e553ee 100644
--- a/Binary Tree Inorder Traversal.txt	
+++ b/Binary Tree Inorder Traversal.txt	
@@ -12,16 +12,15 @@ public:
     vector<int> inorderTraversal(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<int> rets;
-        if (root != NULL)
-            traverse(root, rets);
-        return rets;   
+        vector<int> ret;
+        solve(root, ret);
+        return ret;
     }
-    void traverse(TreeNode* root, vector<int>& rets) {
-        if (root == NULL)
+    void solve(TreeNode* root, vector<int> &ret) {
+        if (!root)
             return;
-        traverse(root->left, rets);
-        rets.push_back(root->val);
-        traverse(root->right, rets);
+        solve(root->left, ret);
+        ret.push_back(root->val);
+        solve(root->right, ret);
     }
 };
\ No newline at end of file
diff --git a/Binary Tree Level Order Traversal II.txt b/Binary Tree Level Order Traversal II.txt
index 6bc11ab..56af8d0 100644
--- a/Binary Tree Level Order Traversal II.txt	
+++ b/Binary Tree Level Order Traversal II.txt	
@@ -12,26 +12,27 @@ public:
     vector<vector<int> > levelOrderBottom(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
+        vector<vector<int>> rets;
         if (root == NULL)
             return rets;
-        vector<TreeNode*> inner, outer;
-        inner.push_back(root);
-        while (!inner.empty()) {
-            outer.clear();
-            vector<int> ret;
-            rets.insert(rets.begin(), ret);
-            while (inner.size() > 0) {
-                TreeNode* cur = *inner.begin();
-                inner.erase(inner.begin());
-                rets.front().push_back(cur->val);
-                if (cur->left)
-                    outer.push_back(cur->left);
-                if (cur->right)
-                    outer.push_back(cur->right);
+        list<TreeNode*>* inner = new list<TreeNode*>, *outer;
+        inner->push_back(root);
+        while(!inner->empty()) {
+            outer = new list<TreeNode*>;
+            rets.insert(rets.begin(), vector<int>(0));
+            while(!inner->empty()) {
+                TreeNode* node = inner->front();
+                inner->pop_front();
+                rets.front().push_back(node->val);
+                if (node->left)
+                    outer->push_back(node->left);
+                if (node->right)
+                    outer->push_back(node->right);
             }
+            delete inner;
             inner = outer;
         }
+        delete inner;
         return rets;   
     }
 };
\ No newline at end of file
diff --git a/Binary Tree Level Order Traversal.txt b/Binary Tree Level Order Traversal.txt
index 911c197..3d7d408 100644
--- a/Binary Tree Level Order Traversal.txt	
+++ b/Binary Tree Level Order Traversal.txt	
@@ -12,26 +12,71 @@ public:
     vector<vector<int> > levelOrder(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
+        vector<vector<int>> rets;
         if (root == NULL)
             return rets;
-        vector<TreeNode*> inner, outer;
-        inner.push_back(root);
-        while (!inner.empty()) {
-            outer.clear();
-            vector<int> ret;
-            rets.push_back(ret);
-            while (inner.size() > 0) {
-                TreeNode* cur = *inner.begin();
-                inner.erase(inner.begin());
-                rets.back().push_back(cur->val);
-                if (cur->left)
-                    outer.push_back(cur->left);
-                if (cur->right)
-                    outer.push_back(cur->right);
+        list<TreeNode*>* inner = new list<TreeNode*>;
+        list<TreeNode*>* outer;
+        inner->push_back(root);
+        while(!inner->empty()) {
+            outer = new list<TreeNode*>;
+            rets.push_back(vector<int>(0));
+            while(!inner->empty()) {
+                TreeNode* node = inner->front();
+                inner->pop_front();
+                rets.back().push_back(node->val);
+                if (node->left)
+                    outer->push_back(node->left);
+                if (node->right)
+                    outer->push_back(node->right);
             }
+            delete inner;
             inner = outer;
         }
+        delete inner;
+        return rets;
+    }
+};
+
+/**
+ * Definition for binary tree
+ * struct TreeNode {
+ *     int val;
+ *     TreeNode *left;
+ *     TreeNode *right;
+ *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
+ * };
+ */
+class Solution {
+public:
+    vector<vector<int> > levelOrder(TreeNode *root) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        vector<vector<int>> rets(1, vector<int>(0));
+        if (root) {
+            list<TreeNode*> queue;
+            queue.push_back(root);
+            int p = 0, ob = 0, nb = 0;
+            while (!queue.empty()) {
+                TreeNode* node = queue.front();
+                queue.pop_front();
+                rets.back().push_back(node->val);
+                if (node->left) {
+                    queue.push_back(node->left);
+                    nb++;
+                }
+                if (node->right) {
+                    queue.push_back(node->right);
+                    nb++;
+                }
+                if (p == ob) {
+                    rets.push_back(vector<int>(0));
+                    ob = nb;
+                }
+                p++;
+            }
+        }
+        rets.pop_back();
         return rets;
     }
 };
\ No newline at end of file
diff --git a/Binary Tree Zigzag Level Order Traversal.txt b/Binary Tree Zigzag Level Order Traversal.txt
index dd2757b..62ffd11 100644
--- a/Binary Tree Zigzag Level Order Traversal.txt	
+++ b/Binary Tree Zigzag Level Order Traversal.txt	
@@ -12,31 +12,38 @@ public:
     vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
+        vector<vector<int>> rets;
         if (root == NULL)
             return rets;
-        vector<TreeNode*> inner, outer;
-        bool left = true;
-        inner.push_back(root);
-        while (!inner.empty()) {
-            outer.clear();
-            vector<int> ret;
-            rets.push_back(ret);
-            while (inner.size() > 0) {
-                TreeNode* cur = *inner.begin();
-                inner.erase(inner.begin());
-                if (left)
-                    rets.back().push_back(cur->val);
-                else
-                    rets.back().insert(rets.back().begin(), cur->val);
-                if (cur->left)
-                    outer.push_back(cur->left);
-                if (cur->right)
-                    outer.push_back(cur->right);
+        list<TreeNode*>* inner = new list<TreeNode*>, *outer;
+        bool reverse = false;
+        inner->push_back(root);
+        while(!inner->empty()) {
+            outer = new list<TreeNode*>;
+            rets.push_back(vector<int>(0));
+            while(!inner->empty()) {
+                TreeNode* node = inner->front();
+                inner->pop_front();
+                rets.back().push_back(node->val);
+                if (node->left)
+                    outer->push_back(node->left);
+                if (node->right)
+                    outer->push_back(node->right);
             }
+            if (reverse) {
+                size_t size = rets.back().size();
+                for (int i = 0; i < size/2; i++) {
+                    int t = rets.back()[i];
+                    rets.back()[i] = rets.back()[size-1-i];
+                    rets.back()[size-1-i] = t;
+                }
+            }
+            reverse = !reverse;
+            delete inner;
             inner = outer;
-            left = !left;
         }
-        return rets;
+        delete inner;
+        return rets;     
+        
     }
 };
\ No newline at end of file
diff --git a/Combination Sum.txt b/Combination Sum.txt
index d6c974b..9cef24c 100644
--- a/Combination Sum.txt	
+++ b/Combination Sum.txt	
@@ -1,32 +1,25 @@
 class Solution {
 public:
-    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
+    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         vector<vector<int> > rets;
         vector<int> ret;
-        sort(num.begin(), num.end());
-        plan(rets, ret, num, target, 0);
+        sort(candidates.begin(), candidates.end());
+        solve(rets, ret, candidates, 0, target);
         return rets;
     }
-    void plan(vector<vector<int> >& rs, vector<int>& r, vector<int>& n, int target, int p) {
-        if (target == 0) {
+    void solve(vector<vector<int> > &rs, vector<int> &r, vector<int> &c, int p, int t) {
+        if (t == 0)
             rs.push_back(r);
-            return;
+        else if (p < c.size()){
+            int round = t/c[p];
+            for (int i = 0; i <= count; i++) {
+                solve(rs, r, c, p + 1, t - i*c[p]);
+                r.push_back(c[p]);
+            }
+            for (int i = 0; i <= count; i++)
+                r.pop_back();
         }
-        if (p == n.size())
-            return;
-        int e = p + 1;
-        while ((e < n.size()) && (n[p] == n[e]))
-            e++;
-        plan(rs, r, n, target, e);
-        int i = 1;
-        for (; (i <= e - p) && (i*n[p] <= target); i++) {
-            r.push_back(n[p]);
-            plan(rs, r, n, target - i*n[p], e);
-        }
-        i--;
-        while (i--)
-            r.pop_back();
     }
 };
\ No newline at end of file
diff --git a/Combination Sum2.txt b/Combination Sum2.txt
index b141947..e4edeba 100644
--- a/Combination Sum2.txt	
+++ b/Combination Sum2.txt	
@@ -1,29 +1,27 @@
 class Solution {
 public:
-    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
+    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
+        vector<vector<int>> rets;
         vector<int> ret;
-        sort(candidates.begin(), candidates.end());
-        plan(rets, ret, candidates, target, 0);
+        sort(num.begin(), num.end());
+        solve(rets, ret, num, 0, target);
         return rets;
     }
-    void plan(vector<vector<int> >& rs, vector<int> r, vector<int> & c, int target, int p) {
-        if (target == 0) {
+    void solve(vector<vector<int>> &rs, vector<int> &r, vector<int> &n, int p, int t) {
+        if (t == 0)
             rs.push_back(r);
-            return;
+        else if (p < n.size()) {
+            int e = p, d = n[p];
+            while ((++e < n.size()) && (n[e] == d));
+            int round = min(e - p, t/d);
+            for (int i = 0; i <= round; i++) {
+                solve(rs, r, n, e, t - i*d);
+                r.push_back(d);
+            }
+            for (int i = 0; i <= round; i++)
+                r.pop_back();
         }
-        if (p == c.size())
-            return;
-        plan(rs, r, c, target, p+1);
-        int i = 1;
-        for (; c[p]*i <= target; i++) {
-            r.push_back(c[p]);
-            plan(rs, r, c, target - c[p]*i, p+1);
-        }
-        i--;
-        while(i--)
-            r.pop_back();
     }
 };
\ No newline at end of file
diff --git a/Combinations.txt b/Combinations.txt
index 80232ba..ddd9a4e 100644
--- a/Combinations.txt
+++ b/Combinations.txt
@@ -5,19 +5,17 @@ public:
         // DO NOT write int main() function
         vector<vector<int> > rets;
         vector<int> ret;
-        plan(rets, ret, 1, n, k);
+        if (k <= n)
+            solve(rets, ret, n, k, 1);
         return rets;
     }
-    void plan(vector<vector<int> >& rs, vector<int> r, int p, int n, int k) {
-        if (k == 0) {
+    void solve(vector<vector<int>> &rs, vector<int> &r, int n, int k, int p) {
+        if (k == 0)
             rs.push_back(r);
-            return;
-        }
-        if (p + 1 + k <= n + 1)
-            plan(rs, r, p+1, n, k);
-        if (p + k <= n + 1) {
+        else if (p <= n) {
+            solve(rs, r, n, k, p + 1);
             r.push_back(p);
-            plan(rs, r, p+1, n, k-1);
+            solve(rs, r, n, k - 1, p + 1);
             r.pop_back();
         }
     }
diff --git a/Construct Binary Tree from Inorder and Postorder Traversal.txt b/Construct Binary Tree from Inorder and Postorder Traversal.txt
index 411d566..da48184 100644
--- a/Construct Binary Tree from Inorder and Postorder Traversal.txt	
+++ b/Construct Binary Tree from Inorder and Postorder Traversal.txt	
@@ -11,19 +11,17 @@ class Solution {
 public:
     TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
         // Start typing your C/C++ solution below
-        // DO NOT write int main() function 
-        int p = postorder.size() - 1;
-        return build(postorder, p, inorder, 0, inorder.size()-1);
+        // DO NOT write int main() function
+        int p = postorder.size()-1;
+        return build(postorder, p, inorder, 0, inorder.size() - 1);
     }
     TreeNode* build(vector<int>& po, int& p, vector<int>& io, int l, int u) {
         if (l > u)
             return NULL;
-        TreeNode* root = new TreeNode(po[p]);
-        int i;
-        for (i = l; i <= u; i++)
-            if (io[i] == po[p])
-                break;
-        p--;
+        int v = po[p--], i = l;
+        TreeNode* root = new TreeNode(v);
+        while (io[i] != v)
+            i++;
         root->right = build(po, p, io, i+1, u);
         root->left = build(po, p, io, l, i-1);
         return root;
diff --git a/Construct Binary Tree from Preorder and Inorder Traversal.txt b/Construct Binary Tree from Preorder and Inorder Traversal.txt
index 9aa1538..85a4e83 100644
--- a/Construct Binary Tree from Preorder and Inorder Traversal.txt	
+++ b/Construct Binary Tree from Preorder and Inorder Traversal.txt	
@@ -13,17 +13,15 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         int p = 0;
-        return build(preorder, p, inorder, 0, inorder.size()-1);
+        return build(preorder, p, inorder, 0, inorder.size() - 1);
     }
     TreeNode* build(vector<int>& po, int& p, vector<int>& io, int l, int u) {
         if (l > u)
             return NULL;
-        TreeNode* root = new TreeNode(po[p]);
-        int i;
-        for (i = l; i <= u; i++)
-            if (io[i] == po[p])
-                break;
-        p++;
+        int v = po[p++], i = l;
+        TreeNode* root = new TreeNode(v);
+        while (io[i] != v)
+            i++;
         root->left = build(po, p, io, l, i-1);
         root->right = build(po, p, io, i+1, u);
         return root;
diff --git a/Container With Most Water.txt b/Container With Most Water.txt
index 471cdd4..d53bc6d 100644
--- a/Container With Most Water.txt	
+++ b/Container With Most Water.txt	
@@ -3,34 +3,17 @@ public:
     int maxArea(vector<int> &height) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (height.size() < 2)
-            return 0;
-        int l = 0, r = height.size() - 1;
-        int result = getMin(height[l], height[r])*(r - l);
-        while (l < r) {
-            if (height[l] < height[r]) {
-                int current_h =height[l];
-                for (l++; l < r ; l++) {
-                    if (height[l] > current_h)
-                        break;
-                }
+        int ret = 0, size = height.size(), l = 0, u = size - 1, h = 0;
+        if (size > 1) {
+            while (l < u) {
+                h = min(height[l], height[u]);
+                ret = max(ret, h*(u-l));
+                if (height[l] <= height[u])
+                    while ((++l < u) && (height[l] <= h));
+                else
+                    while ((l < --u) && (height[u] <= h));
             }
-            else {
-                int current_h = height[r];
-                for (r--; r > l ; r--) {
-                    if (height[r] > current_h)
-                        break;
-                }
-            }
-            if (getMin(height[l], height[r])*(r-l) > result)
-                result = getMin(height[l], height[r])*(r-l);
         }
-        return result;
-    }
-    int getMax(int i, int j) {
-        return (i>j)?i:j; 
-    }
-    int getMin(int i, int j) {
-        return (i<j)?i:j;
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Convert Sorted Array to Binary Search Tree.txt b/Convert Sorted Array to Binary Search Tree.txt
index 1a2af27..38560ad 100644
--- a/Convert Sorted Array to Binary Search Tree.txt	
+++ b/Convert Sorted Array to Binary Search Tree.txt	
@@ -12,15 +12,15 @@ public:
     TreeNode *sortedArrayToBST(vector<int> &num) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        return build(num, 0, num.size() -1);
+        return build(num, 0, num.size() - 1);
     }
-    TreeNode* build(vector<int>& num, int l, int u) {
+    TreeNode* build(vector<int>& n, int l, int u) {
         if (l > u)
             return NULL;
-        int mid = (l+u)/2;
-        TreeNode* root = new TreeNode(num[mid]);
-        root->left = build(num, l, mid-1);
-        root->right = build(num, mid+1, u);
+        int m = (l+u)/2;
+        TreeNode* root = new TreeNode(n[m]);
+        root->left = build(n, l, m-1);
+        root->right = build(n, m+1, u);
         return root;
     }
 };
\ No newline at end of file
diff --git a/Count and Say.txt b/Count and Say.txt
index 00ba5f0..4553f11 100644
--- a/Count and Say.txt	
+++ b/Count and Say.txt	
@@ -5,20 +5,18 @@ public:
         // DO NOT write int main() function
         string ret = "1";
         for (int i = 2; i <= n; i++)
-            ret = count(ret);
+            ret = transform(ret);
         return ret;
     }
-    string count(string& prev) {
-        string succ = "";
-        int p = 0;
-        while (p < prev.size()) {
-            int t = p;
-            while ((t < prev.size()) && (prev[t] == prev[p]))
-                t++;
-            succ += (char)('0' + (t - p));
-            succ += prev[p];
-            p = t;
+    string transform(string s) {
+        int p = 0, np = 0;
+        string r;
+        while (p < s.size()) {
+            np = p;
+            while ((++p < s.size()) && (s[p] == s[np]));
+            r += (char)('0' + p - np);
+            r += s[np];
         }
-        return succ;
+        return r;
     }
 };
\ No newline at end of file
diff --git a/Decode Ways.txt b/Decode Ways.txt
index 6a1835c..676ce42 100644
--- a/Decode Ways.txt	
+++ b/Decode Ways.txt	
@@ -3,28 +3,19 @@ public:
     int numDecodings(string s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size = s.size();
-        if (size == 0)
+        if (s.empty())
             return 0;
-        int plan[size+1];
-        memset(plan, 0, sizeof(int)*(size+1));
+        vector<int> plan(s.size()+1, 0);
         plan[0] = 1;
-        plan[1] = parseOne(s, 0)?1:0;
-        for (int i = 2; i <= size; i++) {
-            plan[i] += parseOne(s, i-1)?plan[i-1]:0;
-            plan[i] += parseTwo(s, i-2)?plan[i-2]:0;
-        }
-        return plan[size];
+        plan[1] = one(s, 0);
+        for (int i = 2; (i <= s.size()) && (!plan[i] || !plan[i-1]); i++)
+            plan[i] = (one(s, i-1)?plan[i-1]:0) + (two(s, i-2)?plan[i-2]:0);
+        return plan[s.size()];
     }
-    bool parseOne(string& s, int d) {
-        return ((s[d] >= '1') && (s[d] <= '9'));
+    bool one(string& s, int p) {
+        return (s[p] != '0');
     }
-    bool parseTwo(string& s, int d) {
-        if (s[d] == '1')
-            return true;
-        else if (s[d] == '2')
-            return ((s[d+1] >= '0') && (s[d+1] <= '6'));
-        else
-            return false;
+    bool two(string& s, int p) {
+        return (s[p] == '1') || ((s[p] == '2') && (s[p+1] <= '6'));
     }
 };
\ No newline at end of file
diff --git a/Distinct Subsequences.txt b/Distinct Subsequences.txt
index 69efce0..2073d5e 100644
--- a/Distinct Subsequences.txt	
+++ b/Distinct Subsequences.txt	
@@ -3,17 +3,16 @@ public:
     int numDistinct(string S, string T) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int sizes = S.size(), sizet = T.size();
-        if (sizes == 0)
+        if (T.empty())
+            return 1;
+        if (S.size() < T.size())
             return 0;
-        int plan[sizet][sizes];
-        for (int i = 0; i <= sizes; i++)
-            plan[0][i] = (i==0)?(T[0] == S[0]):(plan[0][i-1] + (T[0] == S[i]));
-        for (int i = 1; i < sizet; i++) {
-            plan[i][i-1] = 0;
-            for (int j = i; j < sizes; j++)
-                plan[i][j] = (T[i] == S[j])?(plan[i-1][j-1] + plan[i][j-1]):plan[i][j-1];
-        }
-        return plan[sizet-1][sizes-1];
+        int plan[T.size()][S.size()];
+        for (int j = 0; j < S.size(); j++)
+            plan[0][j] = ((j == 0)?0:plan[0][j-1]) + ((T[0] == S[j])?1:0);
+        for (int i = 1; i < T.size(); i++)
+            for (int j = i; j < S.size(); j++)
+                plan[i][j] = ((j == i)?0:plan[i][j-1]) + ((T[i] == S[j])?plan[i-1][j-1]:0);
+        return plan[T.size() - 1][S.size() - 1];
     }
 };
\ No newline at end of file
diff --git a/Divide Two Integers.txt b/Divide Two Integers.txt
index d399066..6f2969c 100644
--- a/Divide Two Integers.txt	
+++ b/Divide Two Integers.txt	
@@ -3,17 +3,23 @@ public:
     int divide(int dividend, int divisor) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
+        if (divisor == 1)
+            return dividend;
+        if (divisor == -1)
+            return (dividend == INT_MIN)?INT_MAX:-dividend;
+        if (dividend == 0)
+            return 0;
         bool pos = true;
-        double upper = dividend, lower = divisor;
+        double a = dividend, b = divisor;
         if (dividend < 0) {
             pos = !pos;
-            upper = -upper;
+            a = -a;
         }
         if (divisor < 0) {
             pos = !pos;
-            lower = -lower;
+            b = -b;
         }
-        double ret = exp(log(upper) - log(lower));
-        return pos?ret:-ret;
+        int result = exp(log(a) - log(b));
+        return pos?result:-result;
     }
 };
\ No newline at end of file
diff --git a/Edit Distance.txt b/Edit Distance.txt
index 0e96c92..628a5e4 100644
--- a/Edit Distance.txt	
+++ b/Edit Distance.txt	
@@ -3,26 +3,21 @@ public:
     int minDistance(string word1, string word2) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size1 = word1.size();
-        int size2 = word2.size();
-        if (size1 == 0)
-            return size2;
-        if (size2 == 0)
-            return size1;
-        int dist[size1+1][size2+1];
-        for (int i = 0; i <= size2; i++)
-            dist[0][i] = i;
+        if (word1.empty())
+            return word2.size();
+        if (word2.empty())
+            return word1.size();
+        int size1 = word1.size(), size2 = word2.size(), dist[size1+1][size2+1];
+        for (int j = 0; j <= size2; j++)
+            dist[0][j] = j;
         for (int i = 0; i <= size1; i++)
             dist[i][0] = i;
-        for (int i = 1; i <= size1; i++) {
+        for (int i = 1; i <= size1; i++)
             for (int j = 1; j <= size2; j++) {
-                dist[i][j] = min(dist[i-1][j]+1, dist[i][j-1]+1);
-                if (word1[i-1] == word2[j-1])
-                    dist[i][j] = min(dist[i-1][j-1], dist[i][j]);
-                else
-                    dist[i][j] = min(dist[i-1][j-1]+1, dist[i][j]);
+                int dist1 = min(dist[i][j-1]+1, dist[i-1][j]+1);
+                int dist2 = dist[i-1][j-1] + ((word1[i-1] != word2[j-1])?1:0);
+                dist[i][j] = min(dist1, dist2);
             }
-        }
         return dist[size1][size2];
     }
 };
\ No newline at end of file
diff --git a/First Missing Positive.txt b/First Missing Positive.txt
index d04c3a4..60eca80 100644
--- a/First Missing Positive.txt	
+++ b/First Missing Positive.txt	
@@ -1,22 +1,17 @@
 class Solution {
 public:
-    //leetcode --> Find the first missing positive
-
-int firstMissingPositive(int A[], int n)
-{
-    for (int i = 0; i < n; i++)
-	{
-		if (A[i] == i+1) continue;
-
-		while (A[i] >= 1 && A[i] <= n && A[i] != i+1
-			&& A[A[i]-1] != A[i])
-			swap(A[i], A[A[i]-1]);
-	}
-
-	for (int i = 0; i < n; i++)
-		if (A[i] != i+1) return i+1;
-
-	return n+1;
-}
-
+    int firstMissingPositive(int A[], int n) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        for (int i = 0; i < n; i++)
+            while ((A[i] > 0) && (A[i] <= n) && (A[A[i]-1] != A[i])) {
+                int t = A[A[i]-1];
+                A[A[i]-1] = A[i];
+                A[i] = t;
+            }
+        int j = 0;
+        while ((j < n) && (A[j] == j+1))
+            j++;
+        return j+1;
+    }
 };
\ No newline at end of file
diff --git a/Generate Parentheses.txt b/Generate Parentheses.txt
index 41d5bb0..d2ca3fb 100644
--- a/Generate Parentheses.txt	
+++ b/Generate Parentheses.txt	
@@ -1,46 +1,39 @@
-class A {
-            public:
-        int l;
-        int r;
+struct entry{
+        int l, r;
         string s;
-        A():l(0), r(0) {}
-        A(const A& a):l(a.l), r(a.r), s(a.s) {}
+        entry():l(0), r(0){}
+        entry(const entry& e):l(e.l), r(e.r), s(e.s){}
 };
-        
 class Solution {
 public:
     vector<string> generateParenthesis(int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<string> results;
-        vector<A>* previous = new vector<A>;
-        vector<A>* current = new vector<A>;
-        A a;
-        previous->push_back(a);
+        vector<entry> *inner = new vector<entry>, *outer;
+        inner->push_back(entry());
         for (int i = 0; i < 2*n; i++) {
-            current->clear();
-            for (int j = 0; j < previous->size(); j++) {
-                A& a = (*previous)[j];
-                if (a.l < n) {
-                    A c(a);
-                    c.l++;
-                    c.s += "(";
-                    current->push_back(c);
+            outer = new vector<entry>;
+            while(!inner->empty()) {
+                entry e = inner->back();
+                inner->pop_back();
+                if (e.l < n) {
+                    outer->push_back(e);
+                    outer->back().l++;
+                    outer->back().s += '(';
                 }
-                if (a.r < a.l) {
-                    A c(a);
-                    c.r++;
-                    c.s += ")";
-                    current->push_back(c);
+                if (e.l > e.r) {
+                    outer->push_back(e);
+                    outer->back().r++;
+                    outer->back().s += ')';
                 }
             }
-            vector<A>* temp = previous;
-            previous = current;
-            current = temp;
+            delete inner;
+            inner = outer;
         }
-        for (int i = 0; i < previous->size(); i++)
-            results.push_back((*previous)[i].s);
-        return results;
-        
+        vector<string> rets;
+        for (size_t i = 0; i < inner->size(); i++)
+            rets.push_back((*inner)[i].s);
+        delete inner;
+        return rets;
     }
 };
\ No newline at end of file
diff --git a/Grey Code.txt b/Grey Code.txt
index 81d2dd4..62605d5 100644
--- a/Grey Code.txt	
+++ b/Grey Code.txt	
@@ -3,16 +3,16 @@ public:
     vector<int> grayCode(int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<int> codes;
-        codes.push_back(0);
+        vector<int> ret;
+        ret.push_back(0);
         if (n == 0)
-            return codes;
-        codes.push_back(1);
-        for (int i = 2; i <= n; i++) {
-            int size = codes.size();
-            for (int j = size - 1; j >= 0 ; j--)
-                codes.push_back(codes[j] | (1<< (i - 1))); 
+            return ret;
+        ret.push_back(1);
+        for (int i = 1; i < n; i++) {
+            int size = ret.size();
+            for (int j = size - 1; j >= 0; j--)
+                ret.push_back(ret[j] | (1<<i));
         }
-        return codes;
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Implement strStr().txt b/Implement strStr().txt
index 1d7791b..15bdc6b 100644
--- a/Implement strStr().txt	
+++ b/Implement strStr().txt	
@@ -3,30 +3,25 @@ public:
     char *strStr(char *haystack, char *needle) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (!needle || (*needle == 0))
-            return haystack;
-        if (!haystack || (*haystack == 0))
+        if (!haystack || !needle)
             return NULL;
-        char *s = haystack;
-        char *e = haystack;
-        char *t = needle;
-        while (*++t)
-            e++;
-        while (*e) {
-            char* m = s;
-            char* n = needle;
-            while ((*m != 0) && (*n != 0) && (*m == *n)) {
-                m++;
-                n++;
-            }
-            if (*n == 0)
-                return s;
-            else {
-                s++;
-                e++;
+        if (!*needle)
+            return haystack;
+        int nsize = strlen(needle)-1;
+        char* htail = haystack;
+        while (*htail && nsize--)
+            htail++;
+        while (*htail) {
+            char* nhead = needle, *hhead = haystack;
+            while (*nhead && (*hhead == *nhead)) {
+                hhead++;
+                nhead++;
             }
+            if (!*nhead)
+                return haystack;
+            haystack++;
+            htail++;
         }
         return NULL;
-        
     }
 };
\ No newline at end of file
diff --git a/Insert Interval.txt b/Insert Interval.txt
index ee7eb17..bef276c 100644
--- a/Insert Interval.txt	
+++ b/Insert Interval.txt	
@@ -12,30 +12,22 @@ public:
     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<Interval> results;
-        Interval n = newInterval;
-        if (intervals.size() == 0) {
-            results.push_back(newInterval);
-            return results;
-        }
-        int p = 0;
-        while (p < intervals.size()) {
-            Interval& c = intervals[p];
-            if (n.start > c.end)
-                results.push_back(c);
-            else if (n.end < c.start) {
+        vector<Interval> rets;
+        vector<Interval>::iterator it = intervals.begin();
+        while (it != intervals.end()) {
+            if ((*it).end < newInterval.start)
+                rets.push_back(*it);
+            else if ((*it).start > newInterval.end)
                 break;
-            }
             else {
-                n.start = (n.start < c.start)?n.start:c.start;
-                n.end = (n.end > c.end)?n.end:c.end;
+                newInterval.start = min(newInterval.start, (*it).start);
+                newInterval.end = max(newInterval.end, (*it).end);
             }
-            p++;
-        }
-        results.push_back(n);
-        while (p < intervals.size()) {
-            results.push_back(intervals[p++]);
+            it++;
         }
-        return results;
+        rets.push_back(newInterval);
+        while (it != intervals.end())
+            rets.push_back(*it++);
+        return rets;
     }
 };
\ No newline at end of file
diff --git a/Integer to Roman.txt b/Integer to Roman.txt
index 35dab44..b8af698 100644
--- a/Integer to Roman.txt	
+++ b/Integer to Roman.txt	
@@ -3,38 +3,36 @@ public:
     string intToRoman(int num) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        string result = "";
-        print(result, num/1000, '0', '0', 'M');
-        num -= (num/1000)*1000;
-        print(result, num/100, 'M', 'D', 'C');
-        num -= (num/100)*100;
-        print(result, num/10, 'C', 'L', 'X');
-        num -= (num/10)*10;
-        print(result, num, 'X', 'V', 'I');
-        return result;
-        
-    }
-    void print(string& result, int num, char ten, char five, char one) {
-        if (num == 9) {
-            result += one;
-            result += ten;
-        }
-        if ((num >= 5) && (num <= 8)) {
-            result += five;
-            while (num > 5) {
-                result += one;
-                num--;
-            }
+        string ret;
+        transform(ret, num, 'X', 'V', 'I');
+        num/=10;
+        transform(ret, num, 'C', 'L', 'X');
+        num/=10;
+        transform(ret, num, 'M', 'D', 'C');
+        num/=10;
+        transform(ret, num, ' ', ' ', 'M');
+        for (int i = 0; i < ret.size()/2; i++) {
+            char t = ret[i];
+            ret[i] = ret[ret.size() - 1 - i];
+            ret[ret.size() - 1 - i] = t;
         }
-        if (num == 4) {
-            result += one;
-            result += five;
-        }
-        if ((num <= 3) && (num >= 1)) {
-            while (num > 0) {
-                result += one;
-                num--;
-            }
+        return ret;
+    }
+    void transform(string& ret, int num, char ten, char five, char one) {
+        int t = num%10;
+        if (t == 9) {
+            ret += ten;
+            ret += one;
+        } else if (t >= 5) {
+            while (t-- > 5)
+                ret += one;
+            ret += five;
+        } else if (t == 4) {
+            ret += five;
+            ret += one;
+        } else {
+            while (t-- > 0)
+                ret += one;
         }
     }
 };
\ No newline at end of file
diff --git a/Interleaving String.txt b/Interleaving String.txt
index 0b0a1b3..d359d9a 100644
--- a/Interleaving String.txt	
+++ b/Interleaving String.txt	
@@ -3,46 +3,18 @@ public:
     bool isInterleave(string s1, string s2, string s3) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (s1.empty())
-            return s2 == s3;
-        if (s2.empty())
-            return s1 == s3;
-        if (s1.size() + s2.size() != s3.size())
+        int size1 = s1.size(), size2 = s2.size(), size3 = s3.size();
+        if ((size1 + size2) != size3)
             return false;
-        int size1 = s1.size(), size2 = s2.size();
-        bool matrix[size1+1][size2+1];
-        matrix[0][0] = true;
-        int i = 1;
-        for (; i <= size1; i++) {
-            if (s1[i-1] == s3[i-1])
-                matrix[i][0] = true;
-            else
-                break;
-        }
-        for (; i <= size1; i++)
-            matrix[i][0] = false;
-        int j = 1;
-        for (; j <= size2; j++) {
-            if (s2[j-1] == s3[j-1])
-                matrix[0][j] = true;
-            else
-                break;
-        }
-        for (; j <= size2; j++)
-            matrix[0][j] = false;
-        for (int i = 1; i <= size1; i++) {
-            for (int j = 1; j <= size2; j++) {
-                if (matrix[i-1][j] && (s1[i-1] == s3[i+j-1])) {
-                    matrix[i][j] = true;
-                    continue;
-                }
-                if (matrix[i][j-1] && (s2[j-1] == s3[i+j-1])) {
-                    matrix[i][j] = true;
-                    continue;
-                }
-                matrix[i][j] = false;
-            }   
-        }
-        return matrix[size1][size2];
+        bool p[size1+1][size2+1];
+        p[0][0] = true;
+        for (int j = 1; j <= size2; j++)
+            p[0][j] = (p[0][j-1] && (s2[j-1] == s3[j-1]));
+        for (int i = 1; i <= size1; i++)
+            p[i][0] = (p[i-1][0] && (s1[i-1] == s3[i-1]));
+        for (int i = 1; i <= size1; i++)
+            for (int j = 1; j <= size2; j++)
+                p[i][j] = (p[i-1][j] && (s1[i-1] == s3[i+j-1]))||(p[i][j-1] && (s2[j-1] == s3[i+j-1]));
+        return p[size1][size2];
     }
 };
\ No newline at end of file
diff --git a/Jump Game 2.txt b/Jump Game 2.txt
index 03076ad..f0c880b 100644
--- a/Jump Game 2.txt	
+++ b/Jump Game 2.txt	
@@ -3,15 +3,16 @@ public:
     int jump(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int oldLast = 0, newLast = INT_MIN, step = 0, i = 0;
-        while (i <= min(n-1, oldLast)) {
-            newLast = max(newLast, i + A[i]);
-            if (i == oldLast) {
-                step += (i == n - 1)?0:1;
-                oldLast = newLast;
+        int last = 0, nlast = 0, step = 0;
+        for (int i = 0; (i < n) && (i <= last); i++) {
+            if (i == n - 1)
+                break;
+            nlast = max(nlast, i + A[i]);
+            if (i == last) {
+                last = nlast;
+                step++;
             }
-            i++;
         }
-        return step;
+        return -1;
     }
 };
\ No newline at end of file
diff --git a/Jump Game.txt b/Jump Game.txt
index f64f45f..6b03ffe 100644
--- a/Jump Game.txt	
+++ b/Jump Game.txt	
@@ -3,11 +3,9 @@ public:
     bool canJump(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n == 0)
-            return true;
-        int last = A[0];
-        for (int i = 1; i <= min(last, n-1); i++)
-            last = max(last, i + A[i]);
-        return (last >= n - 1);
+        int last = 0;
+	    for (int i = 0; (i <= last) && (i < n); i++)
+		    last = (i + A[i] > last)?i + A[i]:last;
+	    return (n == 0)?true:(last >= n-1);
     }
 };
\ No newline at end of file
diff --git a/Largest Rectangle in Histogram.txt b/Largest Rectangle in Histogram.txt
index 4ae4fa9..3e57b10 100644
--- a/Largest Rectangle in Histogram.txt	
+++ b/Largest Rectangle in Histogram.txt	
@@ -3,25 +3,24 @@ public:
     int largestRectangleArea(vector<int> &height) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int ret = 0, size = height.size();
-        if (size > 0) {
-            int l[size], r[size];
-            l[0] = 0, r[size-1] = size-1;
-            for (int i = 1; i < size; i++) {
-                int k = i;
-                while ((k > 0) && (height[i] <= height[k-1]))
-                    k = l[k-1];
-                l[i] = k;
-            }
-            for (int i = size - 2; i >= 0; i--) {
-                int k = i;
-                while ((k < size-1) && (height[i] <= height[k+1]))
-                    k = r[k+1];
-                r[i] = k;
-            }
-            for (int i = 0; i < size; i++)
-                ret = max(ret, height[i]*(r[i] + 1 - l[i]));
+        if (height.size() == 0)
+            return 0;
+        int size = height.size(), l[size], r[size], ret = 0;
+        l[0] = 0, r[size-1] = size-1;
+        for (int i = 1; i < size; i++) {
+            int k = i;
+            while ((k > 0) && (height[i] <= height[k-1]))
+                k = l[k-1];
+            l[i] = k;
         }
+        for (int i = size - 2; i >= 0; i--) {
+            int k = i;
+            while ((k < size - 1) && (height[i] <= height[k+1]))
+                k = r[k+1];
+            r[i] = k;
+        }
+        for (int i = 0; i < size; i++)
+            ret = max(ret, height[i]*(r[i] - l[i] + 1));
         return ret;
     }
 };
\ No newline at end of file
diff --git a/Length of Last Word.txt b/Length of Last Word.txt
index 0394cf3..5fe033d 100644
--- a/Length of Last Word.txt	
+++ b/Length of Last Word.txt	
@@ -3,14 +3,13 @@ public:
     int lengthOfLastWord(const char *s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size = strlen(s), l = size - 1;
-        while ((l >= 0) && !isalpha(tolower(*(s+l))))
-            l--;
-        if (l < 0)
-            return 0;
-        int u = l;
-        while ((l >= 0) && isalpha(tolower(*(s+l))))
-            l--;
-        return u - l;
+        if (!s)
+    	    return 0;
+        int size = strlen(s), len = 0;
+        const char* t = s + size;
+        while ((--t >= s) && (*t == ' '));
+        while ((t >= s) && (*t-- != ' '))
+            len++;
+        return len;
     }
 };
\ No newline at end of file
diff --git a/Longest Common Prefix.txt b/Longest Common Prefix.txt
index 2783e32..3eb0d78 100644
--- a/Longest Common Prefix.txt	
+++ b/Longest Common Prefix.txt	
@@ -3,28 +3,20 @@ public:
     string longestCommonPrefix(vector<string> &strs) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        string ret = "";
-        if (strs.size() > 0) {
-            int p = 0;
-            while (true) {
-                if (p == strs[0].size())
-                    break;
-                char t = strs[0][p];
-                int i = 1;
-                for (; i < strs.size(); i++) {
-                    if (p == strs[i].size())
-                        break;
-                    if (strs[i][p] != t)
-                        break;
-                }
-                if (i == strs.size())
-                    ret += t;
-                else
-                    break;
-                p++;
-            }
-        }
-        return ret;
-        
+        if (strs.empty())
+	    return "";
+        int i = 0;
+	for (;i < strs[0].size(); i++) {
+	    size_t j = 1;
+	    for (; j < strs.size(); j++) {
+		if (i == strs[j].size())
+		    break;
+		if (strs[j][i] != strs[0][i])
+		    break;
+	    }
+	    if (j < strs.size())
+            	break;
+	}
+	return strs[0].substr(0, i);
     }
 };
\ No newline at end of file
diff --git a/Longest Consecutive Sequence.txt b/Longest Consecutive Sequence.txt
new file mode 100644
index 0000000..f1a7099
--- /dev/null
+++ b/Longest Consecutive Sequence.txt	
@@ -0,0 +1,21 @@
+class Solution {
+public:
+    int longestConsecutive(vector<int> &num) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        unordered_set<int> all;
+	for (size_t i = 0; i < num.size(); i++)
+	    all.insert(num[i]);
+	int mav = 0;
+	while (!all.empty()) {
+	    int p = *all.begin(), l = p - 1, r = p + 1;
+	    all.erase(all.begin());
+	    while (!all.empty() && all.erase(l))
+                l--;
+	    while (!all.empty() && all.erase(r))
+                r++;
+	    mav = max(mav, r - l - 1);	
+	}
+	return mav;
+    }
+};
\ No newline at end of file
diff --git a/Longest Palindromic Substring.txt b/Longest Palindromic Substring.txt
index 6c67994..57a6abc 100644
--- a/Longest Palindromic Substring.txt	
+++ b/Longest Palindromic Substring.txt	
@@ -5,29 +5,25 @@ public:
         // DO NOT write int main() function
         if (s.empty())
             return s;
-        string t = "";
-        for (int i = 0; i < s.size(); i++)
-            t = t + '#' + s[i];
-        t += '#';
-        vector<int> p(t.size(), 0);
+        string copy = "#";
+        for (size_t i = 0; i < s.size(); i++)
+            copy = copy + s[i] + '#';
+        vector<int> ra(copy.size(), 0);
         int c = 0, r = 0;
-        for (int i = 1; i < t.size() - 1; i++) {
-            int mirror_i = 2*c - i;
-            p[i] = (r > i)?min(r - i, p[mirror_i]):0;
-            while ((i-p[i]-1 >= 0) && (i+p[i]+1 <= t.size() - 1) && (t[i+p[i]+1] == t[i-p[i]-1]))
-                p[i]++;
-            if (i + p[i] > r) {
-                r = p[i] + i;
+        for (int i = 1; i < copy.size(); i++) {
+            int m = 2*c - i;
+            ra[i] = (i >= r)?0:min(ra[m], r - i);
+            while ((i+ra[i]+1 < copy.size()) && (i-ra[i]-1 >= 0) && (copy[i-ra[i]-1] == copy[i+ra[i]+1]))
+                ra[i]++;
+            if (i + ra[i] > r) {
                 c = i;
+                r = i + ra[i];
             }
         }
-        int mav = 0, index = 0;
-        for (int i = 1; i < t.size() - 1; i++) {
-            if (p[i] > mav) {
-                mav = p[i];
-                index = i;
-            }
-        }
-        return s.substr((index - mav)/2, mav);
+        string ret = "";
+        for (int i = 0; i < copy.size(); i++)
+            if (ra[i] > ret.size())
+                ret = s.substr((i-ra[i])/2, ra[i]);
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Longest Substring Without Repeating Characters.txt b/Longest Substring Without Repeating Characters.txt
index 916acbc..b7614f8 100644
--- a/Longest Substring Without Repeating Characters.txt	
+++ b/Longest Substring Without Repeating Characters.txt	
@@ -3,23 +3,19 @@ public:
     int lengthOfLongestSubstring(string s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (s.size() <= 1)
-            return s.size();
-        int n = 256, b = 0, mav = 1;
-        bool c[n];
-        memset(c, 0x00, sizeof(bool)*n);
-        c[s[b]] = true;
-        for (int i = 1; i < s.size(); i++) {
-            if (!c[s[i]]) {
-                c[s[i]] = true;
-                mav = max(mav, i - b + 1);
-            }
-            else {
-                while (s[b] != s[i])
-                    c[s[b++]] = false;
-                b++;
-            }
-        }
-        return mav;
+        int mav = 0, l = 0, i = 0;
+        vector<bool> use(255, false);
+	    while (i < s.size()) {
+		    if (!use[s[i]]) {
+			    use[s[i]] = true;
+			    mav = max(mav, i - l + 1);
+	        } else {
+			    while (s[l] != s[i])
+				    use[s[l++]] = false;
+	        	l++;
+		    }
+		    i++;
+	    }
+	    return mav;
     }
 };
\ No newline at end of file
diff --git a/Longest Valid Parentheses.txt b/Longest Valid Parentheses.txt
index 5e3db07..0a4cd67 100644
--- a/Longest Valid Parentheses.txt	
+++ b/Longest Valid Parentheses.txt	
@@ -3,29 +3,26 @@ public:
     int longestValidParentheses(string s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size = s.size();
-        if (size == 0)
-            return 0;
-        vector<pair<char, int> > b;
-        vector<bool> use(size, false);
-        for (int i = 0; i < size; i++) {
-            if ((s[i] == ')') && !b.empty() && (b.back().first == '(')) {
-                use[i] = true;
-                use[b.back().second] = true;
-                b.pop_back();
-            }
-            else
-                b.push_back(pair<char, int>(s[i], i));
-        }
-        int p = 0, l = 0, mav = 0;
-        while (p < size) {
-            while ((p < size) && (use[p] == false))
-                p++;
-            l = p;
-            while ((p < size) && (use[p] == true))
-                p++;
-            mav = max(mav, p - l); 
-        }
-        return mav;
+        int p = 0, mav = 0, l = -1;
+        vector<pair<char, int>> b;
+        vector<bool> u(s.size(), false);
+        while (p < s.size()) {
+	    if (s[p] == '(')
+		    b.push_back(pair<char, int>(s[p], p));
+	    else if (!b.empty() && b.back().first == '(') {
+                u[p] = true;
+		u[b.back().second] = true;
+		b.pop_back();
+	    }
+	    p++;
+	}
+	for (int i = 0; i < s.size(); i++) {
+	    if (u[i]) {
+		l = (l == -1)?i:l;
+		mav = max(mav, i - l + 1);
+	    } else
+	    l = -1;
+	}
+	return mav;
     }
 };
\ No newline at end of file
diff --git a/Maximal Rectangle.txt b/Maximal Rectangle.txt
index 143b503..16ab500 100644
--- a/Maximal Rectangle.txt	
+++ b/Maximal Rectangle.txt	
@@ -3,55 +3,36 @@ public:
     int maximalRectangle(vector<vector<char> > &matrix) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int y = matrix.size();
-        if (y == 0) return 0;
-        int x = matrix[0].size();
-        if (x == 0) return 0;
-        
-        vector< vector<int> > result(y, vector<int>(x));
-        for (int j = 0; j < y; ++j)
-        {
-            int tmp = 0;
-            for (int i = 0; i < x; ++i)
-            {
-                if ('0' == matrix[j][i])
-                    tmp =  0;
-                else
-                    ++tmp;
-                result[j][i] = tmp;
-            }
+        int nrows = matrix.size();
+        if (!nrows)
+            return 0;
+        int ncols = matrix[0].size();
+        if (!ncols)
+            return 0;
+        int height[nrows][ncols];
+        for (int i = 0; i < nrows; i++) {
+            height[i][ncols-1] = (matrix[i][ncols-1] == '1')?1:0;
+            for (int j = ncols - 2; j >= 0; j--)
+                height[i][j] = (matrix[i][j] == '1')?height[i][j+1]+1:0;
         }
-        
-        int max = 0;
-        int* l = new int[y];  
-        int* r = new int[y];  
-        for (int i = 0; i < x; ++i)
-        {
-            l[0] = 0;
-            for (int j = 1; j < y; ++j)
-            {
-                int k = j;  
-                while(k > 0 && result[j][i] <= result[k-1][i])  
-                    k = l[k-1];  
-                l[j] = k;  
+        int l[nrows], r[nrows], mav = 0;
+        for (int j = 0; j < ncols; j++) {
+            l[0] = 0, r[nrows-1] = nrows - 1;
+            for (int i = 1; i < nrows; i++) {
+                int k = i;
+                while ((k > 0) && (height[i][j] <= height[k-1][j]))
+                    k = l[k-1];
+                l[i] = k;
+            }
+            for (int i = nrows - 2; i >= 0; i--) {
+                int k = i;
+                while ((k < nrows - 1) && (height[i][j] <= height[k+1][j]))
+                    k = r[k+1];
+                r[i] = k;
             }
-            
-            r[y-1] = y - 1; 
-            for(int j = y-2; j >= 0; --j)  
-            {  
-                int k = j;  
-                while(k < y - 1 && result[j][i] <= result[k+1][i])  
-                    k = r[k+1];  
-                r[j] = k;  
-            } 
-            
-            for (int j = 0; j < y; ++j)  
-            {  
-                int ar = (r[j] - l[j] + 1) * result[j][i];  
-                max = max > ar ? max : ar;     
-            }  
+            for (int i = 0; i < nrows; i++)
+                mav = max(mav, height[i][j]*(r[i]-l[i]+1));
         }
-
-        return max;
+        return mav;
     }
 };
\ No newline at end of file
diff --git a/Maximum Subarray.txt b/Maximum Subarray.txt
index bb42f27..3546a9e 100644
--- a/Maximum Subarray.txt	
+++ b/Maximum Subarray.txt	
@@ -3,13 +3,11 @@ public:
     int maxSubArray(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n == 1)
-            return A[0];
-        int mav = INT_MIN, tmp = 0;
+        int mav = INT_MIN, sum = 0;
         for (int i = 0; i < n; i++) {
-            tmp = tmp + A[i];
-            mav = max(tmp, mav);
-            tmp = max(tmp, 0);
+            sum += A[i];
+            mav = max(mav, sum);
+            sum = max(sum, 0);
         }
         return mav;
     }
diff --git a/Merge Intervals.txt b/Merge Intervals.txt
index 9883a68..9b83fe9 100644
--- a/Merge Intervals.txt	
+++ b/Merge Intervals.txt	
@@ -7,38 +7,26 @@
  *     Interval(int s, int e) : start(s), end(e) {}
  * };
  */
-
+struct myclass{
+    bool operator()(const Interval& a, const Interval& b) {
+        return a.start < b.start;
+    }
+} myobj;
 class Solution {
 public:
-    struct my{
-        bool operator()(const Interval& a, const Interval& b) {
-            return a.start < b.start;
-        }
-    } myo;
     vector<Interval> merge(vector<Interval> &intervals) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<Interval> r;
-        vector<Interval>& s = intervals;
-        if (s.size() == 0)
-            return r;
-        sort(s.begin(), s.end(), myo);
-        r.push_back(s.front());
-        s.erase(s.begin());
-        while (s.size() > 0) {
-            Interval& l = r.back();
-            Interval& t = s.front();
-            if (l.end < t.start) {
-                r.push_back(t);
-                s.erase(s.begin());
-            }
-            else {
-                l.end = (l.end > t.end)?l.end:t.end;
-                s.erase(s.begin());
-            }
+        sort(intervals.begin(), intervals.end(), myobj);
+        vector<Interval> ret;
+        while (!intervals.empty()) {
+            vector<Interval>::iterator it = intervals.begin();
+            if (ret.empty() || ((*it).start > ret.back().end))
+                ret.push_back(*it);    
+            else
+                ret.back().end = max(ret.back().end, (*it).end);
+            intervals.erase(it);
         }
-        return r;
-
-        
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Merge Sorted Array.txt b/Merge Sorted Array.txt
index 9785dff..10cf345 100644
--- a/Merge Sorted Array.txt	
+++ b/Merge Sorted Array.txt	
@@ -3,25 +3,12 @@ public:
     void merge(int A[], int m, int B[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n == 0)
-            return;
-        for (int i = m - 1; i >= 0; i--)
-            A[n+i] = A[i];
-        int a = n;
-        int b = 0;
-        int i = 0;
-        while ((a < (m + n)) && (b < n)) {
-            if (A[a] <= B[b]) {
-                A[i++] = A[a];
-                a++;
-            } else {
-                A[i++] = B[b];
-                b++;
-            }
-        }
-        while (b < n) {
-            A[i++] = B[b];
-            b++;
-        }
+        int ap = m - 1, bp = n - 1, p = m + n - 1;
+        while (ap >= 0 && bp >= 0)
+            A[p--] = (A[ap] >= B[bp])?A[ap--]:B[bp--];
+        while (ap >= 0)
+            A[p--] = A[ap--];
+        while (bp >= 0)
+            A[p--] = B[bp--];
     }
 };
\ No newline at end of file
diff --git a/Merge Two Sorted Lists.txt b/Merge Two Sorted Lists.txt
index 7e39646..9de4fd7 100644
--- a/Merge Two Sorted Lists.txt	
+++ b/Merge Two Sorted Lists.txt	
@@ -11,33 +11,31 @@ public:
     ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        ListNode* root = new ListNode(0);
-        ListNode* tail = root;
-        while (l1 && l2) {
-            if (l1->val < l2->val) {
+        if (!l1)
+            return l2;
+        if (!l2)
+            return l1;
+        ListNode *head = new ListNode(0), *tail = head;
+        while (l1 || l2) {
+            if (l1 && l2) {
+                if (l1->val <= l2->val) {
+                    tail->next = l1;
+                    l1 = l1->next;
+                } else {
+                    tail->next = l2;
+                    l2 = l2->next;
+                }
+            } else if (l1) {
                 tail->next = l1;
                 l1 = l1->next;
-            }
-            else {
+            } else {
                 tail->next = l2;
                 l2 = l2->next;
             }
             tail = tail->next;
         }
-        while (l1) {
-            tail->next = l1;
-            l1 = l1->next;
-            tail = tail->next;
-        }
-        while (l2) {
-            tail->next = l2;
-            l2 = l2->next;
-            tail = tail->next;
-        }
-        tail->next = NULL;
-        ListNode* head = root->next;
-        delete root;
-        return head;
-        
+        tail = head->next;
+        delete head;
+        return tail;
     }
 };
\ No newline at end of file
diff --git a/Merge k Sorted Lists.txt b/Merge k Sorted Lists.txt
index e94deeb..3f8ec15 100644
--- a/Merge k Sorted Lists.txt	
+++ b/Merge k Sorted Lists.txt	
@@ -12,25 +12,21 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         multimap<int, ListNode*> front;
-        for (int i = 0; i < lists.size(); i++) {
-            if (lists[i] != NULL)
-                front.insert(make_pair<int, ListNode*>(lists[i]->val, lists[i]));
+        for (size_t i = 0; i < lists.size(); i++)
+            if (lists[i])
+                front.insert(make_pair(lists[i]->val, lists[i]));
+        ListNode *head = new ListNode(0), *tail = head;
+        while (!front.empty()) {
+            multimap<int, ListNode*>::iterator it = front.begin();
+            tail->next = it->second;
+            tail = tail->next;
+            front.erase(it);
+            if (tail->next)
+                front.insert(make_pair(tail->next->val, tail->next));
         }
-        ListNode* head = new ListNode(0);
-        ListNode* tail = head;
-        while (front.size() > 0) {
-            ListNode* pops = front.begin()->second;
-            front.erase(front.begin());
-            tail->next = pops;
-            ListNode* node = pops->next;
-            if (node != NULL)
-                front.insert(make_pair<int, ListNode*>(node->val, node));
-            pops->next = NULL;
-            tail = pops;
-        }
-        ListNode* temp = head;
-        head = head->next;
-        delete temp;
-        return head;
+        tail = head->next;
+        delete head;
+        return tail;
+        
     }
 };
\ No newline at end of file
diff --git a/Minimum Depth of Binary Tree.txt b/Minimum Depth of Binary Tree.txt
index afe53ff..a566144 100644
--- a/Minimum Depth of Binary Tree.txt	
+++ b/Minimum Depth of Binary Tree.txt	
@@ -16,11 +16,7 @@ public:
             return 0;
         int l = minDepth(root->left);
         int r = minDepth(root->right);
-        if (l == 0)
-            return r+1;
-        if (r == 0)
-            return l+1;
-        return (l < r)?l+1:r+1;
+        return (l == r)?(l+1):min(l, r)?min(l, r)+1:max(l, r)+1;
         
     }
 };
\ No newline at end of file
diff --git a/Minimum Path Sum.txt b/Minimum Path Sum.txt
index 721f8a3..2e52dc0 100644
--- a/Minimum Path Sum.txt	
+++ b/Minimum Path Sum.txt	
@@ -3,25 +3,19 @@ public:
     int minPathSum(vector<vector<int> > &grid) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (grid.size() == 0)
+        int nrows = grid.size();
+        if (!nrows)
             return 0;
-        if (grid[0].size() == 0)
+        int ncols = grid[0].size();
+        if (!ncols)
             return 0;
-        vector<int> path(grid[0]);
-        int size = path.size();
-        for (int i = 1; i < size; i++) {
-            path[i] = path[i-1] + path[i];
-        }
-        int k = 1;
-        while (k < grid.size()) {
-            path[0] += grid[k][0];
-            for (int j = 1; j < size; j++) {
-                path[j] = ((path[j] < path[j-1])?path[j]:path[j-1]) + grid[k][j];
-            }
-            k++;
-        }
-        return path[size - 1];
-            
-        
+        for (int j = 1; j < ncols; j++)
+            grid[0][j] = grid[0][j-1] + grid[0][j];
+        for (int i = 1; i < nrows; i++)
+            grid[i][0] = grid[i-1][0] + grid[i][0];
+        for (int i = 1; i < nrows; i++)
+            for (int j = 1; j < ncols; j++)
+                grid[i][j] = min(grid[i-1][j], grid[i][j-1]) + grid[i][j];
+        return grid[nrows-1][ncols-1];
     }
 };
\ No newline at end of file
diff --git a/Minimum Window Substring.txt b/Minimum Window Substring.txt
index 9248063..079b34a 100644
--- a/Minimum Window Substring.txt	
+++ b/Minimum Window Substring.txt	
@@ -3,66 +3,47 @@ public:
     string minWindow(string S, string T) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int ssize = S.size();
-        int tsize = T.size();
-        if (ssize == 0)
+        vector<int> tuse(255, 0), suse(255, 0);
+        vector<char> tcha;
+        string ret;
+        int tsize = T.size(), ssize = S.size(), snum = 0, l = -1, i = 0, w = INT_MAX;
+        if (!tsize || !ssize || (tsize > ssize))
             return "";
-        if (tsize == 0)
-            return "";
-        string result;
-        int t[255], s[255], tnum = 0, snum = 0;
-        set<char> pos;
-        memset(t, 0x00, sizeof(int)*255);
-        memset(s, 0x00, sizeof(int)*255);
         for (int i = 0; i < tsize; i++) {
-            t[T[i]]++;
-            tnum++;
-            pos.insert(T[i]);
+            tuse[T[i]]++;
+            if (tuse[T[i]] == 1)
+                tcha.push_back(T[i]);
         }
-        int min = ssize+1;
-        int sta = 0;
-        while((sta < ssize) && (t[S[sta]] == 0))
-            sta++;
-        for (int i = sta; i < ssize; i++) {
-            if (t[S[i]] > 0) {
-                s[S[i]]++;
+        while (i < ssize) {
+            if (tuse[S[i]] != 0) {
+                l = (l == -1)?i:l;
+                suse[S[i]]++;
                 snum++;
-                if (s[S[i]] > t[S[i]]) {
-                    while (true) {
-                        if (t[S[sta]] == 0) {
-                            sta++;
-                            continue;
-                        }
-                        else {
-                            if (s[S[sta]] > t[S[sta]]) {
-                                s[S[sta]]--;
-                                sta++;
-                                snum--;
-                            }
-                            else
-                                break;
-                        }
+                if (tuse[S[i]] < suse[S[i]]) {
+                    while(true) {
+                        if (tuse[S[l]] == 0)
+                            l++;
+                        else if (tuse[S[l]] < suse[S[l]]) {
+                            suse[S[l]]--;
+                            snum--;
+                            l++;
+                        } else
+                            break;
                     }
                 }
-                if ((snum >= tnum) && (check(s, t, pos)) && ((i + 1 - sta) < min)) {
-                    min = i + 1 - sta;
-                    result = S.substr(sta, min);
+                if ((snum >= tsize) && check(tuse, suse, tcha) && (w > (i - l + 1))) {
+                    w = i - l + 1;
+                    ret = S.substr(l, w);
                 }
             }
+            i++;
         }
-        return result;
-        
+        return ret;   
     }
-    bool check(int* s, int* t, set<char>& pos) {
-        bool flag = true;
-        set<char>::iterator p = pos.begin();
-        while (p != pos.end()) {
-            if (s[*p] < t[*p]) {
-                flag = false;
-                break;
-            }
-            p++;
-        }
-        return flag;
+    bool check(vector<int>& t, vector<int>& s, vector<char>& c) {
+        for (size_t i = 0; i < c.size(); i++)
+            if (t[c[i]] > s[c[i]])
+                return false;
+        return true;
     }
 };
\ No newline at end of file
diff --git a/Multiply Strings.txt b/Multiply Strings.txt
index f54ea31..c528a81 100644
--- a/Multiply Strings.txt	
+++ b/Multiply Strings.txt	
@@ -3,53 +3,45 @@ public:
     string multiply(string num1, string num2) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if ((num1 == "0") || (num2 == "0"))
-            return "0";
-        if (num1 == "1")
-            return num2;
-        if (num2 == "1")
-            return num1;
-        int size1=num1.size();
-        int size2=num2.size();
+        int size1 = num1.size(), size2 = num2.size(), size3 = size1 + size2;
+        if (!size1 || !size2)
+            return "";
         vector<int> n1, n2;
         for (int i = size1 - 1; i >= 0; i--)
             n1.push_back(num1[i] - '0');
         for (int i = size2 - 1; i >= 0; i--)
             n2.push_back(num2[i] - '0');
-        vector<int> result(size1 + size2 + 2, 0);
-        for (int j = 0; j < n2.size(); j++) {
-            vector<int> current(j, 0);
-            int car = 0;
-            for (int i = 0; i < n1.size(); i++) {
-                int mul = n1[i]*n2[j] + car;
-                current.push_back(mul%10);
-                car = mul/10;
-            }
-            if (car > 0)
-                current.push_back(car);
-            add(result, current);
+        vector<int> r1(size3+1, 0);
+        multiply(r1, n1, n2, 0);
+        for (int i = 1; i < num2.size(); i++) {
+            vector<int> r2(size3+1, 0);
+            multiply(r2, n1, n2, i);
+            add(r1, r2);
         }
-        while ((result.size() > 0) && (result.back() == 0))
-            result.pop_back();
-        string r = "";
-        for (int i = result.size() - 1; i >= 0; i--)
-            r+= (char)('0' + result[i]);
-        return r;
+        while (r1.back() == 0)
+            r1.pop_back();
+        string ret;
+        for (int i = r1.size() - 1; i >= 0; i--)
+            ret += (char)('0' + r1[i]);
+        return ret.empty()?"0":ret;
     }
-    void add(vector<int>& r, vector<int>& c) {
-        int car = 0;
-        int i = 0;
-        for (; i < c.size(); i++) {
-            int add = r[i] + c[i] + car;
-            r[i] = add%10;
-            car = add/10;
+    void multiply(vector<int>& r, vector<int>& n1, vector<int>& n2, int p) {
+        int m = n2[p], c = 0;
+        for (int i = 0; i < n1.size(); i++) {
+            int sum = m*n1[i] + c;
+            r[p++] = sum%10;
+            c = sum/10;
         }
-        for (; i < r.size(); i++) {
-            int add = r[i] + car;
-            r[i] = add%10;
-            car = add/10;
-            if (!car)
-                break;
+        if (c > 0)
+            r[p] = c;
+    }
+    void add(vector<int>& r1, vector<int> r2) {
+        int p = 0, c = 0;
+        while (p < r1.size()) {
+            int sum = r1[p] + r2[p] + c;
+            r1[p] = sum%10;
+            c = sum/10;
+            p++;
         }
     }
 };
\ No newline at end of file
diff --git a/N-Queens.txt b/N-Queens.txt
index ca6a6bf..1a4b710 100644
--- a/N-Queens.txt
+++ b/N-Queens.txt
@@ -3,42 +3,43 @@ public:
     vector<vector<string> > solveNQueens(int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<string> > rets;
-        string empty(n, '.');
-        vector<string> ret(n, empty);
-        vector<int> pos;
+        vector<vector<string>> rets;
+        if (n == 0)
+            return rets;
+        vector<int> plan(n, 0);
         for (int i = 0; i < n; i++)
-            pos.push_back(i);
-        plan(rets, ret, pos, 0);
+            plan[i] = i;
+        solve(plan, 0, n, rets);
         return rets;
     }
-    void plan(vector<vector<string> > &rets, vector<string> & ret, vector<int>& pos, int p) {
-        if (p == pos.size()) {
-            rets.push_back(ret);
-            for (int i = 0; i < pos.size(); i++)
-                rets.back()[i][pos[i]] = 'Q';
-        }
-        else {
-            if (check(pos, p))
-                plan(rets, ret, pos, p+1);
-            for (int i = p + 1; i < pos.size(); i++) {
-                swap(pos[p], pos[i]);
-                if (check(pos, p))
-                    plan(rets, ret, pos, p+1);
-                swap(pos[p], pos[i]);
+    void solve(vector<int> &plan, int p, int n, vector<vector<string>> &r) {
+        if (p == n) {
+            r.push_back(vector<string>(0));
+            for (int i = 0; i < n; i++) {
+                string s(n, '.');
+                s[plan[i]] = 'Q';
+                r.back().push_back(s);
             }
+            return;
         }
-    }
-    bool check(vector<int> &pos, int p) {
-        for (int i = 0; i < p; i++) {
-            if (abs(i-p) == abs(pos[i] - pos[p]))
-                    return false;
+        if (valid(plan, p))
+            solve(plan, p+1, n, r);
+        for (int i = p + 1; i < n; i++) {
+            swapp(plan, p, i);
+            if (valid(plan, p))
+                solve(plan, p+1, n, r);
+            swapp(plan, p, i);
         }
-        return true;
     }
-    void swap(int& a, int& b) {
-        int t = a;
-        a = b;
-        b = t;
+    void swapp(vector<int> &plan, int i, int j) {
+        int t = plan[j];
+        plan[j] = plan[i];
+        plan[i] = t;
+    }
+    bool valid(vector<int> &plan, int p) {
+        for (int i = 0; i < p; i++)
+            if (abs(p - i) == abs(plan[p] - plan[i]))
+                return false;
+        return true;
     }
 };
\ No newline at end of file
diff --git a/Next Permutation.txt b/Next Permutation.txt
index f0b5a73..f35db96 100644
--- a/Next Permutation.txt	
+++ b/Next Permutation.txt	
@@ -3,28 +3,24 @@ public:
     void nextPermutation(vector<int> &num) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size = num.size();
-        if ((size == 0) || (size == 1))
+        if (num.empty())
             return;
-        int mav = num.back(), i = size - 2;
+        int mval = num.back(), mind = num.size() - 1, i = num.size() - 2;
         for (; i >= 0; i--) {
-            if (num[i] < mav)
-                break;
-            mav = max(mav, num[i]);
+            if (num[i] < num[mind]) {
+                for (int k = i + 1; k < num.size(); k++)
+                    if ((num[k] > num[i]) && (num[k] < num[mind]))
+                        mind = k;
+                break;  
+            } else
+                mind = i;
         }
         if (i < 0)
             sort(num.begin(), num.end());
         else {
-            int pos = 0, dif = INT_MAX;
-            for (int j = i + 1; j < size; j++) {
-                if ((num[i] < num[j]) && ((num[j] - num[i]) < dif)) {
-                    pos = j;
-                    dif = num[j] - num[i];
-                }
-            }
             int t = num[i];
-            num[i] = num[pos];
-            num[pos] = t;
+            num[i] = num[mind];
+            num[mind] = t;
             sort(num.begin()+i+1, num.end());
         }
     }
diff --git a/Palindrome Number.txt b/Palindrome Number.txt
index c3b1ebe..6748367 100644
--- a/Palindrome Number.txt	
+++ b/Palindrome Number.txt	
@@ -5,14 +5,14 @@ public:
         // DO NOT write int main() function
         if (x < 0)
             return false;
-        int div = 1;
-        while (x/div >= 10)
-            div *= 10;
-        while (x >= 10) {
-            if (x/div != x%10)
+        int p = 1;
+        while (x/p >= 10)
+            p*=10;
+        while(x >= 10) {
+            if (x/p != x%10)
                 return false;
-            x = (x % div)/10;
-            div = div/100;
+            x = (x%p)/10;
+            p /= 100;
         }
         return true;
     }
diff --git a/Palindrome Partitioning.txt b/Palindrome Partitioning.txt
new file mode 100644
index 0000000..f14fc84
--- /dev/null
+++ b/Palindrome Partitioning.txt	
@@ -0,0 +1,38 @@
+class Solution {
+public:
+    vector<vector<string>> partition(string s) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        vector<vector<string>> rets;
+        if (s.empty())
+            return rets;
+        string c = "#";
+        for (int i = 0; i < s.size(); i++)
+            c = c + s[i] + "#";
+        vector<int> r(c.size(), 0);
+        for (int i = 0; i < c.size(); i++) {
+            int l = 1;
+            while ((i-l >= 0) && (i+l < c.size()) && (c[i-l] == c[i+l]))
+                l++;
+            r[i] = l - 1;
+        }
+        vector<string> ret;
+        plan(rets, ret, c, s, r, 1);
+        return rets;
+    }
+    void plan(vector<vector<string>> &rs, vector<string> &r, string& c, string& s, vector<int> &d, int p) {
+        if (p == c.size()) {
+            rs.push_back(r);
+            return;
+        } else {
+            for (int i = p; i < c.size() - 1; i++) {
+                int rad = i - p + 1;
+                if ((rad <= d[i]) && (i + rad < c.size())) {
+                    r.push_back(s.substr((i - rad)/2, rad));
+                    plan(rs, r, c, s, d, i + rad + 1);
+                    r.pop_back();
+                }
+            }
+        }
+    }
+};
\ No newline at end of file
diff --git a/Palindrome PartitioningII.txt b/Palindrome PartitioningII.txt
new file mode 100644
index 0000000..48a784b
--- /dev/null
+++ b/Palindrome PartitioningII.txt	
@@ -0,0 +1,28 @@
+class Solution {
+public:
+    int minCut(string s) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        if (s.size() < 2)
+            return 0;
+        string c = "#";
+        for (int i = 0; i < s.size(); i++)
+            c = c + s[i] + '#';
+        vector<int> r(c.size(), 0);
+        for (int i = 0; i < c.size(); i++) {
+            int p = 1;
+            while ((i-p >= 0) && (i+p < c.size()) && (c[i+p] == c[i-p]))
+                p++;
+            r[i] = p - 1;
+        }
+        int plan[s.size()+1];
+        plan[0] = -1;
+        for (int i = 1; i <= s.size(); i++) {
+            plan[i] = plan[i-1]+1;
+            for (int j = 0; j < i - 1; j++)
+                if (r[i+j] >= (i - j))
+                    plan[i] = min(plan[i], plan[j]+1);
+        }
+        return plan[s.size()];
+    }
+};
\ No newline at end of file
diff --git a/Partition List.txt b/Partition List.txt
index 4243e1c..6a19480 100644
--- a/Partition List.txt	
+++ b/Partition List.txt	
@@ -11,26 +11,23 @@ public:
     ListNode *partition(ListNode *head, int x) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        ListNode* prev = new ListNode(0);
-        ListNode* succ = new ListNode(0);
-        ListNode *pt = prev, *st = succ;
-        while (head != NULL) {
-            ListNode* temp = head;
-            head = head->next;
-            if (temp->val < x) {
-                pt->next = temp;
-                pt = pt->next;
-            }
-            else {
-                st->next = temp;
-                st = st->next;
+        ListNode *l1 = new ListNode(0), *t1 = l1;
+        ListNode *l2 = new ListNode(0), *t2 = l2;
+        while (head) {
+            if (head->val < x) {
+                t1->next = head;
+                t1 = t1->next;
+            } else {
+                t2->next = head;
+                t2 = t2->next;
             }
+            head = head->next;
         }
-        pt->next = succ->next;
-        st->next = NULL;
-        delete succ;
-        head = prev->next;
-        delete prev;
+        t2->next = NULL;
+        t1->next = l2->next;
+        delete l2;
+        head = l1->next;
+        delete l1;
         return head;
     }
 };
\ No newline at end of file
diff --git a/Pascal's Triangle II.txt b/Pascal's Triangle II.txt
index 65320a7..fc26843 100644
--- a/Pascal's Triangle II.txt	
+++ b/Pascal's Triangle II.txt	
@@ -3,12 +3,9 @@ public:
     vector<int> getRow(int rowIndex) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<int> ret;
-        ret.push_back(1);
-        if (rowIndex > 0) {
-            for (int i = 1; i <= rowIndex; i++)
-                ret.push_back(((double)ret.back())*(rowIndex - i + 1)/i);
-        }
+        vector<int> ret(1, 1);
+        for (int j = 1; j <= rowIndex; j++)
+            ret.push_back(ret.back()*(double)(rowIndex-j+1)/j);
         return ret;
     }
 };
\ No newline at end of file
diff --git a/Pascal's Triangle.txt b/Pascal's Triangle.txt
index 11e4f5c..80e5958 100644
--- a/Pascal's Triangle.txt	
+++ b/Pascal's Triangle.txt	
@@ -3,15 +3,13 @@ public:
     vector<vector<int> > generate(int numRows) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
-        if (numRows) {
-            for (int i = 0; i < numRows; i++) {
-                vector<int> ret;
-                rets.push_back(ret);
-                rets.back().push_back(1);
-                for (int j = 1; j <= i; j++)
-                    rets.back().push_back(((double)rets.back().back())*(i - j + 1)/j);
-            }
+        vector<vector<int>> rets;
+        if (numRows == 0)
+            return rets;
+        for (int i = 0; i < numRows; i++) {
+            rets.push_back(vector<int>(1, 1));
+            for (int j = 1; j <= i; j++)
+                rets.back().push_back(rets.back().back()*(double)(i-j+1)/j);
         }
         return rets;
     }
diff --git a/Path Sum II.txt b/Path Sum II.txt
index 4365f0e..eb883d0 100644
--- a/Path Sum II.txt	
+++ b/Path Sum II.txt	
@@ -12,43 +12,25 @@ public:
     vector<vector<int> > pathSum(TreeNode *root, int sum) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rs;
-        vector<int> r;
-        if (root == NULL)
-            return rs;
-        else
-            test(root, sum, rs, r);
-        return rs;
-        
-        
+        vector<vector<int>> rets;
+        vector<int> ret;
+        if (root)
+            solve(rets, ret, root, sum);
+        return rets;
     }
-    void test(TreeNode *root, int sum, vector<vector<int> >& rs, vector<int>& r) {
-        if ((root->left == NULL) && (root->right == NULL)) {
-            if (root->val == sum) {
-                r.push_back(root->val);
+    void solve(vector<vector<int>> &rs, vector<int> &r, TreeNode* n, int rem) {
+        if (!n->left && !n->right) {
+            if (rem == n->val) {
                 rs.push_back(r);
-                r.pop_back();
+                rs.back().push_back(n->val);
             }
-        }
-        if (root->left != NULL) {
-            r.push_back(root->val);
-            test(root->left, sum - root->val, rs, r);
-            r.pop_back();
-        }
-        if (root->right != NULL) {
-            r.push_back(root->val);
-            test(root->right, sum - root->val, rs, r);
+        } else {
+            r.push_back(n->val);
+            if (n->left)
+                solve(rs, r, n->left, rem - n->val);
+            if (n->right)
+                solve(rs, r, n->right, rem - n->val);
             r.pop_back();
         }
     }
-};
-
-/**
- * Definition for binary tree
- * struct TreeNode {
- *     int val;
- *     TreeNode *left;
- *     TreeNode *right;
- *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
\ No newline at end of file
+};
\ No newline at end of file
diff --git a/Path Sum.txt b/Path Sum.txt
index 44d9f2f..037d8b4 100644
--- a/Path Sum.txt	
+++ b/Path Sum.txt	
@@ -12,27 +12,13 @@ public:
     bool hasPathSum(TreeNode *root, int sum) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (root == NULL)
-            return false;
-        else
-            return test(root, sum);
-        
+        return root?solve(root, sum):false;
     }
-    bool test(TreeNode *root, int sum) {
-        if ((root->left == NULL) && (root->right == NULL)) {
-            if (root->val == sum)
-                return true;
-            else
-                return false;
-        }
-        bool l = false, r = false;
-        if (root->left != NULL)
-            l = test(root->left, sum - root->val);
-        if (root->right != NULL)
-            r = test(root->right, sum - root->val);
-        if (l || r)
+    bool solve(TreeNode* r, int s) {
+        if (!r->left && !r->right)
+            return (s == r->val);
+        if (r->left && solve(r->left, s - r->val))
             return true;
-        else
-            return false;
+        return (r->right && solve(r->right, s - r->val));
     }
 };
\ No newline at end of file
diff --git a/Permutation Sequence.txt b/Permutation Sequence.txt
index 88afe76..8671e51 100644
--- a/Permutation Sequence.txt	
+++ b/Permutation Sequence.txt	
@@ -3,19 +3,18 @@ public:
     string getPermutation(int n, int k) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int fac[n], d;
-        for (int i = 0; i < n; i++)
-            fac[i] = (!i)?1:fac[i-1]*i;
-        string ret = "";
-        vector<int> nums;
-        for (int i = 0; i < n; i++)
-            nums.push_back(i+1);
+        vector<int> plan(n+1, 1), nums;
+        string ret;
+        for (int i = 1; i <= n; i++) {
+            plan[i] = plan[i-1]*i;
+            nums.push_back(i);
+        }
         k--;
-        for (int i = n-1; i >= 0; i--) {
-            d = k/fac[i];
-            ret += (nums[d] + '0');
+        for (int i = n - 1; i >= 0; i--) {
+            int d = k/plan[i];
+            ret = ret + (char)('0' + nums[d]);
             nums.erase(nums.begin() + d);
-            k = k%fac[i];
+            k = k%plan[i];
         }
         return ret;
     }
diff --git a/Permutations II.txt b/Permutations II.txt
index d8589cb..4d66baa 100644
--- a/Permutations II.txt	
+++ b/Permutations II.txt	
@@ -4,33 +4,26 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         vector<vector<int> > rets;
-        if (num.size() == 0)
-            return rets;
-        sort(num.begin(), num.end());
-        vector<int> use(num.size(), 0);
         vector<int> ret;
-        plan(rets, ret, num, use, 0);
+        sort(num.begin(), num.end());
+        vector<bool> use(num.size(), false);
+        solve(rets, ret, num, use, 0);
         return rets;
     }
-    void plan(vector<vector<int> >& rs, vector<int>& r, vector<int>& n, vector<int>& u, int d) {
-        if (d == n.size()) {
+    void solve(vector<vector<int>> &rs, vector<int> &r, vector<int> &n, vector<bool> &u, int p) {
+        if (p == n.size())
             rs.push_back(r);
-            return;
-        }
-        int last = -1;
-        for (int i = 0; i < n.size(); i++) {
-            if (u[i])
-                continue;
-            else if ((last >= 0) && (n[last] == n[i]))
-                continue;
-            else {
-                last = i;
-                r.push_back(n[i]);
-                u[i] = 1;
-                plan(rs, r, n, u, d+1);
-                u[i] = 0;
-                r.pop_back();
-            }
+        else if (p < n.size()){
+            int last = -1;
+            for (int i = 0; i < n.size(); i++)
+                if (!u[i] && ((last == -1) || (n[i] != n[last]))) {
+                        r.push_back(n[i]);
+                        u[i] = true;
+                        solve(rs, r, n, u, p + 1);
+                        u[i] = false;
+                        r.pop_back();
+                        last = i;
+                }
         }
     }
 };
\ No newline at end of file
diff --git a/Permutations.txt b/Permutations.txt
index c7a6e2a..7ec226a 100644
--- a/Permutations.txt
+++ b/Permutations.txt
@@ -2,26 +2,23 @@ class Solution {
 public:
     vector<vector<int> > permute(vector<int> &num) {
         // Start typing your C/C++ solution below
-        // DO NOT write int main() function   
+        // DO NOT write int main() function
         vector<vector<int> > rets;
-        if (num.size() == 0)
-            return rets;
-        plan(rets, num, 0);
+        solve(rets, num, 0);
         return rets;
     }
-    void plan(vector<vector<int> >& rets, vector<int>& ret, int p) {
-        if (p == ret.size() - 1) {
-            rets.push_back(ret);
-            return;
-        }
-        plan(rets, ret, p+1);
-        for (int i = p + 1; i < ret.size(); i++) {
-            swap(ret[i], ret[p]);
-            plan(rets, ret, p+1);
-            swap(ret[i], ret[p]);
+    void solve(vector<vector<int>> &rs, vector<int> &r, int p) {
+        if (p == r.size())
+            rs.push_back(r);
+        else {
+            for (int i = p; i < r.size(); i++) {
+                swap(r[i], r[p]);
+                solve(rs, r, p + 1);
+                swap(r[i], r[p]);
+            }
         }
     }
-    void swap(int& a, int& b) {
+    void swap(int &a, int &b) {
         int t = a;
         a = b;
         b = t;
diff --git a/Plus One.txt b/Plus One.txt
index ed93815..ee64b2b 100644
--- a/Plus One.txt	
+++ b/Plus One.txt	
@@ -3,11 +3,12 @@ public:
     vector<int> plusOne(vector<int> &digits) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int i;
-        for (i = digits.size() - 1; i >= 0; i--) {
-            if (++digits[i] >= 10)
-                digits[i] %= 10;
-            else
+        if (digits.empty())
+            return vector<int>(0);
+        int i = digits.size() - 1;
+        for (; i >= 0; i--) {
+            digits[i] = (digits[i]+1)%10;
+            if (digits[i] != 0)
                 break;
         }
         if (i == -1)
diff --git a/Populatig Next Right Pointers in Each Node.txt b/Populatig Next Right Pointers in Each Node.txt
index bc8a249..d546f27 100644
--- a/Populatig Next Right Pointers in Each Node.txt	
+++ b/Populatig Next Right Pointers in Each Node.txt	
@@ -11,27 +11,27 @@ public:
     void connect(TreeLinkNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (root == NULL)
+        if (!root)
             return;
-        else {
-            linklr(root);
-            while (root != NULL) {
-                root->next = NULL;
-                root = root->right;
-            }
+        connect(root->left);
+        connect(root->right);
+        TreeLinkNode *l = root->left, *r = root->right;
+        while (l) {
+            TreeLinkNode* lp = l, *rp = r;
+            l = leftMost(l);
+            r = leftMost(r);
+            while (lp->next)
+                lp = lp->next;
+            lp->next = rp;
         }
     }
-    void linklr(TreeLinkNode* root) {
-        if (root == NULL)
-            return;
-        TreeLinkNode* l = root->left;
-        TreeLinkNode* r = root->right;
-        while ((l != NULL) && (r != NULL)) {
-            l->next = r;
-            l = l->right;
-            r = r->left;
-        }
-        linklr(root->left);
-        linklr(root->right);
+    TreeLinkNode* leftMost(TreeLinkNode* root) {
+        if (!root)
+            return NULL;
+        if (root->left)
+            return root->left;
+        if (root->right)
+            return root->right;
+        return leftMost(root->next);
     }
 };
\ No newline at end of file
diff --git a/Populating Next Right Pointers in Each Node II.txt b/Populating Next Right Pointers in Each Node II.txt
index 2b94a86..d546f27 100644
--- a/Populating Next Right Pointers in Each Node II.txt	
+++ b/Populating Next Right Pointers in Each Node II.txt	
@@ -11,30 +11,27 @@ public:
     void connect(TreeLinkNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (root == NULL)
+        if (!root)
             return;
         connect(root->left);
         connect(root->right);
-        TreeLinkNode* l = root->left;
-        TreeLinkNode* r = root->right;
-        while (l != NULL) {
-            TreeLinkNode* p = l;
-            while (p->next != NULL)
-                p = p->next;
-            TreeLinkNode* rt = r;
-            l = getLMost(l);
-            r = getLMost(r);
-            p->next = rt;
+        TreeLinkNode *l = root->left, *r = root->right;
+        while (l) {
+            TreeLinkNode* lp = l, *rp = r;
+            l = leftMost(l);
+            r = leftMost(r);
+            while (lp->next)
+                lp = lp->next;
+            lp->next = rp;
         }
     }
-    TreeLinkNode* getLMost(TreeLinkNode* root) {
-        if (root == NULL)
+    TreeLinkNode* leftMost(TreeLinkNode* root) {
+        if (!root)
             return NULL;
-        if (root->left != NULL)
+        if (root->left)
             return root->left;
-        if (root->right != NULL)
+        if (root->right)
             return root->right;
-        else
-            return getLMost(root->next);
+        return leftMost(root->next);
     }
 };
\ No newline at end of file
diff --git a/Pow(x,n).txt b/Pow(x,n).txt
index bf53783..e83aa4c 100644
--- a/Pow(x,n).txt
+++ b/Pow(x,n).txt
@@ -3,20 +3,14 @@ public:
     double pow(double x, int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
+        bool pos = !(n < 0);
+        unsigned p = pos?n:-(double)n;
         double ret = 1;
-        unsigned pow;
-        bool neg = false;
-        if (n < 0) {
-            neg = true;
-            pow = (unsigned)((double)-1 * (double)n);
-        }
-        else
-            pow = n;
-        while (pow > 0) {
-            ret = (pow & 1)?ret*x:ret;
+        while (p > 0) {
+            ret = (p & 1)?x*ret:ret;
             x *= x;
-            pow /= 2;
+            p >>= 1;
         }
-        return (neg)?1/ret:ret;
+        return pos?ret:1/ret;   
     }
 };
\ No newline at end of file
diff --git a/Recover Binary Search Tree.txt b/Recover Binary Search Tree.txt
index 2e53330..3a50326 100644
--- a/Recover Binary Search Tree.txt	
+++ b/Recover Binary Search Tree.txt	
@@ -9,35 +9,35 @@
  */
 class Solution {
 public:
-    TreeNode *last, *t1, *t2;
     void recoverTree(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        last = NULL, t1 = NULL;
-        findFirst(root);
-        last = NULL, t2 = NULL;
-        findSecond(root);
-        int temp = t1->val;
-        t1->val = t2->val;
-        t2->val = temp;
-        
+        TreeNode *prev = NULL, *succ = NULL;
+        TreeNode *l = findFirst(root, prev), *r = findLast(root, succ);
+        int t = l->val;
+        l->val = r->val;
+        r->val = t;
     }
-    void findFirst(TreeNode* root) {
-        if (root == NULL)
-            return;
-        findFirst(root->left);
-        if (last != NULL)
-            t1 = (root->val < last->val)?root:t1;
-        last = root;
-        findFirst(root->right);
+    TreeNode* findFirst(TreeNode* root, TreeNode*& prev) {
+        if (!root)
+            return NULL;
+        TreeNode* l = findFirst(root->left, prev);
+        if (l)
+            return l;
+        if (prev && (prev->val > root->val))
+            return prev;
+        prev = root;
+        return findFirst(root->right, prev);
     }
-    void findSecond(TreeNode* root) {
-        if (root == NULL)
-            return;
-        findSecond(root->right);
-        if (last != NULL)
-            t2 = (root->val > last->val)?root:t2;
-        last = root;
-        findSecond(root->left);
+    TreeNode* findLast(TreeNode* root, TreeNode*& succ) {
+        if (!root)
+            return NULL;
+        TreeNode* r = findLast(root->right, succ);
+        if (r)
+            return r;
+        if (succ && (root->val > succ->val))
+            return succ;
+        succ = root;
+        return findLast(root->left, succ);
     }
 };
\ No newline at end of file
diff --git a/Regular Expression Matching.txt b/Regular Expression Matching.txt
index 1adc0f2..5da285d 100644
--- a/Regular Expression Matching.txt	
+++ b/Regular Expression Matching.txt	
@@ -2,24 +2,22 @@ class Solution {
 public:
     bool isMatch(const char *s, const char *p) {
         // Start typing your C/C++ solution below
-        // DO NOT write int main() function 
-        if (!p)
-            return (!s)?true:false;
+        // DO NOT write int main() function
         if (!s)
-            return (!p)?true:false;
+            return !p;
+        if (!p)
+            return !s;
         if (*p == 0)
-            return (*s == 0);
+            return *s == 0;
         if (*(p+1) != '*') {
-            if (((*p == *s) || ((*p == '.') && (*s != 0))) && (isMatch(s+1, p+1)))
-                return true;
-        }
-        else {
-            if (isMatch(s, p+2))
+            if ((*s == *p) || ((*p == '.') && (*s != 0)))
+                return isMatch(s+1, p+1);
+        } else {
+            if (isMatch(s, p + 2))
                 return true;
-            while ((*p == *s) || ((*p == '.') && (*s != 0))) {
+            while ((*s == *p) || ((*p == '.') && (*s != 0)))
                 if (isMatch(++s, p+2))
                     return true;
-            }
         }
         return false;
     }
diff --git a/Remove Duplicates form Sorted Array II.txt b/Remove Duplicates form Sorted Array II.txt
index 2ea600b..446d7af 100644
--- a/Remove Duplicates form Sorted Array II.txt	
+++ b/Remove Duplicates form Sorted Array II.txt	
@@ -3,12 +3,10 @@ public:
     int removeDuplicates(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n == 0 || n == 1)
-            return n;
-        int e = 1;
-        for (int i = 2; i < n; i++)
-            if (A[i] != A[e-1])
-                A[++e] = A[i];
-        return e+1;
+        int p = 0;
+        for (int i = 0; i < n; i++)
+            if ((i < 2) || (A[i] != A[p-2]))
+                A[p++] = A[i];
+        return p;
     }
 };
\ No newline at end of file
diff --git a/Remove Duplicates from Sorted Array.txt b/Remove Duplicates from Sorted Array.txt
index 17ea6b1..fe8846f 100644
--- a/Remove Duplicates from Sorted Array.txt	
+++ b/Remove Duplicates from Sorted Array.txt	
@@ -3,12 +3,10 @@ public:
     int removeDuplicates(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n == 0)
-            return 0;
-        int e = 0;
-        for (int i = 1; i < n; i++)
-            if (A[i] != A[e])
-                A[++e] = A[i];
-        return e+1;
+        int p = 0;
+        for (int i = 0; i < n; i++)
+            if ((i == 0) || (A[i] != A[p-1]))
+                A[p++] = A[i];
+        return p;
     }
 };
\ No newline at end of file
diff --git a/Remove Duplicates from Sorted List II.txt b/Remove Duplicates from Sorted List II.txt
index d516061..374dabc 100644
--- a/Remove Duplicates from Sorted List II.txt	
+++ b/Remove Duplicates from Sorted List II.txt	
@@ -11,28 +11,26 @@ public:
     ListNode *deleteDuplicates(ListNode *head) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (head == NULL)
-            return head;
-        ListNode* root = new ListNode(0);
-        ListNode* tail = root;
-        while (head != NULL) {
-            ListNode* temp = head->next;
-            while ((temp != NULL) && (temp->val == head->val))
+        ListNode *root = new ListNode(0), *tail = root, *temp, *dele;
+        while (head) {
+            temp = head->next;
+            while (temp && (temp->val == head->val))
                 temp = temp->next;
             if (temp == head->next) {
                 tail->next = head;
                 tail = tail->next;
-                tail->next = NULL;
                 head = temp;
-            }
-            while (head != temp) {
-                ListNode* t = head;
-                head = head->next;
-                delete t;
+            } else {
+                while (head != temp) {
+                    dele = head;
+                    head = head->next;
+                    delete dele;
+                }
             }
         }
+        tail->next = NULL;
         head = root->next;
         delete root;
-        return head;    
+        return head;
     }
 };
\ No newline at end of file
diff --git a/Remove Duplicates from Sorted List.txt b/Remove Duplicates from Sorted List.txt
index d00c46a..e23772c 100644
--- a/Remove Duplicates from Sorted List.txt	
+++ b/Remove Duplicates from Sorted List.txt	
@@ -11,19 +11,14 @@ public:
     ListNode *deleteDuplicates(ListNode *head) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        ListNode* temp = head;
-        while (temp) {
-            if (temp->next == NULL)
-                break;
-            ListNode* tail = temp->next;
-            while (tail && (tail->val == temp->val))
+        ListNode *tail = head, *temp;
+        while (tail) {
+            if (tail->next && (tail->val == tail->next->val)) {
+                temp = tail->next;
+                tail->next = temp->next;
+                delete temp;
+            } else
                 tail = tail->next;
-            while (temp->next != tail) {
-                ListNode* dele = temp->next;
-                temp->next = dele->next;
-                delete dele;
-            }
-            temp = tail;
         }
         return head;
     }
diff --git a/Remove Element.txt b/Remove Element.txt
index 0e9abf0..4f0a48a 100644
--- a/Remove Element.txt	
+++ b/Remove Element.txt	
@@ -3,13 +3,10 @@ public:
     int removeElement(int A[], int n, int elem) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int newn = 0;
-        for (int i = 0; i < n; i++) {
-            if (A[i] == elem)
-                continue;
-            else
-                A[newn++]=A[i];
-        }
-        return newn;
+        int p = 0;
+        for (int i = 0; i < n; i++)
+            if (A[i] != elem)
+                A[p++] = A[i];
+        return p;
     }
 };
\ No newline at end of file
diff --git a/Restore IP Addresses.txt b/Restore IP Addresses.txt
index a636301..c646c2b 100644
--- a/Restore IP Addresses.txt	
+++ b/Restore IP Addresses.txt	
@@ -3,32 +3,30 @@ public:
     vector<string> restoreIpAddresses(string s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<string> rs;
-        if ((s.size() < 4) || (s.size() > 12))
-            return rs;
-        plan(rs, s, "", 0, 0);
-        return rs;
-        
+        vector<string> rets;
+        if ((s.size() >= 4) || (s.size() <= 12))
+            plan(rets, "", s, 0, 0);
+        return rets;
     }
-    void plan(vector<string>& rs, string& s, string r, int b, int d) {
-        if ((d == 4) && (b == s.size()))
-            rs.push_back(r.substr(0, r.size() - 1));
-        for (int i = 1; i <= 3; i++)
-            if (((b + i) <= s.size()) && valid(s, b, i))
-                plan(rs, s, r + s.substr(b, i) + ".", b + i, d + 1);
+    void plan(vector<string> &rs, string r, string& s, int p, int d) {
+        if (d == 4) {
+            if (p == s.size())
+                rs.push_back(r.substr(0, r.size()-1));
+        } else if (p < s.size()) {
+            vector<string> opt = parse(s, p);
+            for (int i = 0; i < opt.size(); i++)
+                plan(rs, r + opt[i] + ".", s, p + opt[i].size(), d+1);
+        }
     }
-    bool valid(string& s, int b, int i) {
-        if (i == 2)
-            return (s[b] != '0')?true:false;
-        else if (i == 3) {
-            if (s[b] == '0')
-                return false;
-            int num = 0;
-            for (int j = 0; j < i; j++) 
-                num = num*10 + (s[b+j] -'0');
-            return (num <= 255)? true:false;
+    vector<string> parse(string& s, int p) {
+        vector<string> rets(1, s.substr(p, 1));
+        if ((p + 1 < s.size()) && (s[p] != '0'))
+            rets.push_back(s.substr(p, 2));
+        if (p + 2 < s.size()) {
+            int n = (s[p] - '0')*100 + (s[p+1] - '0')*10 + (s[p+2] - '0');
+            if ((n >= 100) && (n <= 255))
+                rets.push_back(s.substr(p, 3));
         }
-        else
-            return true;
+        return rets;
     }
 };
\ No newline at end of file
diff --git a/Reverse Integer.txt b/Reverse Integer.txt
index 7854a15..8695e3e 100644
--- a/Reverse Integer.txt	
+++ b/Reverse Integer.txt	
@@ -4,21 +4,15 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         bool pos = (x >= 0)?true:false;
-        string r = "";
-        while (x != 0) {
-            r += (char)('0' + abs((x%10) - 0));
-            x = x/10;
+        double posx = pos?x:-(double)x, revx = 0;
+        while (posx > 0) {
+            revx = revx*10 + posx - floor(posx/10)*10;
+            posx = floor(posx/10);
         }
-        double d = 0;
-        for (int i = 0; i < r.size(); i++)
-            d = d*10 + (r[i] - '0');
+        revx = pos?revx:-revx;
         if (!pos)
-            d *= -1;
-        if (pos && (d > (double)INT_MAX))
-            return INT_MAX;
-        if (!pos && (d > (double)INT_MIN * -1))
-            return INT_MIN;
+            return (revx < INT_MIN)?INT_MIN:revx;
         else
-            return (int)d;
+            return (revx > INT_MAX)?INT_MAX:revx;
     }
 };
\ No newline at end of file
diff --git a/Reverse Linked List II.txt b/Reverse Linked List II.txt
index d9c0896..16c450d 100644
--- a/Reverse Linked List II.txt	
+++ b/Reverse Linked List II.txt	
@@ -13,17 +13,12 @@ public:
         // DO NOT write int main() function
         ListNode* root = new ListNode(0);
         root->next = head;
-        int num = n - m + 1;
-        ListNode* prev = root;
-        ListNode* tail = root;
-        int move1 = n + 1;
-        int move2 = m - 1;
-        while (move1-- > 0)
-            tail = tail->next;
-        while (move2-- > 0)
-            prev = prev->next;
-        while (num-- > 0) {
-            ListNode* temp = prev->next;
+        ListNode* prev = root, *tail = root;
+        while (m-- > 1) {prev = prev->next; tail = tail->next; n--;}
+        while (n-- > -1) tail = tail->next;
+        ListNode* mark = tail, *temp;
+        while (prev->next != mark) {
+            temp = prev->next;
             prev->next = temp->next;
             temp->next = tail;
             tail = temp;
diff --git a/Reverse Nodes in K-Group.txt b/Reverse Nodes in K-Group.txt
index 4cd5751..b1df1f4 100644
--- a/Reverse Nodes in K-Group.txt	
+++ b/Reverse Nodes in K-Group.txt	
@@ -11,31 +11,26 @@ public:
     ListNode *reverseKGroup(ListNode *head, int k) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        ListNode* root = new ListNode(0);
+        ListNode* root = new ListNode(0), *prev = root, *tail = root;
         root->next = head;
-        ListNode* prev = root;
         while (prev) {
-            int p = k;
-            ListNode* tail = prev;
-            while (tail && p--)
-                tail = tail->next;
-            if (tail == NULL)
+            int kp = k + 1;
+            while (kp && tail) {tail = tail->next; kp--;}
+            if (kp > 0)
                 break;
-            ListNode* newp = prev->next;
-            ListNode* next = tail->next;
-            ListNode* temp = prev->next;
-            tail->next = NULL;
-            while (temp) {
-                prev->next = temp->next;
-                temp->next = next;
-                next = temp;
+            ListNode* mark = tail, *temp, *newp = prev->next;
+            while (prev->next != mark) {
                 temp = prev->next;
+                prev->next = temp->next;
+                temp->next = tail;
+                tail = temp;
             }
-            prev->next = next;
+            prev->next = tail;
             prev = newp;
+            tail = newp;
         }
         head = root->next;
         delete root;
-        return head;  
+        return head;
     }
 };
\ No newline at end of file
diff --git a/Roman to Integer.txt b/Roman to Integer.txt
index 920145b..6ac18a4 100644
--- a/Roman to Integer.txt	
+++ b/Roman to Integer.txt	
@@ -3,22 +3,16 @@ public:
     int romanToInt(string s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int value[255], ret = 0;
-        value['I'] = 1;
-        value['V'] = 5;
-        value['X'] = 10;
-        value['L'] = 50;
-        value['C'] = 100;
-        value['D'] = 500;
-        value['M'] = 1000;
-        int p = 0;
-        while (p < s.size()) {
-            if ((p + 1 < s.size()) && (value[s[p]] < value[s[p+1]]))
-                ret -= value[s[p]];
-            else
-                ret += value[s[p]];
-            p++;
-        }
+        int maps[255], p = 0, ret = 0;
+        maps['I'] = 1;
+        maps['V'] = 5;
+        maps['X'] = 10;
+        maps['L'] = 50;
+        maps['C'] = 100;
+        maps['D'] = 500;
+        maps['M'] = 1000;
+        while (p < s.size())
+            ret += ((p + 1 < s.size()) && (maps[s[p]] < maps[s[p+1]]))?-maps[s[p++]]:maps[s[p++]];
         return ret;
     }
 };
\ No newline at end of file
diff --git a/Rotate List.txt b/Rotate List.txt
index 851a6d9..f906c76 100644
--- a/Rotate List.txt	
+++ b/Rotate List.txt	
@@ -13,25 +13,23 @@ public:
         // DO NOT write int main() function
         if (head == NULL)
             return head;
-        ListNode* tail = head;
-        int num = 1;
+        ListNode *temp = head, *tail = head;
+        int n = 1;
         while (tail->next != NULL) {
-            num++;
             tail = tail->next;
+            n++;
+        }
+        int m = k%n;
+        if (m > 0) {
+            n = n - m - 1;
+            temp = head;
+            while (n--)
+                temp = temp->next;
+            ListNode* newh = temp->next;
+            tail->next = head;
+            temp->next = NULL;
+            head = newh;
         }
-        int mov = k%num;
-        if (mov == 0)
-            return head;
-        ListNode* root = new ListNode(0);
-        ListNode* prev = head;
-        int p = (num - mov) - 1;
-        while (p--)
-            prev = prev->next;
-        root->next = prev->next;
-        tail->next = head;
-        prev->next = NULL;
-        head = root->next;
-        delete root;
         return head;
     }
 };
\ No newline at end of file
diff --git a/Same Tree.txt b/Same Tree.txt
index 91f46b1..908723c 100644
--- a/Same Tree.txt	
+++ b/Same Tree.txt	
@@ -12,15 +12,10 @@ public:
     bool isSameTree(TreeNode *p, TreeNode *q) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (!p)
-            return !q;
-        if (!q)
-            return !p;
-        if (p->val != q->val)
+        if ((p && !q) || (!p && q))
             return false;
-        else if (!isSameTree(p->left, q->left) || !isSameTree(p->right, q->right))
-            return false;
-        else
+        if (!p && !q)
             return true;
+        return ((p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right));  
     }
 };
\ No newline at end of file
diff --git a/Scramble String.txt b/Scramble String.txt
index 2f82cc3..e63b704 100644
--- a/Scramble String.txt	
+++ b/Scramble String.txt	
@@ -3,39 +3,29 @@ public:
     bool isScramble(string s1, string s2) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
+        
         if (s1.size() != s2.size())
             return false;
-        int size = s1.size();
-        bool matrix[size][size][size];
+        int size = s1.size(), plan[size][size][size];
         for (int i = 0; i < size; i++)
             for (int j = 0; j < size; j++)
-                matrix[0][i][j] = (s1[i] == s2[j]);
-        for (int n = 1; n < size; n++) {
-            int len = n + 1;
-            for (int i = 0; i <= size - len; i++)
-                for (int j = 0; j <= size - len; j++) {
-                    matrix[n][i][j] = false;
+                plan[0][i][j] = (s1[i] == s2[j])?true:false;
+        for (int l = 1; l < size; l++) {
+            int len = l + 1;
+            for (int i = 0; i < size - l; i++)
+                for (int j = 0; j < size - l; j++)
                     for (int k = 1; k < len; k++) {
-                        if (matrix[k-1][i][j] && matrix[len-k-1][i+k][j+k]) {
-                            matrix[n][i][j] = true;
+                        plan[l][i][j] = false;
+                        if (plan[k-1][i][j] && plan[len-k-1][i+k][j+k]) {
+                            plan[l][i][j] = true;
                             break;
                         }
-                        if (matrix[len-k-1][i][j] && matrix[k-1][i+len-k][j+len-k]) {
-                            matrix[n][i][j] = true;
+                        if (plan[k-1][i][j+len-k] && plan[len-k-1][i+k][j]) {
+                            plan[l][i][j] = true;
                             break;
                         }
-                        if (matrix[k-1][i][j+len-k] && matrix[len-k-1][i+k][j]) {
-                            matrix[n][i][j] = true;
-                            break;
-                        }
-                        if (matrix[k-1][i+len-k][j] && matrix[len-k-1][i][j+k]) {
-                            matrix[n][i][j] = true;
-                            break;
-                        } 
                     }
-                }
         }
-        return matrix[size-1][0][0];
+        return plan[size-1][0][0];
     }
-    
 };
\ No newline at end of file
diff --git a/Search Insert Position.txt b/Search Insert Position.txt
index 78778ea..57ccc5d 100644
--- a/Search Insert Position.txt	
+++ b/Search Insert Position.txt	
@@ -3,10 +3,16 @@ public:
     int searchInsert(int A[], int n, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        for (int i = 0; i < n; i++)
-            if (A[i] >= target)
-                return i;
-        return n;
-        
+        int l = 0, u = n - 1;
+        while (l <= u) {
+            int mid = (l+u)/2;
+            if (target == A[mid])
+                return mid;
+            else if (target > A[mid])
+                l = mid+1;
+            else
+                u = mid-1;
+        }
+        return u+1;
     }
 };
\ No newline at end of file
diff --git a/Search for a Range.txt b/Search for a Range.txt
index a294c0c..8dd14c1 100644
--- a/Search for a Range.txt	
+++ b/Search for a Range.txt	
@@ -3,49 +3,40 @@ public:
     vector<int> searchRange(int A[], int n, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<int> ret;
-        ret.push_back(-1);
-        ret.push_back(-1);
-        if (n == 0) 
+        vector<int> ret(2, -1);
+        if ((n == 0) || (target < A[0]) || (target > A[n-1]))
             return ret;
-        if (target < A[0])
-            return ret;
-        if (target > A[n-1])
-            return ret;
-        ret[0] = findHead(A, n, target);
-        ret[1] = findTail(A, n, target);
+        ret[0] = findFirst(A, n, target);
+        ret[1] = findLast(A, n, target);
         return ret;
-        
     }
-    int findHead(int A[], int n, int target) {
-        int s = 0;
-        int e = n - 1;
-        while (s <= e) {
-            int p = (s+e)/2;
-            if (A[p] > target)
-                e = p - 1;
-            else if (A[p] < target)
-                s = p + 1;
-            else if ((p == 0) || (A[p-1] < A[p]))
-                return p;
+    int findFirst(int A[], int n, int target) {
+        int l = 0, u = n - 1, mid;
+        while (l <= u) {
+            mid = (l+u)/2;
+            if (target == A[mid]) {
+                if ((mid == 0) || (A[mid-1] != A[mid]))
+                    return mid;
+                u = mid - 1;
+            } else if (target < A[mid])
+                u = mid - 1;
             else
-                e = p - 1;
+                l = mid + 1;
         }
         return -1;
     }
-    int findTail(int A[], int n, int target) {
-        int s = 0;
-        int e = n - 1;
-        while (s <= e) {
-            int p = (s+e)/2;
-            if (A[p] > target)
-                e = p - 1;
-            else if (A[p] < target)
-                s = p + 1;
-            else if ((p == n-1) || (A[p+1] > A[p]))
-                return p;
+    int findLast(int A[], int n, int target) {
+        int l = 0, u = n - 1, mid;
+        while (l <= u) {
+            mid = (l+u)/2;
+            if (target == A[mid]) {
+                if ((mid == n - 1) || (A[mid+1] != A[mid]))
+                    return mid;
+                l = mid + 1;
+            } else if (target < A[mid])
+                u = mid - 1;
             else
-                s = p + 1;
+                l = mid + 1;
         }
         return -1;
     }
diff --git a/Search in Rotated Sorted Array II.txt b/Search in Rotated Sorted Array II.txt
index 292e1fc..f74168a 100644
--- a/Search in Rotated Sorted Array II.txt	
+++ b/Search in Rotated Sorted Array II.txt	
@@ -3,54 +3,28 @@ public:
     bool search(int A[], int n, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n == 0) return false;
-        
-        int l = 0;
-        int u = n-1;
-        bool result = false;
-        while(l <= u)
-        {
-            int mid = l+(u-l)/2;
-            
-            if (A[mid] == target)
+        int l = 0, u = n - 1;
+        while (l <= u) {
+            int mid = (l+u)/2;
+            if (target == A[mid])
                 return true;
-            else if (A[mid] > A[l])
-            {
-                if (A[l] <= target && target < A[mid])
-                {
-                    u = mid - 1;
-                }
+            else if (A[mid] > A[l]) {
+                if ((target >= A[l]) && (target < A[mid]))
+                    u = mid-1;
                 else
-                {
-                    l = mid + 1;
-                }
-            }
-            else if (A[mid] < A[l])
-            {
-                if (A[u] >= target && target > A[mid])
-                {
-                    l = mid + 1;
-                }
+                    l = mid+1;
+            } else if (A[mid] < A[l]) {
+                if ((target > A[mid]) && (target <= A[u]))
+                    l = mid+1;
                 else
-                {
-                    u = mid - 1;
-                }
-            }
-            else
-            {
-                if (A[mid] != A[u])
-                {
-                    l = mid + 1;
-                }
+                    u = mid-1;
+            } else {
+                if (A[l] != A[u])
+                    l = mid+1;
                 else
-                {
-                    result = search(A+l, mid-l,target);
-                    if (!result)
-                        result = search(A+mid+1, u-mid ,target);
-                    break;
-                }
+                    return (search(A+l, mid-l, target) || search(A+mid+1, u - mid,target));
             }
         }
-        return result;
+        return false;
     }
 };
\ No newline at end of file
diff --git a/Set Matrix Zeroes.txt b/Set Matrix Zeroes.txt
index 65d2c34..898cb15 100644
--- a/Set Matrix Zeroes.txt	
+++ b/Set Matrix Zeroes.txt	
@@ -3,51 +3,38 @@ public:
     void setZeroes(vector<vector<int> > &matrix) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int size1 = matrix.size();
-        if (size1 == 0)
+        int nrows = matrix.size();
+        if (nrows == 0)
             return;
-        int size2 = matrix[0].size();
-        if (size2 == 0)
+        int ncols = matrix[0].size();
+        if (ncols == 0)
             return;
-        int row1 = false;
-        int col1 = false;
-        for (int i = 0; i < size2; i++) {
-            if (matrix[0][i] == 0) {
-                row1 = true;
-                break;
-            }
-        }
-        for (int i = 0; i < size1; i++) {
-            if (matrix[i][0] == 0) {
-                col1 = true;
-                break;
-            }
-        }
-        for (int i = 1; i < size1; i++) {
-            for (int j = 1; j < size2; j++) {
-                if (matrix[i][j] == 0) {
+        bool row = true, col = true;
+        for (int i = 0; (i < nrows) && row; i++)
+            if (!matrix[i][0])
+                row = false;
+        for (int j = 0; (j < ncols) && col; j++)
+            if (!matrix[0][j])
+                col = false;
+        for (int i = 1; i < nrows; i++)
+            for (int j = 1; j < ncols; j++)
+                if (!matrix[i][j]) {
                     matrix[i][0] = 0;
                     matrix[0][j] = 0;
                 }
-            }
-        }
-        for (int i = 1; i < size1; i++) {
-            if (matrix[i][0] == 0) {
-                for (int j = 1; j < size2; j++)
-                    matrix[i][j] = 0;
-            }
+        for (int i = 1; i < nrows; i++) {
+            if (!matrix[i][0])
+            for (int j = 1; j < ncols; j++)
+                matrix[i][j] = 0;
         }
-        for (int i = 1; i < size2; i++) {
-            if (matrix[0][i] == 0) {
-                for (int j = 1; j < size1; j++)
-                    matrix[j][i] = 0;
-            }
+        for (int j = 1; j < ncols; j++) {
+            if (!matrix[0][j])
+            for (int i = 1; i < nrows; i++)
+                matrix[i][j] = 0;
         }
-        if (row1)
-            for (int i = 0; i < size2; i++)
-                matrix[0][i] = 0;
-        if (col1)
-            for (int i = 0; i < size1; i++)
+        for (int i = 0; (i < nrows) && !row; i++)
                 matrix[i][0] = 0;
+        for (int j = 0; (j < ncols) && !col; j++)
+                matrix[0][j] = 0;
     }
 };
\ No newline at end of file
diff --git a/Spiral Matrix II.txt b/Spiral Matrix II.txt
index 6635932..5586ca3 100644
--- a/Spiral Matrix II.txt	
+++ b/Spiral Matrix II.txt	
@@ -3,24 +3,20 @@ public:
     vector<vector<int> > generateMatrix(int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
-        for (int i = 0; i < n; i++) {
-            vector<int> ret(n, 0);
-            rets.push_back(ret);
-        }
-        int p = 1, round = n/2;
+        vector<vector<int>> matrix(n, vector<int>(n, 0));
+        int round = n/2, p = 1;
         for (int i = 0; i < round; i++) {
             for (int j = i; j < n - 1 - i; j++)
-                rets[i][j] = p++;
+                matrix[i][j] = p++;
             for (int j = i; j < n - 1 - i; j++)
-                rets[j][n - 1 - i] = p++;
+                matrix[j][n-1-i] = p++;
             for (int j = n - 1 - i; j > i; j--)
-                rets[n - 1 - i][j] = p++;
+                matrix[n-1-i][j] = p++;
             for (int j = n - 1 - i; j > i; j--)
-                rets[j][i] = p++;
+                matrix[j][i] = p++;
         }
         if (n%2)
-            rets[round][round] = p;
-        return rets;
+            matrix[round][round] = p;
+        return matrix;
     }
 };
\ No newline at end of file
diff --git a/Sqrt.txt b/Sqrt.txt
new file mode 100644
index 0000000..21a98fe
--- /dev/null
+++ b/Sqrt.txt
@@ -0,0 +1,13 @@
+class Solution {
+public:
+    int sqrt(int x) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        double nx = x/2.0, ox = x;
+        while (fabs(nx - ox) >= 1e-5) {
+            ox = nx;
+            nx = (ox + x/ox)/2;
+        }
+        return nx;
+    }
+};
\ No newline at end of file
diff --git a/Srounded Regions.txt b/Srounded Regions.txt
new file mode 100644
index 0000000..0f0d941
--- /dev/null
+++ b/Srounded Regions.txt	
@@ -0,0 +1,51 @@
+class Solution {
+public:
+    void solve(vector<vector<char>> &board) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        int nrows = board.size();
+        if (!nrows)
+            return;
+        int ncols = board[0].size();
+        if (!ncols)
+            return;
+        vector<vector<bool>> state(nrows, vector<bool>(ncols, 0));
+        for (int i = 0; i < nrows; i++)
+            for (int j = 0; j < ncols; j++)
+                if ((board[i][j] == 'O') && !state[i][j]) {
+                    vector<int> block;
+                    bool flag = true;
+                    traverse(board, state, block, i, j, flag);
+                    if (flag)
+                        for (int k = 0; k < block.size(); k++)
+                            board[block[k]/board.size()][block[k]%board.size()] = 'X';
+                }
+    }
+    void traverse(vector<vector<char>> &d, vector<vector<bool>> &s, vector<int> &b, int i, int j, bool &flag) {
+        b.push_back(i*d.size() + j);
+        s[i][j] = 1;
+        int p = 0;
+        while (p < b.size()) {
+            int pi = b[p]/d.size(), pj = b[p]%d.size();
+            if ((pi == 0) || (pj == 0) || (pi == d.size() - 1) || (pj == d[0].size() - 1))
+                flag = false;
+            if ((pi + 1 < d.size()) && !s[pi+1][pj] && (d[pi+1][pj] == 'O')) {
+                b.push_back((pi+1)*d.size()+pj);
+                s[pi+1][pj] = 1;
+            }
+            if ((pi - 1 >= 0) && !s[pi-1][pj] && (d[pi-1][pj] == 'O')) {
+                b.push_back((pi-1)*d.size()+pj);
+                s[pi-1][pj] = 1;
+            }
+            if ((pj + 1 < d[0].size()) && !s[pi][pj+1] && (d[pi][pj+1] == 'O')) {
+                b.push_back(pi*d.size()+pj+1);
+                s[pi][pj+1] = 1;
+            }
+            if ((pj - 1 >= 0) && !s[pi][pj-1] && (d[pi][pj-1] == 'O')) {
+                b.push_back(pi*d.size()+pj-1);
+                s[pi][pj-1] = 1;
+            }
+            p++;
+        }
+    }
+};
\ No newline at end of file
diff --git a/Subsets II.txt b/Subsets II.txt
index 7606d00..fa0fc89 100644
--- a/Subsets II.txt	
+++ b/Subsets II.txt	
@@ -4,33 +4,25 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         vector<vector<int> > rets;
-        int size = S.size();
-        if (size == 0)
-            return rets;
-        sort(S.begin(), S.end());
         vector<int> ret;
-        traverse(rets, ret, S, 0);
+        sort(S.begin(), S.end());
+        solve(rets, ret, S, 0);
         return rets;
     }
-    void traverse(vector<vector<int> > &rets, vector<int> &ret, vector<int> &S, int p) {
-        if (p == S.size()) {
-            rets.push_back(ret);
+    void solve(vector<vector<int> > &rs, vector<int> &r, vector<int> &s, int p) {
+        if (p == s.size()) {
+            rs.push_back(r);
             return;
         }
-        else {
-            int e = p + 1, n = 1;
-            while ((e < S.size()) && (S[e] == S[e-1])) {
-                e++;
-                n++;
-            }
-            int r = n;
-            traverse(rets, ret, S, e);
-            while (r--){
-                ret.push_back(S[p]);
-                traverse(rets, ret, S, e);
-            }
-            while (n--)
-                ret.pop_back();
+        int n = p + 1;
+        while ((n < s.size()) && (s[n] == s[p]))
+            n++;
+        solve(rs, r, s, n);
+        for (int i = 1; i <= n - p; i++) {
+            r.push_back(s[p]);
+            solve(rs, r, s, n);
         }
+        for (int i = 1; i <= n - p; i++)
+            r.pop_back();
     }
 };
\ No newline at end of file
diff --git a/Subsets.txt b/Subsets.txt
index 3a8fca2..0e6631d 100644
--- a/Subsets.txt
+++ b/Subsets.txt
@@ -3,20 +3,20 @@ public:
     vector<vector<int> > subsets(vector<int> &S) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<vector<int> > rets;
-        sort(S.begin(), S.end());
-        int size = S.size();
-        if (size == 0)
+        vector<vector<int>> rets;
+        if (S.empty())
             return rets;
-        for (int i = 0; i < pow(2, size); i++) {
-            vector<int> ret;
-            rets.push_back(ret);
-            int p = 0, n = i;
+        sort(S.begin(), S.end());
+        int l = S.size();
+        for (double i = 0; i < pow(2, l); i++) {
+            rets.push_back(vector<int>(0));
+            int p = 0;
+            double n = i;
             while (n > 0) {
-                if (n & 1)
+                if (ceil(n/2) != n/2)
                     rets.back().push_back(S[p]);
+                n = floor(n/2);
                 p++;
-                n >>= 1;
             }
         }
         return rets;
diff --git a/Sudoku Solver.txt b/Sudoku Solver.txt
index e99e428..390f166 100644
--- a/Sudoku Solver.txt	
+++ b/Sudoku Solver.txt	
@@ -98,4 +98,49 @@ public:
             }
         }
     }//end function
+};
+
+class Solution {
+public:
+    void solveSudoku(vector<vector<char> > &board) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        vector<pair<int, int>> queue;
+        int nrows = board.size(), ncols = board.size();
+        for (int i = 0; i < nrows; i++)
+            for (int j = 0; j < ncols; j++)
+                if (board[i][j] == '.')
+                    queue.push_back(make_pair(i, j));
+        solve(board, queue);
+    }
+    void solve(vector<vector<char>>& b, vector<pair<int, int>>& q) {
+        int p = 0;
+        while ((p >= 0) && (p < q.size())) {
+            int i = q[p].first, j = q[p].second;
+            if (b[i][j] == '9') {
+                b[i][j] = '.';
+                p--;
+            } else {
+                b[i][j] = (b[i][j] == '.')?'1':b[i][j]+(char)1;
+                if (check(i, j, b))
+                    p++;
+            }
+        }  
+    }
+    bool check(int& i, int& j, vector<vector<char>>& b) {
+        const int n = b.size();
+        for (int k = 0; k < n; k++)
+            if ((k != i) && (b[k][j] != '.') && (b[k][j] == b[i][j]))
+                return false;
+        vector<char> &row = b[i];
+        for (int k = 0; k < n; k++)
+            if ((k != j) && (row[k] != '.') && (row[k] == b[i][j])) 
+                return false;
+        int r = i/3*3, c = j/3*3;
+        for (int k = r; k < r + 3; k++)
+            for (int l = c; l < c + 3; l++)
+            if (((k != i) || (l != j)) && (b[k][l] != '.') && (b[k][l] == b[i][j]))
+                return false;
+        return true;
+    }
 };
\ No newline at end of file
diff --git a/Sum Root to Leaf Numbers.txt b/Sum Root to Leaf Numbers.txt
new file mode 100644
index 0000000..1be0f60
--- /dev/null
+++ b/Sum Root to Leaf Numbers.txt	
@@ -0,0 +1,30 @@
+/**
+ * Definition for binary tree
+ * struct TreeNode {
+ *     int val;
+ *     TreeNode *left;
+ *     TreeNode *right;
+ *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
+ * };
+ */
+class Solution {
+public:
+    int sumNumbers(TreeNode *root) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        int sum = 0;
+        if (root)
+            solve(root, 0, sum);
+        return sum;
+    }
+    void solve(TreeNode* root, int num, int& sum) {
+        if (!root->left && !root->right) {
+            sum += num*10 + root->val;
+            return;
+        }
+        if (root->left)
+            solve(root->left, num*10 + root->val, sum);
+        if (root->right)
+            solve(root->right, num*10 + root->val, sum);
+    }
+};
\ No newline at end of file
diff --git a/Swap Nodes in Pairs.txt b/Swap Nodes in Pairs.txt
index 42d18e9..2957a1c 100644
--- a/Swap Nodes in Pairs.txt	
+++ b/Swap Nodes in Pairs.txt	
@@ -11,24 +11,23 @@ public:
     ListNode *swapPairs(ListNode *head) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (head == NULL)
-            return head;
-        if (head->next == NULL) {
-            return head;
-        }
-        ListNode *r = NULL, *p = head, *s = head->next;
-        head = s;
-        while ((p != NULL) && (s != NULL)) {
-            ListNode* n = s->next;
-            p->next = n;
-            s->next = p;
-            if (r != NULL)
-                r->next = s;
-            r = p;
-            p = p->next;
-            if (p != NULL)
-                s = p->next;
+        ListNode *root = new ListNode(0), *prev = root, *tail, *temp;
+        root->next = head;
+        while (prev) {
+            tail = prev;
+            int k = 2;
+            while (tail && k--)
+                tail = tail->next;
+            if (!tail)
+                break;
+            temp = prev->next;
+            temp->next = tail->next;
+            tail->next = temp;
+            prev->next = tail;
+            prev = temp;
         }
+        head = root->next;
+        delete root;
         return head;
     }
 };
\ No newline at end of file
diff --git a/Symmetric Tree.txt b/Symmetric Tree.txt
index 9d70207..22053cc 100644
--- a/Symmetric Tree.txt	
+++ b/Symmetric Tree.txt	
@@ -12,25 +12,15 @@ public:
     bool isSymmetric(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (root == NULL)
-            return true;
-        else
-            return sym(root->left, root->right);
+        return (!root)?true:solve(root->left, root->right);
     }
-    bool sym(TreeNode* l, TreeNode* r) {
-        if ((!l) && (!r))
+    bool solve(TreeNode* l, TreeNode* r) {
+        if ((!l && r) || (l && !r))
+            return false;
+        if (!l && !r)
             return true;
-        else if ((!r && l) || (!l && r))
+        if (l->val != r->val)
             return false;
-        else {
-            if (!sym(l->left, r->right) || !sym(l->right, r->left))
-                return false;
-            if (l->val != r->val)
-                return false;
-            else
-                return true;
-        }
-        
+        return solve(l->left, r->right) && solve(l->right, r->left);
     }
-    
 };
\ No newline at end of file
diff --git a/Text Justification.txt b/Text Justification.txt
index ec26c1a..c294403 100644
--- a/Text Justification.txt	
+++ b/Text Justification.txt	
@@ -3,54 +3,35 @@ public:
     vector<string> fullJustify(vector<string> &words, int L) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<string> rs;
-        int size = words.size();
-        if (size == 0) {
-            rs.push_back("");
-            return rs;
-        }
-        if (L <= 0) {
-            rs.push_back("");
-            return rs;
-        }
-        int p = 0;
-        while (p < size) {
-            int num = 0;
-            string r = "";
-            vector<string> b;
-            while ((p < size) && (num + b.size() + words[p].size() <= L)) {
-                b.push_back(words[p++]);
-                num += b.back().size();
-            }
-            if (b.size() == 1) {
-                r += b[0];
-                r += string(L - b[0].size(), ' ');
-                rs.push_back(r);
-            }
-            else {
-                if (p != size) {
-                    int space = L - num;
-                    int width = space/(b.size() - 1);
-                    int spare = space%(b.size() - 1);
-                    r = b[0];
-                    for (int i = 1; i < b.size(); i++) {
-                        r += (i <= spare)?string(width+1, ' '):string(width, ' ');
-                        r += b[i];
-                    }
-                }
-                else {
-                    r = b[0];
-                    for (int i = 1; i < b.size(); i++) {
-                        r += " ";
-                        r += b[i];
-                    }
-                    if (r.size() < L) {
-                        r += string(L-r.size(), ' ');
-                    }
-                }
-                rs.push_back(r);
+        vector<string> rets;
+        int p = 0, l = -1, len = 0, num = 0;
+        while (p < words.size()) {
+            l = (l == -1)?p:l;
+            if (len + words[p].size() + num <= L) {
+                len += words[p++].size();
+                num++;  
+            } else {
+                string ret;
+                if (num > 1) {
+                    int snum = L - len, base = snum/(num-1), full = snum%(num-1);
+                    for (int i = l; i < p - 1; i++)
+                        ret += words[i]+((full-- > 0)?string(base+1, ' '):string(base, ' '));
+                    ret += words[p-1];
+                } else
+                    ret = words[l] + string(L-words[l].size(), ' ');
+                rets.push_back(ret);
+                l = -1;
+                len = 0;
+                num = 0;
             }
         }
-        return rs;
+        if (l != -1) {
+            string ret;
+            for (int i = l; i < words.size(); i++)
+                ret += (i == words.size()-1)?words[i]:words[i] + ' ';
+            ret += string(L-ret.size(), ' ');
+            rets.push_back(ret);
+        }
+        return rets;
     }
 };
\ No newline at end of file
diff --git a/Trapping Rain Water.txt b/Trapping Rain Water.txt
index bcf4e46..475a4db 100644
--- a/Trapping Rain Water.txt	
+++ b/Trapping Rain Water.txt	
@@ -3,19 +3,18 @@ public:
     int trap(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (n <= 2)
+        if (n == 0)
             return 0;
-        int l[n];
-        int r[n];
-        l[0] = 0;
+        int l[n], r[n]; 
+        l[0] = A[0];
         for (int i = 1; i < n; i++)
-            l[i] = (A[i-1] > l[i-1])?A[i-1]:l[i-1];
-        r[n-1] = 0;
+            l[i] = max(A[i], l[i-1]);
+        r[n-1] = A[n-1];
         for (int i = n - 2; i >= 0; i--)
-            r[i] = (A[i+1] > r[i+1])?A[i+1]:r[i+1];
-        int total = 0;
+            r[i] = max(A[i], r[i+1]);
+        int sum = 0;
         for (int i = 0; i < n; i++)
-            total += (min(l[i], r[i]) > A[i])?(min(l[i], r[i]) - A[i]):0;
-        return total;
+            sum += min(l[i], r[i]) - A[i];
+        return sum;
     }
 };
\ No newline at end of file
diff --git a/Triangle.txt b/Triangle.txt
index f25b6a9..459a7e0 100644
--- a/Triangle.txt
+++ b/Triangle.txt
@@ -3,23 +3,24 @@ public:
     int minimumTotal(vector<vector<int> > &triangle) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int row = triangle.size();
-        if (row == 0)
-            return -1;
-        for (int i = 1; i < row; i++) {
+        if (triangle.empty())
+            return 0;
+        vector<int> inner(triangle.size(), 0), outer(triangle.size(), 0);
+        for (int i = 0; i < triangle.size(); i++) {
+            vector<int> &t = triangle[i];
             for (int j = 0; j <= i; j++) {
                 if (j == 0)
-                    triangle[i][j] += triangle[i-1][j];
+                    outer[j] = inner[j] + t[j];
                 else if (j == i)
-                    triangle[i][j] += triangle[i-1][j-1];
+                    outer[j] = inner[j-1] + t[j];
                 else
-                    triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j]);
+                    outer[j] = t[j] + min(inner[j-1], inner[j]);
             }
-            
+            inner = outer;
         }
-        int m = INT_MAX;
-        for (int i = 0; i < triangle.back().size(); i++)
-            m = min(triangle.back()[i], m);
-        return m;
+        int miv = INT_MAX;
+        for (int i = 0; i < triangle.size(); i++)
+            miv = min(miv, inner[i]);
+        return miv;
     }
 };
\ No newline at end of file
diff --git a/Two Sum.txt b/Two Sum.txt
index 72f6ae2..f7a2dab 100644
--- a/Two Sum.txt	
+++ b/Two Sum.txt	
@@ -3,26 +3,23 @@ public:
     vector<int> twoSum(vector<int> &numbers, int target) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        vector<int> ret;
-        ret.push_back(-1);
-        ret.push_back(-1);
-        vector<pair<int, int> > nums;
+        vector<int> ret(2, 1);
+        vector<pair<int, int>> n;
         for (int i = 0; i < numbers.size(); i++)
-            nums.push_back(make_pair<int, int>(numbers[i], i+1));
-        sort(nums.begin(), nums.end());
-        int l = 0, u = nums.size() - 1;
+            n.push_back(make_pair(numbers[i], i));
+        sort(n.begin(), n.end());
+        int l = 0, u = n.size() - 1;
         while (l < u) {
-            int sum = nums[l].first + nums[u].first;
+            int sum = n[l].first + n[u].first;
             if (sum == target) {
-                ret[0] = min(nums[l].second, nums[u].second);
-                ret[1] = max(nums[l].second, nums[u].second);
-                return ret;
-            }
-            else if (sum < target) 
+                ret[0] = min(n[l].second+1, n[u].second+1);
+                ret[1] = max(n[l].second+1, n[u].second+1);
+                break;
+            } else if (sum < target)
                 l++;
             else
                 u--;
         }
-        return ret;   
+        return ret;
     }
 };
\ No newline at end of file
diff --git a/Unique Binary Search Tree II.txt b/Unique Binary Search Tree II.txt
index 431dce7..e82ab91 100644
--- a/Unique Binary Search Tree II.txt	
+++ b/Unique Binary Search Tree II.txt	
@@ -12,25 +12,25 @@ public:
     vector<TreeNode *> generateTrees(int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        return buildTrees(1, n);
+        return solve(1, n);
     }
-    vector<TreeNode*> buildTrees(int s, int e) {
-        vector<TreeNode* > ret;
-        if (s > e) {
-            ret.push_back(NULL);
-            return ret;
+    vector<TreeNode*> solve(int l, int u) {
+        vector<TreeNode*> rets;
+        if (l > u) 
+            rets.push_back(NULL);
+        else {
+            for (int i = l; i <= u; i++) {
+                vector<TreeNode*> lrets = solve(l, i-1);
+                vector<TreeNode*> rrets = solve(i+1, u);
+                for (int m = 0; m < lrets.size(); m++)
+                    for (int n = 0; n < rrets.size(); n++) {
+                        TreeNode* node = new TreeNode(i);
+                        node->left = lrets[m];
+                        node->right = rrets[n];
+                        rets.push_back(node);
+                    }
+            }
         }
-        for (int i = s; i <= e; i++) {
-            vector<TreeNode*> l = buildTrees(s, i-1);
-            vector<TreeNode*> r = buildTrees(i+1, e);
-            for (int k = 0; k < l.size(); k++)
-                for (int j = 0; j < r.size(); j++) {
-                    TreeNode* root = new TreeNode(i);
-                    root->left = l[k];
-                    root->right = r[j];
-                    ret.push_back(root);
-                }
-        }
-        return ret;
+        return rets;
     }
 };
\ No newline at end of file
diff --git a/Unique Binary Search Tree.txt b/Unique Binary Search Tree.txt
index b76bd6b..2040a7d 100644
--- a/Unique Binary Search Tree.txt	
+++ b/Unique Binary Search Tree.txt	
@@ -3,14 +3,12 @@ public:
     int numTrees(int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        int plans[n+1];
-        plans[0] = 1;
-        plans[1] = 1;
-        for (int i = 2; i <= n; i++) {
-            plans[i] = 0;
+        vector<int> plan(n+1, 1);
+        for (int i = 1; i <= n; i++) {
+            plan[i] = 0;
             for (int j = 0; j <= i - 1; j++)
-                plans[i] += plans[j]*plans[i-1-j];
+                plan[i] += plan[j]*plan[i-1-j];
         }
-        return plans[n];
+        return plan[n];
     }
 };
\ No newline at end of file
diff --git a/Unique Path II.txt b/Unique Path II.txt
index bb299cb..5275fea 100644
--- a/Unique Path II.txt	
+++ b/Unique Path II.txt	
@@ -4,30 +4,23 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         int nrows = obstacleGrid.size();
+        if (!nrows)
+            return 0;
         int ncols = obstacleGrid[0].size();
-        if (!nrows || !ncols)
+        if (!ncols)
             return 0;
-        int mat[nrows][ncols];
-        int i = 0;
-        for (; i < ncols; i++) {
-            if (obstacleGrid[0][i])
-                break;
-            mat[0][i] = 1;
-        }
-        for (; i < ncols; i++)
-            mat[0][i] = 0;
-        i = 0;
-        for (; i < nrows; i++) {
-            if (obstacleGrid[i][0])
-                break;
-            mat[i][0] = 1;
-        }
-        for (; i < nrows; i++)
-            mat[i][0] = 0;
-        for (int i = 1; i < nrows; i++) {
-            for (int j = 1; j < ncols; j++)
-                mat[i][j] = (obstacleGrid[i][j])?0:mat[i-1][j] + mat[i][j-1];
-        }
-        return mat[nrows-1][ncols-1];
+        int path[nrows][ncols];
+        for (int i = 0; i < nrows; i++)
+            for (int j = 0; j < ncols; j++) {
+                if (obstacleGrid[i][j] == 1)
+                    path[i][j] = 0;
+                else {
+                    if (!i && !j)
+                        path[i][j] = 1;
+                    else
+                        path[i][j] = (i?path[i-1][j]:0) + (j?path[i][j-1]:0);
+                }
+            }
+        return path[nrows-1][ncols-1];
     }
 };
\ No newline at end of file
diff --git a/Unique Path.txt b/Unique Path.txt
index 517618b..dd3daa7 100644
--- a/Unique Path.txt	
+++ b/Unique Path.txt	
@@ -5,14 +5,14 @@ public:
         // DO NOT write int main() function
         if (!m || !n)
             return 0;
-        if ((m == 1) || (n == 1))
-            return 1;
-        int l = max(m, n), s = min(m, n);
-        double upper = 1, lower = 1;
-        for (int i = l; i <= m+n-2; i++)
-            upper *= i;
-        for (int i = 1; i <= s - 1; i++)
-            lower *= i;
-        return (int)(upper/lower);
+        int path[m][n];
+        for (int i = 0; i < m; i++)
+            for (int j = 0; j < n; j++) {
+                if (!i && !j)
+                    path[i][j] = 1;
+                else
+                    path[i][j] = (i?path[i-1][j]:0) + (j?path[i][j-1]:0);
+            }
+        return path[m-1][n-1];
     }
 };
\ No newline at end of file
diff --git a/Valid Number.txt b/Valid Number.txt
index c4c373e..cf80a1d 100644
--- a/Valid Number.txt	
+++ b/Valid Number.txt	
@@ -1,58 +1,49 @@
 class Solution {
 public:
     bool isNumber(const char *s) {
-    	int len = (int)strlen(s);
-		int left = 0;
-		int right = len - 1;
-		while(' ' == s[left]) ++left;
-		while(' ' == s[right]) --right;
-		for(int i = 0; i < len; ++i)
-		{
-			if('e' == s[i])
-				return isNumber(s, left, i-1) && isInteger(s, i+1, right);
-		}
-		return isNumber(s, left, right);
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        if (!s)
+            return false;
+        int l = 0, u = strlen(s) - 1;
+        while (*(s+l) == ' ')
+            l++;
+        while (*(s+u) == ' ')
+            u--;
+        for (int i = l; i <= u; i++)
+            if (*(s+i) == 'e')
+                return number(s, l, i-1) && integer(s, i+1, u);
+        return number(s, l, u);
     }
-
-	bool isNumber(const char* s, int start, int end)
-	{
-		if(start <= end)
-		{
-			int cur = start;
-			bool hasDot = false;
-			bool hasDigit = false;
-			if('+' == s[cur] || '-' == s[cur])
-				++cur;
-			for(int i = cur; i <= end; ++i)
-			{
-				char ch = s[i];
-				if(ch >= '0' && ch <= '9')
-				{
-					hasDigit = true;
-					continue;
-				}
-				if('.' == ch && !hasDot)
-					hasDot = true;
-				else
-					return false;
-			}
-			return hasDigit;
-		}
-		return false;
-	}
-	bool isInteger(const char* s, int start, int end)
-	{
-		if(start <= end)
-		{
-			bool hasDigit = false;
-			if('+' == s[start] || '-' == s[start])
-				++start;
-			for(int i = start; i <= end; ++i)
-				if(s[i] >= '0' && s[i] <= '9')
-					hasDigit = true;
-				else
-					return false;
-			return hasDigit;
-		}
-	}
+    bool number(const char* p, int l, int u) {
+        bool dot = false, num = false;
+        if ((*(p+l) == '+') || (*(p+l) == '-'))
+            l++;
+        while (l <= u) {
+            if (*(p+l) == '.') {
+                if (dot)
+                    return false;
+                dot = !dot;
+            } else if (isdigit(*(p+l)))
+                num = true;
+            else
+                return false;
+            l++;
+        }
+        return num;
+    }
+    bool integer(const char* p, int l, int u) {
+        bool num = false;
+        if ((*(p+l) == '+') || (*(p+l) == '-'))
+            l++;
+        while (l <= u) {
+            if (isdigit(*(p+l)))
+                num = true;
+            else
+                return false;
+            l++;
+        }
+        return num;
+    }
+    
 };
\ No newline at end of file
diff --git a/Valid Palindrome.txt b/Valid Palindrome.txt
new file mode 100644
index 0000000..ecaad40
--- /dev/null
+++ b/Valid Palindrome.txt	
@@ -0,0 +1,23 @@
+class Solution {
+public:
+    bool isPalindrome(string s) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        int l = 0, u = s.size() - 1;
+        while ((l < u) && !isalnum(s[l]))
+            l++;
+        while ((l < u) && !isalnum(s[u]))
+            u--;
+        while (l < u) {
+            if (tolower(s[l]) != tolower(s[u]))
+                return false;
+            l++;
+            while ((l < u) && !isalnum(s[l]))
+                l++;
+            u--;
+            while ((l < u) && !isalnum(s[u]))
+                u--;
+        }
+        return true;
+    }
+};
\ No newline at end of file
diff --git a/Valid Parentheses.txt b/Valid Parentheses.txt
index 2e27d8e..37ca78c 100644
--- a/Valid Parentheses.txt	
+++ b/Valid Parentheses.txt	
@@ -1,25 +1,31 @@
 class Solution {
 public:
-    bool isPalindrome(string s) {
+    bool isValid(string s) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        if (s == "")
-            return true;
-        int l = 0, u = s.size() - 1;
-        while ((l < u) && !(isalpha(s[l]) || isdigit(s[l])))
-            l++;
-        while ((l < u) && !(isalpha(s[u]) || isdigit(s[u])))
-            u--;
-        while (l < u) {
-            if (tolower(s[l]) != tolower(s[u]))
-                return false;
-            l++;
-            u--;
-            while ((l < u) && !(isalpha(s[l]) || isdigit(s[l])))
-                l++;
-            while ((l < u) && !(isalpha(s[u]) || isdigit(s[u])))
-                u--;
+        vector<char> stack;
+        for (int i = 0; i < s.size(); i++) {
+            if ((s[i] == '(') || (s[i] == '[') || (s[i] == '{'))
+                stack.push_back(s[i]);
+            if (s[i] == ')') {
+                if (stack.empty() || (stack.back() == '[') || (stack.back() == '{'))
+                    return false;
+                else
+                    stack.pop_back();
+            }
+            if (s[i] == ']') {
+                if (stack.empty() || (stack.back() == '(') || (stack.back() == '{'))
+                    return false;
+                else
+                    stack.pop_back();
+            }
+            if (s[i] == '}') {
+                if (stack.empty() || (stack.back() == '(') || (stack.back() == '['))
+                    return false;
+                else
+                    stack.pop_back();
+            }
         }
-        return true;
+        return stack.empty();
     }
 };
\ No newline at end of file
diff --git a/Validate Binary Search Tree.txt b/Validate Binary Search Tree.txt
index 0c6cda0..01e8ec7 100644
--- a/Validate Binary Search Tree.txt	
+++ b/Validate Binary Search Tree.txt	
@@ -12,16 +12,13 @@ public:
     bool isValidBST(TreeNode *root) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        return isValid(root, INT_MIN, INT_MAX);
+        return solve(root, INT_MIN, INT_MAX);
     }
-    bool isValid(TreeNode* root, int l, int r) {
-        if (root == NULL)
+    bool solve(TreeNode* root, int l, int u) {
+        if (!root)
             return true;
-        if ((root->val < l) || (root->val > r))
+        if ((root->val < l) || (root->val > u))
             return false;
-        if (!isValid(root->left, l, root->val - 1) || !isValid(root->right, root->val + 1, r))
-            return false;
-        else
-            return true;
+        return solve(root->left, l, root->val-1) && solve(root->right, root->val+1, u);
     }
 };
\ No newline at end of file
diff --git a/Wildcard Matching.txt b/Wildcard Matching.txt
index 10a3ea9..3abfd0f 100644
--- a/Wildcard Matching.txt	
+++ b/Wildcard Matching.txt	
@@ -3,38 +3,45 @@ public:
     bool isMatch(const char *s, const char *p) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
-        const char *str, *pat;
+        if (!s)
+            return !p;
+        if (!p)
+            return !s;
+        if (!*p)
+            return !*s;
+        const char* str = s, *pat = p;
         bool star = false;
-        
-        for (str = s, pat = p; *str != '\0'; ++str, ++pat){
+        for (;*str ;str++, pat++) {
             switch(*pat) {
-                case '?':
+                case '?': {
                     break;
-                case '*':
+                }
+                case '*': {
                     star = true;
-                    s = str, p = pat;
-                    do {
-                        ++p;
-                    } while(*p == '*');
+                    p = ++pat;
+                    while (*p == '*')
+                        p++;
                     if (!*p)
                         return true;
-                    str = s - 1;
+                    s = str;
                     pat = p - 1;
+                    str = s - 1;
                     break;
-                default:
-                    if (*str != *pat) {
+                } default: {
+                    if (*str == *pat)
+                        break;
+                    else {
                         if (!star)
                             return false;
-                        ++s;
+                        s++;
                         str = s - 1;
                         pat = p - 1;
                     }
-                    break;
+                }
             }
         }
-        
         while (*pat == '*')
-            ++pat;
-        return (!*pat);
+            pat++;
+        return !*pat;
     }
 };
\ No newline at end of file
diff --git a/Word Ladder II.txt b/Word Ladder II.txt
new file mode 100644
index 0000000..1c9e23c
--- /dev/null
+++ b/Word Ladder II.txt	
@@ -0,0 +1,61 @@
+class Solution {
+public:
+    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        vector<vector<string>> rets;
+        unordered_map<string, int> state;
+        unordered_map<string, vector<string>> paths;
+        state.insert(make_pair(start, 0));
+        int len = 1;
+        list<string> inner, outer;
+        inner.push_back(start);
+        while (!inner.empty()) {
+            outer.clear();
+            bool flag = false;
+            while (!inner.empty()) {
+                string s = inner.front();
+                inner.erase(inner.begin());
+                for (int i = 0; i < s.size(); i++) {
+                    string c = s;
+                    for (int j = 0; j < 26; j++) {
+                        c[i] = 'a' + (char)j;
+                        if (c[i] == s[i])
+                            continue;
+                        if (c == end) {
+                            flag = true;
+                            vector<string> ans(1, end);
+                            solve(rets, ans, s, start, paths);
+                        } else {
+                            if (dict.find(c) == dict.end())
+                                continue;
+                            if (state.find(c) == state.end()) {
+                                state.insert(make_pair(c, len));
+                                paths[c].push_back(s);
+                                outer.push_back(c);
+                            } else if (len == state[c])
+                                paths[c].push_back(s);
+                            
+                        }
+                    }
+                }
+            }
+            if (flag)
+                break;
+            len++;
+            inner = outer;
+        }
+        return rets;
+    }
+    void solve(vector<vector<string>> &r, vector<string>& a, string& c, string& s, unordered_map<string, vector<string> > &p) {
+        a.insert(a.begin(), c);
+        if (c == s) {
+            r.push_back(a);
+        } else {
+            vector<string>& v = p[c];
+            for (int i = 0; i < v.size(); i++)
+                solve(r, a, v[i], s, p);
+        }
+        a.erase(a.begin());
+    }
+};
\ No newline at end of file
diff --git a/Word Ladder.txt b/Word Ladder.txt
new file mode 100644
index 0000000..f234cef
--- /dev/null
+++ b/Word Ladder.txt	
@@ -0,0 +1,36 @@
+class Solution {
+public:
+    int ladderLength(string start, string end, unordered_set<string> &dict) {
+        // Start typing your C/C++ solution below
+        // DO NOT write int main() function
+        list<string> inner, outer;
+        unordered_set<string> state;
+        inner.push_back(start);
+        state.insert(start);
+        int len = 2;
+        while (!inner.empty()) {
+            outer.clear();
+            while (!inner.empty()) {
+                string s = inner.front();
+                inner.erase(inner.begin());
+                for (int i = 0; i < s.size(); i++) {
+                    string c = s;
+                    for (int j = 0; j < 26; j++) {
+                        c[i] = 'a' + (char)j;
+                        if (c[i] == s[i])
+                            continue;
+                        if (c == end)
+                            return len;
+                        if ((dict.find(c) == dict.end()) || (state.find(c) != state.end()))
+                            continue;
+                        state.insert(c);
+                        outer.push_back(c);
+                    }
+                }
+            }
+            len++;
+            inner = outer;
+        }
+        return 0;
+    }
+};
\ No newline at end of file
diff --git a/Word Search.txt b/Word Search.txt
index b9554da..facac88 100644
--- a/Word Search.txt	
+++ b/Word Search.txt	
@@ -4,44 +4,47 @@ public:
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         int nrows = board.size();
-        if (nrows == 0)
+        if (!nrows)
             return false;
         int ncols = board[0].size();
-        if (ncols == 0)
+        if (!ncols)
             return false;
-        if (word == "")
+        if (word.empty())
             return true;
-        vector<vector<bool> > state;
+        bool** use = new bool*[nrows];
         for (int i = 0; i < nrows; i++) {
-            vector<bool> temp(ncols, 0);
-            state.push_back(temp);
+            use[i] = new bool[ncols];
+            memset(use[i], 0, sizeof(bool)*ncols);
         }
-        for (int i = 0; i < nrows; i++) {
+        for (int i = 0; i < nrows; i++)
             for (int j = 0; j < ncols; j++)
-                if (traverse(state, word, 0, i, j, board))
+                if (solve(board, i, j, word, 0, use))
                     return true;
-        }
         return false;
     }
-    bool traverse(vector<vector<bool> >& s, string& w, int p, int r, int c, vector<vector<char> > &b) {
-        if ((w[p] != b[r][c]) || s[r][c])
-            return false;
-        else {
-            if (p == w.size() - 1)
+    bool solve(vector<vector<char> > &b, int x, int y, string& w, int p, bool** use) {
+            if (p == w.size())
+                return true;
+            if ((x < 0) || (y < 0) || (x >= b.size()) || (y >= b[0].size()) || use[x][y]|| (w[p] != b[x][y]))
+                return false;
+            use[x][y] = true;
+            if (solve(b, x-1, y, w, p+1, use)) {
+                use[x][y] = false;
                 return true;
-            else {
-                s[r][c] = true;
-                if ((r > 0) && traverse(s, w, p+1, r-1, c, b))
-                    return true;
-                if ((c > 0) && traverse(s, w, p+1, r, c-1, b))
-                    return true;
-                if ((r < s.size() - 1) && traverse(s, w, p+1, r+1, c, b))
-                    return true;
-                if ((c < s[0].size() - 1) && traverse(s, w, p+1, r, c+1, b))
-                    return true;
-                s[r][c] = false;
             }
-        }
-        return false;
+            if (solve(b, x, y-1, w, p+1, use)) {
+                use[x][y] = false;
+                return true;
+            }
+            if (solve(b, x+1, y, w, p+1, use)) {
+                use[x][y] = false;
+                return true;
+            }
+            if (solve(b, x, y+1, w, p+1, use)) {
+                use[x][y] = false;
+                return true;
+            }
+            use[x][y] = false;
+            return false;
     }
 };
\ No newline at end of file
diff --git a/Zigzag Conversion.txt b/Zigzag Conversion.txt
index 13d0a96..19af2c2 100644
--- a/Zigzag Conversion.txt	
+++ b/Zigzag Conversion.txt	
@@ -2,18 +2,17 @@ class Solution {
 public:
     string convert(string s, int nRows) {
         // Start typing your C/C++ solution below
-        // DO NOT write int main() function
-        if (nRows == 1)
+        // DO NOT write int main() function    
+        vector<string> r(nRows, "");
+        if (nRows <= 1)
             return s;
-        string rows[nRows];
-        int zigsize = nRows*2 - 2, p = 0;
-        while (p < s.size()) {
-            rows[nRows - 1 - abs((p%zigsize) - nRows + 1)] += s[p];
-            p++;
+        for (int i = 0; i < s.size(); i++) {
+            int l = i%(2*nRows-2);
+            r[nRows - 1 - abs(nRows-1-l)] += s[i];
         }
         string ret;
         for (int i = 0; i < nRows; i++)
-            ret = ret + rows[i];
+            ret += r[i];
         return ret;
     }
 };
\ No newline at end of file