From f9cade74c5907b9023c488c10ff682217c4c1c69 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Fri, 7 Mar 2014 17:40:57 +0800 Subject: [PATCH 01/10] Create Reverse_Words_in_a_String.cc --- Reverse_Words_in_a_String.cc | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Reverse_Words_in_a_String.cc diff --git a/Reverse_Words_in_a_String.cc b/Reverse_Words_in_a_String.cc new file mode 100644 index 0000000..03acb47 --- /dev/null +++ b/Reverse_Words_in_a_String.cc @@ -0,0 +1,42 @@ +//O(1) space solution +class Solution { +public: + void reverseWords(string &s) { + preHandle(s); + int left = 0, right; + for (right = 0; right < s.length(); right++) { + if (s[right] != ' ') + continue; + if (left < right - 1) + reverse(s, left, right - 1); + left = right + 1; + } + if (left < right - 1) + reverse(s, left, right - 1); + reverse(s, 0, right - 1); + return; + } +private: + void preHandle(string &s) { + int slow = 0, fast = 0; + while (slow < s.length()) { + while (fast < s.length() && s[fast] == ' ') + fast++; + if (fast == s.length()) + break; + if (slow != 0 && fast > 0 && s[fast - 1] == ' ') + s[slow++] = ' '; + s[slow++] = s[fast++]; + } + s.resize(slow); + } + void reverse(string &s, int st, int ed) { + char ch; + while (st < ed) { + ch = s[st]; + s[st] = s[ed]; + s[ed] = ch; + st++, ed--; + } + } +}; From d83c2cba26be5b61d7852f4ab15616c5db265ecf Mon Sep 17 00:00:00 2001 From: iphkwan Date: Fri, 7 Mar 2014 17:43:14 +0800 Subject: [PATCH 02/10] Update Reverse_Words_in_a_String.cc --- Reverse_Words_in_a_String.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/Reverse_Words_in_a_String.cc b/Reverse_Words_in_a_String.cc index 03acb47..dfe10fb 100644 --- a/Reverse_Words_in_a_String.cc +++ b/Reverse_Words_in_a_String.cc @@ -1,4 +1,5 @@ //O(1) space solution +// First, normalize the string, then reverse substring and at last reverse the whole string. class Solution { public: void reverseWords(string &s) { From 362879275e61df4c9c2c21662d8f8d675f4924c8 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Fri, 7 Mar 2014 17:45:09 +0800 Subject: [PATCH 03/10] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c3e0a5..412b25d 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,5 @@ leetcode No voice but big deal. -> 150 / 150 +> 151 / 151 +> Last updated at Mar. 7th 2014. From a731a397e92fd41942fcf3b8fbe084a619cbb6bf Mon Sep 17 00:00:00 2001 From: iphkwan Date: Fri, 7 Mar 2014 17:46:02 +0800 Subject: [PATCH 04/10] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 412b25d..687cb6a 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,6 @@ leetcode No voice but big deal. +Last updated at Mar. 7th 2014. + > 151 / 151 -> Last updated at Mar. 7th 2014. From 926d1cb0e5dda37d0af0535d3a9c3543f80638d2 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Sat, 27 Sep 2014 10:44:00 +0800 Subject: [PATCH 05/10] Create Maximum_Product_Subarray.cc DP. In order to deal with the negatives, it needs to mark down the maximum(>0) and minimum result(<0) at the same time. --- Maximum_Product_Subarray.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Maximum_Product_Subarray.cc diff --git a/Maximum_Product_Subarray.cc b/Maximum_Product_Subarray.cc new file mode 100644 index 0000000..f1aa2b9 --- /dev/null +++ b/Maximum_Product_Subarray.cc @@ -0,0 +1,17 @@ +class Solution { +public: + int maxProduct(int A[], int n) { + if (n <= 0) return 0; + int iMaxProductEndByPrePos = A[0], iMaxProductEndByCurrentPos; + int iMinProductEndByPrePos = A[0], iMinProductEndByCurrentPos; + int ret = A[0]; + for (int i = 1; i < n; i++) { + iMaxProductEndByCurrentPos = max(max(iMaxProductEndByPrePos * A[i], iMinProductEndByPrePos * A[i]), A[i]); + iMinProductEndByCurrentPos = min(min(iMaxProductEndByPrePos * A[i], iMinProductEndByPrePos * A[i]), A[i]); + ret = max(ret, iMaxProductEndByCurrentPos); + iMaxProductEndByPrePos = iMaxProductEndByCurrentPos; + iMinProductEndByPrePos = iMinProductEndByCurrentPos; + } + return ret; + } +}; From 1c85298e03a027291aa0144a6ae7df56e421ce64 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Sat, 27 Sep 2014 10:49:07 +0800 Subject: [PATCH 06/10] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 687cb6a..cb54ac9 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ leetcode ======== -No voice but big deal. +Have fun! -Last updated at Mar. 7th 2014. +Last updated at Sep. 27th 2014. -> 151 / 151 +> 152 / 152 From d175c808a179e3ad8c4a70cf3afd1d6a2d037e17 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Mon, 6 Oct 2014 16:25:23 +0800 Subject: [PATCH 07/10] Rename Recorder_List.cc to Reorder_List.cc Fix name's bug via @Jesse Wang --- Recorder_List.cc => Reorder_List.cc | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Recorder_List.cc => Reorder_List.cc (100%) diff --git a/Recorder_List.cc b/Reorder_List.cc similarity index 100% rename from Recorder_List.cc rename to Reorder_List.cc From 99c73479c06083747f7beaf716c5bf25cfb3636f Mon Sep 17 00:00:00 2001 From: iphkwan Date: Mon, 6 Oct 2014 16:25:54 +0800 Subject: [PATCH 08/10] Rename Maximun_Depth_of_Binary_Tree.cc to Maximum_Depth_of_Binary_Tree.cc Fix name's bug via @Jesse Wang --- ...n_Depth_of_Binary_Tree.cc => Maximum_Depth_of_Binary_Tree.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Maximun_Depth_of_Binary_Tree.cc => Maximum_Depth_of_Binary_Tree.cc (95%) diff --git a/Maximun_Depth_of_Binary_Tree.cc b/Maximum_Depth_of_Binary_Tree.cc similarity index 95% rename from Maximun_Depth_of_Binary_Tree.cc rename to Maximum_Depth_of_Binary_Tree.cc index 28c9737..70b3b60 100644 --- a/Maximun_Depth_of_Binary_Tree.cc +++ b/Maximum_Depth_of_Binary_Tree.cc @@ -17,4 +17,4 @@ class Solution { } return max(maxDepth(root->left), maxDepth(root->right)) + 1; } -}; \ No newline at end of file +}; From d9bc0f123c7548020556f2b9399fcd0a0a38f033 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Thu, 16 Oct 2014 23:59:43 +0800 Subject: [PATCH 09/10] Create Find_Minimum_in_Rotated_Sorted_Array.cc For the reason that it assumes no duplicate exists in the array...This problem seems more easier... --- Find_Minimum_in_Rotated_Sorted_Array.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Find_Minimum_in_Rotated_Sorted_Array.cc diff --git a/Find_Minimum_in_Rotated_Sorted_Array.cc b/Find_Minimum_in_Rotated_Sorted_Array.cc new file mode 100644 index 0000000..b2dff88 --- /dev/null +++ b/Find_Minimum_in_Rotated_Sorted_Array.cc @@ -0,0 +1,15 @@ +class Solution { +public: + int findMin(vector &num) { + int L = 0, R = num.size() - 1, M; + if (R < 0) return -1; + while (L < R) { + M = L + (R - L) / 2; + if (num[M] > num[R]) + L = M + 1; + else + R = M; + } + return num[L]; + } +}; From c53c1cef84ee2bb1a0f45e624ed4833bf60741a8 Mon Sep 17 00:00:00 2001 From: iphkwan Date: Fri, 17 Oct 2014 00:02:24 +0800 Subject: [PATCH 10/10] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb54ac9..83f5b57 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,6 @@ leetcode Have fun! -Last updated at Sep. 27th 2014. +Last updated at Oct. 17th 2014. -> 152 / 152 +> 153 / 153