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]; + } +}; 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 +}; 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; + } +}; diff --git a/README.md b/README.md index 5c3e0a5..83f5b57 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ leetcode ======== -No voice but big deal. +Have fun! -> 150 / 150 +Last updated at Oct. 17th 2014. + +> 153 / 153 diff --git a/Recorder_List.cc b/Reorder_List.cc similarity index 100% rename from Recorder_List.cc rename to Reorder_List.cc diff --git a/Reverse_Words_in_a_String.cc b/Reverse_Words_in_a_String.cc new file mode 100644 index 0000000..dfe10fb --- /dev/null +++ b/Reverse_Words_in_a_String.cc @@ -0,0 +1,43 @@ +//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) { + 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--; + } + } +};