From 37ed5222bf8be56f72dbe6e5234bb20a3621955a Mon Sep 17 00:00:00 2001 From: Jackistang Date: Mon, 1 Apr 2019 14:34:29 +0800 Subject: [PATCH 1/5] Jackistang add Jackis.c --- c.c | 1 + .../0973_K_Closest_Points_to_Origin/Jackis.c | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 c.c create mode 100644 problems/0973_K_Closest_Points_to_Origin/Jackis.c diff --git a/c.c b/c.c new file mode 100644 index 00000000..9d349d40 --- /dev/null +++ b/c.c @@ -0,0 +1 @@ +This file is added by Jackis diff --git a/problems/0973_K_Closest_Points_to_Origin/Jackis.c b/problems/0973_K_Closest_Points_to_Origin/Jackis.c new file mode 100644 index 00000000..cb661696 --- /dev/null +++ b/problems/0973_K_Closest_Points_to_Origin/Jackis.c @@ -0,0 +1,54 @@ +/* +基于选择排序的方法,但该方法提交时显示超时 +*/ + +class Solution { +public: + vector> kClosest(vector>& points, int K) { + vector> res; + SelectSort(points, res, K); + return res; + } + double Distance(vector n) + { + return (double)n[0]*n[0] + (double)n[1]*n[1]; + } + void SelectSort(vector>& num, vector>& res, int K) + { + double min; + int min_pos; + int i, j; + for(i=0; i> kClosest(vector>& points, int K) { + sort(begin(points), end(points), [this](auto& f, auto& s) + { + return (f[0]*f[0] + f[1]*f[1] < s[0]*s[0] + s[1]*s[1]); + }); + + points.erase(begin(points) + K, end(points)); + return points; + } +}; From ff5484a8c0a0c3e1666d6b5db9a236c61a7e25cd Mon Sep 17 00:00:00 2001 From: Jackistang Date: Mon, 1 Apr 2019 14:37:48 +0800 Subject: [PATCH 2/5] Jackistang add Jackis-1.txt --- submittors/Jackis-1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 submittors/Jackis-1.txt diff --git a/submittors/Jackis-1.txt b/submittors/Jackis-1.txt new file mode 100644 index 00000000..df485dbc --- /dev/null +++ b/submittors/Jackis-1.txt @@ -0,0 +1 @@ +This is From 2fd96d8860c184f8f376d08b7fbfd8198c5c60df Mon Sep 17 00:00:00 2001 From: Jackistang Date: Tue, 2 Apr 2019 15:11:16 +0800 Subject: [PATCH 3/5] Jackistang add Jackis.cpp --- .../torresng.cpp | 109 +++++++++ .../torresng.cpp | 33 +-- problems/0006_ZigZag_Conversion/torresng.cpp | 104 ++++++++ .../0008_String_to_Integer_atoi/torresng.cpp | 150 ++++++++++++ .../torresng.cpp | 63 +++++ problems/0012_Integer_to_Roman/torresng.cpp | 223 ++++++++++++++++++ problems/0018_4Sum/wanxu_pursue_0018.java | 158 +++++++++++++ .../wanxu_pursue_0019.java | 89 +++++++ .../wanxu_pursue_0020.java | 87 +++++++ .../wanxu_pursue_0021.java | 76 ++++++ problems/0189_Rotate_Array/torresng.cpp | 105 +++++++++ problems/0213_House_Robber_II/torresng.cpp | 88 +++++++ problems/0520_Detect_Capital/nicefuture.c | 33 +++ .../nicefuture.c | 8 + .../dachuan.java | 26 ++ problems/0561_array-partition-i/dachuan.java | 11 + submittors/22k.txt | 1 - submittors/ALOHA.txt | 1 - submittors/Ashbringer.txt | 1 - submittors/Barlowwang.txt | 1 - submittors/Decadence.txt | 1 - submittors/ELon.py | 1 - submittors/HH.txt | 1 - submittors/Lonecloud.md | 1 - submittors/abelalan.txt | 1 - "submittors/boom\347\261\263\350\212\261.txt" | 1 - submittors/hainingbaby.md | 2 - submittors/jacklynd.c | 1 - submittors/jasonvv.txt | 1 - submittors/{liangxu.c => liangxu-1.c} | 0 submittors/liangxu-2.c | 0 submittors/lionfly.txt | 1 - submittors/merwinlin.txt | 1 - submittors/nian4week | 1 - submittors/nicefuture.txt | 1 - ...5\247\350\226\252\345\260\235\350\203\206" | 1 - "submittors/\345\244\247\350\210\271.txt" | 1 - "submittors/\346\265\201\346\230\237.txt" | 1 - .../\346\265\201\346\230\237\346\263\2523.22" | 1 - "submittors/\347\216\213\350\210\252.txt" | 1 - .../\347\237\263\346\210\220\347\216\211" | 1 - "submittors/\350\277\275\350\265\266.txt" | 11 - "submittors/\351\230\277\346\265\251.c" | 1 - 43 files changed, 1349 insertions(+), 50 deletions(-) create mode 100644 problems/0004_Median_of_Two_Sorted_Arrays/torresng.cpp create mode 100644 problems/0006_ZigZag_Conversion/torresng.cpp create mode 100644 problems/0008_String_to_Integer_atoi/torresng.cpp create mode 100644 problems/0011_Container_With_Most_Water/torresng.cpp create mode 100644 problems/0012_Integer_to_Roman/torresng.cpp create mode 100644 problems/0018_4Sum/wanxu_pursue_0018.java create mode 100644 problems/0019_Remove_Nth_Node_From_End_of_List/wanxu_pursue_0019.java create mode 100644 problems/0020_Valid_Parentheses/wanxu_pursue_0020.java create mode 100644 problems/0021_Merge_Two_Sorted_Lists/wanxu_pursue_0021.java create mode 100644 problems/0189_Rotate_Array/torresng.cpp create mode 100644 problems/0213_House_Robber_II/torresng.cpp create mode 100644 problems/0520_Detect_Capital/nicefuture.c create mode 100644 problems/0521_Longest_Uncommon_Subsequence_I/nicefuture.c create mode 100644 problems/0557_Reverse_Words_in_a_String_III/dachuan.java create mode 100644 problems/0561_array-partition-i/dachuan.java delete mode 100644 submittors/22k.txt delete mode 100644 submittors/ALOHA.txt delete mode 100644 submittors/Ashbringer.txt delete mode 100644 submittors/Barlowwang.txt delete mode 100644 submittors/Decadence.txt delete mode 100644 submittors/ELon.py delete mode 100644 submittors/HH.txt delete mode 100644 submittors/Lonecloud.md delete mode 100644 submittors/abelalan.txt delete mode 100644 "submittors/boom\347\261\263\350\212\261.txt" delete mode 100644 submittors/hainingbaby.md delete mode 100644 submittors/jacklynd.c delete mode 100644 submittors/jasonvv.txt rename submittors/{liangxu.c => liangxu-1.c} (100%) create mode 100644 submittors/liangxu-2.c delete mode 100644 submittors/lionfly.txt delete mode 100644 submittors/merwinlin.txt delete mode 100644 submittors/nian4week delete mode 100644 submittors/nicefuture.txt delete mode 100644 "submittors/\345\215\247\350\226\252\345\260\235\350\203\206" delete mode 100644 "submittors/\345\244\247\350\210\271.txt" delete mode 100644 "submittors/\346\265\201\346\230\237.txt" delete mode 100644 "submittors/\346\265\201\346\230\237\346\263\2523.22" delete mode 100644 "submittors/\347\216\213\350\210\252.txt" delete mode 100644 "submittors/\347\237\263\346\210\220\347\216\211" delete mode 100644 "submittors/\350\277\275\350\265\266.txt" delete mode 100644 "submittors/\351\230\277\346\265\251.c" diff --git a/problems/0004_Median_of_Two_Sorted_Arrays/torresng.cpp b/problems/0004_Median_of_Two_Sorted_Arrays/torresng.cpp new file mode 100644 index 00000000..120684e4 --- /dev/null +++ b/problems/0004_Median_of_Two_Sorted_Arrays/torresng.cpp @@ -0,0 +1,109 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/03/22 + * + ***************************************************************/ +/************************************************************** +There are two sorted arrays nums1 and nums2 of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should +be O(log (m+n)). + +You may assume nums1 and nums2 cannot be both empty. + +Example 1: + + nums1 = [1, 3] + nums2 = [2] + + The median is 2.0 + +Example 2: + + nums1 = [1, 2] + nums2 = [3, 4] + + The median is (2 + 3)/2 = 2.5 +***************************************************************/ + +#include +#include + +using namespace std; + +class Solution { +public: + double findMedianSortedArrays(vector &nums1, vector &nums2) { + int n = nums1.size() + nums2.size(); + int k = (n + 1) / 2; + if (n % 2 == 0) { + double r1 = findKSortedArrays(nums1, 0, nums2, 0, k); + double r2 = findKSortedArrays(nums1, 0, nums2, 0, k + 1); + return (findKSortedArrays(nums1, 0, nums2, 0, k) + + findKSortedArrays(nums1, 0, nums2, 0, k + 1)) / + 2; + } + return findKSortedArrays(nums1, 0, nums2, 0, k); + } + + double findKSortedArrays(vector &nums1, int s1, vector &nums2, + int s2, int k) { + if (nums1.size() - s1 > nums2.size() - s2) { + return findKSortedArrays(nums2, s2, nums1, s1, k); + } else if (nums1.size() == s1) { + return nums2[s2 + k - 1]; + } else if (k == 1) { + return min(nums1[s1], nums2[s2]); + } + + int tmp = k / 2; + int pa = (tmp < (nums1.size() - s1)) ? tmp : (nums1.size() - s1); + int pb = k - pa; + if (nums1[s1 + pa - 1] < nums2[s2 + pb - 1]) { + return findKSortedArrays(nums1, s1 + pa, nums2, s2, k - pa); + } else if (nums2[s2 + pb - 1] < nums1[s1 + pa - 1]) { + return findKSortedArrays(nums1, s1, nums2, s2 + pb, k - pb); + } else { + return nums1[s1 + pa - 1]; + } + } +}; + +void test_case_1() { + Solution s = Solution(); + vector nums1 = {1, 3}; + vector nums2 = {2}; + assert(s.findMedianSortedArrays(nums1, nums2) == 2.0); +} + +void test_case_2() { + Solution s = Solution(); + vector nums1 = {1, 2}; + vector nums2 = {3, 4}; + assert(s.findMedianSortedArrays(nums1, nums2) == 2.5); +} + +void test_case_3() { + Solution s = Solution(); + vector nums1 = {100001}; + vector nums2 = {100000}; + assert(s.findMedianSortedArrays(nums1, nums2) == 100000.5); +} + +void test_case_4() { + Solution s = Solution(); + vector nums1 = {1, 4}; + vector nums2 = {2, 3}; + assert(s.findMedianSortedArrays(nums1, nums2) == 2.5); +} + +int main(void) { + test_case_1(); + test_case_2(); + test_case_3(); + test_case_4(); + + return 0; +} diff --git a/problems/0005_Longest_Palindromic_Substring/torresng.cpp b/problems/0005_Longest_Palindromic_Substring/torresng.cpp index bb301b20..49cd401a 100644 --- a/problems/0005_Longest_Palindromic_Substring/torresng.cpp +++ b/problems/0005_Longest_Palindromic_Substring/torresng.cpp @@ -22,6 +22,7 @@ Example 2: #include #include +#include #include using namespace std; @@ -29,22 +30,26 @@ using namespace std; class Solution { public: string longestPalindrome(string s) { - if(s.empty()) return ""; - int left = 0, right = 0, maxLength = 0; - bool dp[s.size()][s.size()]; - memset(dp, false, s.size()*s.size()*sizeof(bool)); - for(int j = 0; j < s.size(); ++j) { - for(int i = 0; i < j; ++i) { - dp[i][j] = (s[i] == s[j]) && ((j - i) < 2 || dp[i+1][j-1]);; - if(dp[i][j] && ((j - i + 1) > maxLength)) { - left = i; - right = j; - maxLength = j - i + 1; - } + string t = "$#"; + for(auto c : s) { + t += c; + t += '#'; + } + vector p(t.size(), 0); + int mx = 0, id = 0, resLen = 0, resCenter = 0; + for(int i = 1; i < t.size(); i++) { + p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1; + while(t[i + p[i]] == t[i - p[i]]) ++p[i]; + if(mx < i + p[i]) { + mx = i + p[i]; + id = i; + } + if(resLen < p[i]) { + resLen = p[i]; + resCenter = i; } - dp[j][j] = true; } - return s.substr(left, right-left+1); + return s.substr((resCenter - resLen) / 2, resLen - 1); } }; diff --git a/problems/0006_ZigZag_Conversion/torresng.cpp b/problems/0006_ZigZag_Conversion/torresng.cpp new file mode 100644 index 00000000..59017fd1 --- /dev/null +++ b/problems/0006_ZigZag_Conversion/torresng.cpp @@ -0,0 +1,104 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/03/22 + * + ***************************************************************/ +/************************************************************** +The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of +rows like this: (you may want to display this pattern in a fixed font for better +legibility) + + P A H N + A P L S I I G + Y I R + +And then read line by line: "PAHNAPLSIIGYIR" + +Write the code that will take a string and make this conversion given a number +of rows: + + string convert(string s, int numRows); + +Example 1: + + Input: s = "PAYPALISHIRING", numRows = 3 + Output: "PAHNAPLSIIGYIR" + +Example 2: + + Input: s = "PAYPALISHIRING", numRows = 4 + Output: "PINALSIGYAHRPI" + Explanation: + + P I N + A L S I G + Y A H R + P I +***************************************************************/ + +#include +#include +#include +#include +#include + +using namespace std; + +class Solution { +public: + string convert(string s, int numRows) { + if (numRows == 1) + return s; + string mp[numRows]; + int period = numRows * 2 - 2; + for (int i = 0; i < s.size(); i++) { + int pos = i % period; + if (i % period >= numRows) { + pos = period - pos; + } + mp[pos] += s[i]; + } + string res; + for (int i = 0; i < numRows; i++) { + res += mp[i]; + } + return res; + } +}; + +void test_case_1() { + Solution s = Solution(); + assert(s.convert("PAYPALISHIRING", 3) == "PAHNAPLSIIGYIR"); +} + +void test_case_2() { + Solution s = Solution(); + assert(s.convert("PAYPALISHIRING", 4) == "PINALSIGYAHRPI"); +} + +void test_case_3() { + Solution s = Solution(); + assert(s.convert("A", 1) == "A"); +} + +void test_case_4() { + Solution s = Solution(); + assert(s.convert("AB", 1) == "AB"); +} + +void test_case_5() { + Solution s = Solution(); + assert(s.convert("ABC", 1) == "ABC"); +} + +int main(void) { + test_case_1(); + test_case_2(); + test_case_3(); + test_case_4(); + test_case_5(); + + return 0; +} diff --git a/problems/0008_String_to_Integer_atoi/torresng.cpp b/problems/0008_String_to_Integer_atoi/torresng.cpp new file mode 100644 index 00000000..f406a380 --- /dev/null +++ b/problems/0008_String_to_Integer_atoi/torresng.cpp @@ -0,0 +1,150 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/03/23 + * + ***************************************************************/ +/************************************************************** +Implement atoi which converts a string to an integer. + +The function first discards as many whitespace characters as necessary until the +first non-whitespace character is found. Then, starting from this character, +takes an optional initial plus or minus sign followed by as many numerical +digits as possible, and interprets them as a numerical value. + +The string can contain additional characters after those that form the integral +number, which are ignored and have no effect on the behavior of this function. + +If the first sequence of non-whitespace characters in str is not a valid +integral number, or if no such sequence exists because either str is empty or it +contains only whitespace characters, no conversion is performed. + +If no valid conversion could be performed, a zero value is returned. + +Note: + + * Only the space character ' ' is considered as whitespace character. + * Assume we are dealing with an environment which could only store integers +within the 32-bit signed integer range: [?231, 231 ? 1]. If the numerical value +is out of the range of representable values, INT_MAX (231 ? 1) or INT_MIN (?231) +is returned. + +Example 1: + + Input: "42" + Output: 42 + +Example 2: + + Input: " -42" + Output: -42 + Explanation: The first non-whitespace character is '-', which is the minus +sign. Then take as many numerical digits as possible, which gets 42. + +Example 3: + + Input: "4193 with words" + Output: 4193 + Explanation: Conversion stops at digit '3' as the next character is not a +numerical digit. + +Example 4: + + Input: "words and 987" + Output: 0 + Explanation: The first non-whitespace character is 'w', which is not a +numerical digit or a +/- sign. Therefore no valid conversion could be performed. + +Example 5: + + Input: "-91283472332" + Output: -2147483648 + Explanation: The number "-91283472332" is out of the range of a 32-bit +signed integer. Thefore INT_MIN (?231) is returned. +***************************************************************/ + +#include +#include + +using namespace std; + +class Solution { +public: + int myAtoi(string str) { + if (str.empty()) { + return 0; + } + + int sign = 1; + long base = 0, i = 0; + + // skip all the preceding whitespaces + while (i < str.size() && str[i] == ' ') { + ++i; + } + + // check if first char is sign + // if it is, skip one char + if (str[i] == '-') { + sign = -1; + ++i; + } else if (str[i] == '+') { + ++i; + } + + // convert number one by one + // if encounter letter, break + while (i < str.size() && isdigit(str[i])) { + base = 10 * base + (str[i] - '0'); + ++i; + // small optimization to stop processing + // if hitting the limit + if (base > INT_MAX) { + break; + } + } + + // check if base has exceeded maximum + if (base > INT_MAX) { + return (sign == 1) ? INT_MAX : INT_MIN; + } + + return base * sign; + } +}; + +void test_case_1() { + Solution s = Solution(); + assert(s.myAtoi("42") == 42); +} + +void test_case_2() { + Solution s = Solution(); + assert(s.myAtoi(" -42") == -42); +} + +void test_case_3() { + Solution s = Solution(); + assert(s.myAtoi("4193 with words") == 4193); +} + +void test_case_4() { + Solution s = Solution(); + assert(s.myAtoi("words and 987") == 0); +} + +void test_case_5() { + Solution s = Solution(); + assert(s.myAtoi("-91283472332") == -2147483648); +} + +int main(void) { + test_case_1(); + test_case_2(); + test_case_3(); + test_case_4(); + test_case_5(); + + return 0; +} diff --git a/problems/0011_Container_With_Most_Water/torresng.cpp b/problems/0011_Container_With_Most_Water/torresng.cpp new file mode 100644 index 00000000..b75a2963 --- /dev/null +++ b/problems/0011_Container_With_Most_Water/torresng.cpp @@ -0,0 +1,63 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/04/01 + * + ***************************************************************/ +/************************************************************** +Given n non-negative integers a1, a2, ..., an , where each represents a point at +coordinate (i, ai). n vertical lines are drawn such that the two endpoints of +line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis +forms a container, such that the container contains the most water. + +Note: You may not slant the container and n is at least 2. + +![image](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg) + +The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this +case, the max area of water (blue section) the container can contain is 49. + +Example: + + Input: [1,8,6,2,5,4,8,3,7] + Output: 49 +***************************************************************/ + +#include +#include + +using namespace std; + +class Solution { +public: + int maxArea(vector &height) { + int i = 0, j = height.size() - 1; + int res = (j - i) * min(height[i], height[j]); + while (i < j) { + if (height[i] > height[j]) { + j -= 1; + } else { + i += 1; + } + int c = (j - i) * min(height[i], height[j]); + if (c > res) { + res = c; + } + } + return res; + } +}; + +void test_case_1() { + Solution s = Solution(); + vector height{1, 8, 6, 2, 5, 4, 8, 3, 7}; + int ans = 49; + assert(s.maxArea(height) == ans); +} + +int main(void) { + test_case_1(); + + return 0; +} diff --git a/problems/0012_Integer_to_Roman/torresng.cpp b/problems/0012_Integer_to_Roman/torresng.cpp new file mode 100644 index 00000000..4ee21cfa --- /dev/null +++ b/problems/0012_Integer_to_Roman/torresng.cpp @@ -0,0 +1,223 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/04/01 + * + ***************************************************************/ +/************************************************************** +Roman numerals are represented by seven different symbols: I, V, X, L, C, D and +M. + + Symbol Value + I 1 + V 5 + X 10 + L 50 + C 100 + D 500 + M 1000 + +For example, two is written as II in Roman numeral, just two one's added +together. Twelve is written as, XII, which is simply X + II. The number twenty +seven is written as XXVII, which is XX + V + II. + +Roman numerals are usually written largest to smallest from left to right. +However, the numeral for four is not IIII. Instead, the number four is written +as IV. Because the one is before the five we subtract it making four. The same +principle applies to the number nine, which is written as IX. There are six +instances where subtraction is used: + + * I can be placed before V (5) and X (10) to make 4 and 9. + * X can be placed before L (50) and C (100) to make 40 and 90. + * C can be placed before D (500) and M (1000) to make 400 and 900. + +Given an integer, convert it to a roman numeral. Input is guaranteed to be +within the range from 1 to 3999. + +Example 1: + + Input: 3 + Output: "III" + +Example 2: + + Input: 4 + Output: "IV" + +Example 3: + + Input: 9 + Output: "IX" + +Example 4: + + Input: 58 + Output: "LVIII" + Explanation: L = 50, V = 5, III = 3. + +Example 5: + + Input: 1994 + Output: "MCMXCIV" + Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. +***************************************************************/ + +#include +#include + +using namespace std; + +class Solution { +public: + string intToRoman(int num) { + string res; + int n_1000 = num / 1000; + int n_m_1000 = num % 1000; + for (int i = 0; i < n_1000; i++) { + res += 'M'; + } + + int n_100 = n_m_1000 / 100; + int n_m_100 = n_m_1000 % 100; + switch (n_100) { + case 9: + res += "CM"; + break; + case 8: + res += "DCCC"; + break; + case 7: + res += "DCC"; + break; + case 6: + res += "DC"; + break; + case 5: + res += "D"; + break; + case 4: + res += "CD"; + break; + case 3: + res += "CCC"; + break; + case 2: + res += "CC"; + break; + case 1: + res += "C"; + break; + } + + int n_10 = n_m_100 / 10; + int n_m_10 = n_m_100 % 10; + switch (n_10) { + case 9: + res += "XC"; + break; + case 8: + res += "LXXX"; + break; + case 7: + res += "LXX"; + break; + case 6: + res += "LX"; + break; + case 5: + res += "L"; + break; + case 4: + res += "XL"; + break; + case 3: + res += "XXX"; + break; + case 2: + res += "XX"; + break; + case 1: + res += "X"; + break; + }; + + int n_1 = n_m_10; + switch (n_1) { + case 9: + res += "IX"; + break; + case 8: + res += "VIII"; + break; + case 7: + res += "VII"; + break; + case 6: + res += "VI"; + break; + case 5: + res += "V"; + break; + case 4: + res += "IV"; + break; + case 3: + res += "III"; + break; + case 2: + res += "II"; + break; + case 1: + res += "I"; + break; + }; + + return res; + } +}; + +void test_case_1() { + Solution s = Solution(); + int num = 3; + string ans = "III"; + assert(s.intToRoman(num) == ans); +} + +void test_case_2() { + Solution s = Solution(); + int num = 4; + string ans = "IV"; + assert(s.intToRoman(num) == ans); +} + +void test_case_3() { + Solution s = Solution(); + int num = 9; + string ans = "IX"; + assert(s.intToRoman(num) == ans); +} + +void test_case_4() { + Solution s = Solution(); + int num = 58; + string ans = "LVIII"; + assert(s.intToRoman(num) == ans); +} + +void test_case_5() { + Solution s = Solution(); + int num = 1994; + string ans = "MCMXCIV"; + assert(s.intToRoman(num) == ans); +} + +int main(void) { + test_case_1(); + test_case_2(); + test_case_3(); + test_case_4(); + test_case_5(); + + return 0; +} diff --git a/problems/0018_4Sum/wanxu_pursue_0018.java b/problems/0018_4Sum/wanxu_pursue_0018.java new file mode 100644 index 00000000..64463488 --- /dev/null +++ b/problems/0018_4Sum/wanxu_pursue_0018.java @@ -0,0 +1,158 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Author: Json Wan + * Created at: 2019/3/2 13:48 + * Description: + * 18. 4Sum + * Medium + * Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. + * Note: + * The solution set must not contain duplicate quadruplets. + * Example: + *

+ * Given array nums = [1, 0, -1, 0, -2, 2], and target = 0. + * A solution set is: + * [ + * [-1, 0, 0, 1], + * [-2, -1, 1, 2], + * [-2, 0, 0, 2] + * ] + * 思路:与3Sum类似,思路也类似 + * 需要注意的点: + * (1)去重复,循环内变下标,一方面考虑while而不是if,一方面需要时刻注意边界问题,防止越界后访问; + **/ +public class wanxu_pursue_0018 { + + //优化点1:將调用改为了循环; + //优化点2:整个过程只新建了一次ArrayList,一次放四个数,避免了无效的循环添加,利用3层局部变量保存上下文信息,而不是递归再遍历的方式 + public List> bestFourSum(int[] nums, int target) { + List> result = new ArrayList(); + Arrays.sort(nums); + + for(int i=0; i < nums.length - 3; i++) { + if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3] > target) + break; + if(nums[i]+nums[nums.length-1]+nums[nums.length-2]+nums[nums.length-3]0 && nums[i-1]==nums[i]) continue; + + for(int j=i+1; jtarget) + break; + if(nums[i]+nums[j]+nums[nums.length-1]+nums[nums.length-2]i+1 && nums[j]==nums[j-1]) + continue; + + int current = nums[i] + nums[j]; + + int k = j+1; + int l = nums.length-1; + + while(k> fourSum(int[] nums, int target) { + Arrays.sort(nums); + List> result = new ArrayList<>(); + int l = nums.length; + for (int i = 0; i < l - 3; i++) { + //去除重复 + while (i < l - 3&&i != 0 && nums[i] == nums[i - 1]){ + i++; + } + if (i >= l - 3) + break; + List> subResult = threeSum(nums, target - nums[i], i + 1); + for (List list : subResult) { + list.add(nums[i]); + result.add(list); + } + } + return result; + } + + private List> twoSum(int[] nums, int target, int start) { + List> result = new ArrayList<>(); + int l = nums.length; + int i = start, j = l - 1; + while (i < j) { + while (i start && nums[i] == nums[i - 1]) + i++; + while (i=j) + break; + int sum = nums[i] + nums[j]; + if (sum == target) { + result.add(Arrays.asList(nums[i], nums[j])); + i++; + j--; + } else if (sum < target) { + i++; + } else { + j--; + } + } + return result; + } + + private List> threeSum(int[] nums, int target, int start) { + List> result = new ArrayList<>(); + int l = nums.length; + for (int i = start; i < l - 2; i++) { + //去除重复 + while (i < l - 2&&i != start && nums[i] == nums[i - 1]) { + i++; + } + if (i >= l - 2) + break; + List> subResult = twoSum(nums, target - nums[i], i + 1); + for (List list : subResult) { + list = new ArrayList<>(list); + list.add(nums[i]); + result.add(list); + } + } + return result; + } + + + public static void main(String[] args) { + wanxu_pursue_0018 instance = new wanxu_pursue_0018(); + System.out.println(instance.fourSum(new int[]{ + -4,0,-4,2,2,2,-2,-2}, 7)); + System.out.println(instance.fourSum(new int[]{ + 0,0,4,-2,-3,-2,-2,-3}, -1)); + System.out.println(instance.fourSum(new int[]{ + -1, 2, 2, -5, 0, -1, 4}, 3)); + System.out.println(instance.fourSum(new int[]{0, 0, 0, 0}, 0)); + System.out.println(instance.fourSum(new int[]{1, 0, -1, 0, -2, 2}, 0)); + } +} diff --git a/problems/0019_Remove_Nth_Node_From_End_of_List/wanxu_pursue_0019.java b/problems/0019_Remove_Nth_Node_From_End_of_List/wanxu_pursue_0019.java new file mode 100644 index 00000000..35597b09 --- /dev/null +++ b/problems/0019_Remove_Nth_Node_From_End_of_List/wanxu_pursue_0019.java @@ -0,0 +1,89 @@ +/** + * Author: Json Wan + * Created at: 2019/3/5 23:00 + * Description: + * 19. Remove Nth Node From End of List + Medium + Given a linked list, remove the n-th node from the end of list and return its head. + Example: + Given linked list: 1->2->3->4->5, and n = 2. + After removing the second node from the end, the linked list becomes 1->2->3->5. + Note: + Given n will always be valid. + 思路:先求长度,然后顺序删除,需要注意的点: +(1) 循环遍历时必须考虑两端的边界情况,注意是两端,严防空指针异常; + (2)对需要保存的头指针备份。 + **/ +class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +public class wanxu_pursue_0019 { + + //AC,0.9893 + public ListNode removeNthFromEnd(ListNode head, int n) { + if(head==null){ + return null; + } + int l=0; + ListNode pHead=head; + while(pHead!=null){ + l++; + pHead=pHead.next; + } + n=l-n; + if(n<0){ + return head; + }else if(n==0){ + return head.next; + }else{ + pHead=head; + n--; + while(n>0){ + head=head.next; + n--; + } + if(head.next!=null) { + head.next = head.next.next; + }else{ + head.next=null; + } + } + return pHead; + } + + private static void printLinkedList(ListNode root){ + while(root!=null){ + System.out.print(root.val); + root=root.next; + if(root!=null) + System.out.print("-->"); + } + System.out.println(); + } + + public static void main(String[] args) { + wanxu_pursue_0019 instance=new wanxu_pursue_0019(); + ListNode n1=new ListNode(1); + ListNode n2=new ListNode(2); + ListNode n3=new ListNode(3); + ListNode n4=new ListNode(4); + ListNode n5=new ListNode(5); + n1.next=n2; + n2.next=n3; + n3.next=n4; + n4.next=n5; + printLinkedList(n1); + printLinkedList(instance.removeNthFromEnd(n1,5)); + printLinkedList(instance.removeNthFromEnd(n1,4)); + printLinkedList(instance.removeNthFromEnd(n1,3)); + printLinkedList(instance.removeNthFromEnd(n1,2)); + printLinkedList(instance.removeNthFromEnd(n1,1)); + printLinkedList(instance.removeNthFromEnd(n1,0)); + } +} diff --git a/problems/0020_Valid_Parentheses/wanxu_pursue_0020.java b/problems/0020_Valid_Parentheses/wanxu_pursue_0020.java new file mode 100644 index 00000000..1aa7d328 --- /dev/null +++ b/problems/0020_Valid_Parentheses/wanxu_pursue_0020.java @@ -0,0 +1,87 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Author: Json Wan + * Created at: 2019/3/13. + * Description: + * + * ˼·ջ˼뼴ɽҪעǰжϣֻҪеһҲƥϼɽ + */ +public class wanxu_pursue_0020 { + + //1.ԼдݽṹArrayListʹֳɵҪÿ죬Ϊһװnewijɱ + //2.жʹif-elsemapÿ죬ΪʡHashĹ + // '(', ')', '{', '}', '[' and ']' + public boolean bestIsValid(String s) { + char[] stack = new char[s.length()]; + int top = -1; + for(char ch : s.toCharArray()) { + if (ch == '(' || ch == '{' || ch == '[') { + stack[++top] = ch; + } else if (ch == ')') { + if (top >= 0 && stack[top] == '(') { + --top; + } else { + return false; + } + } else if (ch == '}') { + if (top >= 0 && stack[top] == '{') { + --top; + } else { + return false; + } + } else if (ch == ']') { + if (top >= 0 && stack[top] == '[') { + --top; + } else { + return false; + } + } + } + return top == -1; + } + //AC,0.9215 + public boolean isValid(String s) { + int l = s.length(); + if (l % 2 != 0) { + return false; + } + Map map=new HashMap<>(); + map.put(')','('); + map.put(']','['); + map.put('}','{'); + List list=new ArrayList<>(); + for(int i=0;i2->4, 1->3->4 +Output: 1->1->2->3->4->4 + ˼· +1.򵥵鲢mergeɣ +2.Ҫռöռ䣬͵غϲͨеָָʵ֣ + */ +class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + } +} + +public class wanxu_pursue_0021 { + + //㷨ռɼʼһʡ岻ʱ临Ӷ㹻 + //AC,1.00,Nice!!! + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode pHead=new ListNode(0); + ListNode pHeadBak=pHead; + while(l1!=null&&l2!=null){ + if(l1.val"); + } + } + System.out.println(); + } + + public static void main(String[] args) { + ListNode root11=new ListNode(1); + ListNode root12=new ListNode(2); + ListNode root13=new ListNode(4); + root11.next=root12; + root12.next=root13; + ListNode root21=new ListNode(1); + ListNode root22=new ListNode(3); + ListNode root23=new ListNode(4); + root21.next=root22; + root22.next=root23; + wanxu_pursue_0021 instance=new wanxu_pursue_0021(); + instance.printList(root11); + instance.printList(root21); + instance.printList(instance.mergeTwoLists(null,root21)); + instance.printList(instance.mergeTwoLists(root11,null)); + instance.printList(instance.mergeTwoLists(root11,root21)); + } +} diff --git a/problems/0189_Rotate_Array/torresng.cpp b/problems/0189_Rotate_Array/torresng.cpp new file mode 100644 index 00000000..d85ca9d3 --- /dev/null +++ b/problems/0189_Rotate_Array/torresng.cpp @@ -0,0 +1,105 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/03/31 + * + ***************************************************************/ +/************************************************************** +Given an array, rotate the array to the right by k steps, where k is +non-negative. + +Example 1: + + Input: [1,2,3,4,5,6,7] and k = 3 + Output: [5,6,7,1,2,3,4] + Explanation: + rotate 1 steps to the right: [7,1,2,3,4,5,6] + rotate 2 steps to the right: [6,7,1,2,3,4,5] + rotate 3 steps to the right: [5,6,7,1,2,3,4] + +Example 2: + + Input: [-1,-100,3,99] and k = 2 + Output: [3,99,-1,-100] + Explanation: + rotate 1 steps to the right: [99,-1,-100,3] + rotate 2 steps to the right: [3,99,-1,-100] + +Note: + + * Try to come up as many solutions as you can, there are at least 3 different +ways to solve this problem. + * Could you do it in-place with O(1) extra space? + +***************************************************************/ + +#include +#include +#include + +using namespace std; + +class Solution { +public: + void rotate(vector &nums, int k) { + if(nums.size() <= 1) return; + k %= nums.size(); + reverse(nums.begin(), nums.end()); + reverse(nums.begin(), nums.begin() + k); + reverse(nums.begin() + k, nums.end()); + } +}; + +void test_case_1() { + Solution s = Solution(); + vector nums = {1, 2, 3, 4, 5, 6, 7}; + int k = 3; + s.rotate(nums, k); + vector ans = {5, 6, 7, 1, 2, 3, 4}; + for (int i = 0; i < nums.size(); i++) { + assert(nums[i] == ans[i]); + } +} + +void test_case_2() { + Solution s = Solution(); + vector nums = {-1, -100, 3, 99}; + int k = 2; + s.rotate(nums, k); + vector ans = {3, 99, -1, -100}; + for (int i = 0; i < nums.size(); i++) { + assert(nums[i] == ans[i]); + } +} + +void test_case_3() { + Solution s = Solution(); + vector nums = {1,2}; + int k = 3; + s.rotate(nums, k); + vector ans = {2,1}; + for (int i = 0; i < nums.size(); i++) { + assert(nums[i] == ans[i]); + } +} + +void test_case_4() { + Solution s = Solution(); + vector nums = {-1}; + int k = 2; + s.rotate(nums, k); + vector ans = {-1}; + for (int i = 0; i < nums.size(); i++) { + assert(nums[i] == ans[i]); + } +} + +int main(void) { + test_case_1(); + test_case_2(); + test_case_3(); + test_case_4(); + + return 0; +} diff --git a/problems/0213_House_Robber_II/torresng.cpp b/problems/0213_House_Robber_II/torresng.cpp new file mode 100644 index 00000000..e5484d8f --- /dev/null +++ b/problems/0213_House_Robber_II/torresng.cpp @@ -0,0 +1,88 @@ +/*************************************************************** + * Copyright (C) 2019 All rights reserved. + * + * Auth :Torres Ng + * Create Time :2019/04/01 + * + ***************************************************************/ +/************************************************************** +You are a professional robber planning to rob houses along a street. Each house +has a certain amount of money stashed. All houses at this place are arranged in +a circle. That means the first house is the neighbor of the last one. Meanwhile, +adjacent houses have security system connected and it will automatically contact +the police if two adjacent houses were broken into on the same night. + +Given a list of non-negative integers representing the amount of money of each +house, determine the maximum amount of money you can rob tonight without +alerting the police. + +Example 1: + + Input: [2,3,2] + Output: 3 + Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = +2), because they are adjacent houses. + +Example 2: + + Input: [1,2,3,1] + Output: 4 + Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). + Total amount you can rob = 1 + 3 = 4. +***************************************************************/ + +#include +#include + +using namespace std; + +class Solution { +public: + int rob(vector &nums) { + if (nums.size() == 1) + return nums[0]; + return max(robRange(nums, 0, nums.size() - 1), + robRange(nums, 1, nums.size())); + } + + int robRange(vector &nums, int s, int e) { + int s1 = 0, s2 = 0, res = 0; + for (int i = s; i < e; i++) { + if (s1 + nums[i] > s2) { + res = s1 + nums[i]; + } else { + res = s2; + } + s1 = s2; + s2 = res; + } + return res; + } +}; + +void test_case_1() { + Solution s = Solution(); + vector nums = {2, 3, 2}; + int ans = 3; + assert(s.rob(nums) == ans); +} + +void test_case_2() { + Solution s = Solution(); + vector nums = {1, 2, 3, 1}; + int ans = 4; + assert(s.rob(nums) == ans); +} + +void test_case_3() { + Solution s = Solution(); + vector nums = {}; + int ans = 0; + assert(s.rob(nums) == ans); +} + +int main(void) { + test_case_1(); + test_case_2(); + return 0; +} diff --git a/problems/0520_Detect_Capital/nicefuture.c b/problems/0520_Detect_Capital/nicefuture.c new file mode 100644 index 00000000..0301fad9 --- /dev/null +++ b/problems/0520_Detect_Capital/nicefuture.c @@ -0,0 +1,33 @@ +bool isCapital(char c) { + return c >= 'A' && c <= 'Z'; +} +bool allCapital(char *p) { + while (*p != '\0') { + if (!isCapital(*p)) return false; + p++; + } + return true; +} +bool allLower(char *p) { + while (*p != '\0') { + // printf("%c is not capital.\n", *p); + if (isCapital(*p)) return false; + p++; + } + return true; +} + +bool detectCapitalUse(char* word) { + char *p = word; + if (isCapital(*p)) { + if (isCapital(*(++p))) { + // printf("is capital"); + return allCapital(p); //剩余全是大写 + } else { + // printf("not capital."); + return allLower(p); //剩余全是小写 + } + } else { + return allLower(p); //全是小写 + } +} diff --git a/problems/0521_Longest_Uncommon_Subsequence_I/nicefuture.c b/problems/0521_Longest_Uncommon_Subsequence_I/nicefuture.c new file mode 100644 index 00000000..31999c3f --- /dev/null +++ b/problems/0521_Longest_Uncommon_Subsequence_I/nicefuture.c @@ -0,0 +1,8 @@ +int findLUSlength(char* a, char* b) { + if (strcmp(a, b) == 0) { + return -1; + } else { + int len_a = strlen(a), len_b = strlen(b); + return len_a > len_b ? len_a : len_b; + } +} diff --git a/problems/0557_Reverse_Words_in_a_String_III/dachuan.java b/problems/0557_Reverse_Words_in_a_String_III/dachuan.java new file mode 100644 index 00000000..71b61ff1 --- /dev/null +++ b/problems/0557_Reverse_Words_in_a_String_III/dachuan.java @@ -0,0 +1,26 @@ +class Solution { + public String reverseWords(String s) { + StringBuilder sb = new StringBuilder(); + String[] items = s.split(" "); + for (int i = 0; i < items.length; i++) { + if(i == items.length - 1){ + //sb.append(new StringBuilder(items[i]).reverse().toString()); + sb.append(reverseString(items[i])); + }else{ + //sb.append(new StringBuilder(items[i]).reverse().toString() + " "); + sb.append(reverseString(items[i]) + " "); + } + } + return sb.toString(); + } + //发现自己写的 reverse 方法效率更高; + public String reverseString(String str){ + char[] chars = str.toCharArray(); + for (int i = 0; i < chars.length / 2; i++) { + char tmp = chars[i]; + chars[i] = chars[chars.length - 1 - i]; + chars[chars.length - 1 - i] = tmp; + } + return new String(chars, 0, chars.length); + } +} \ No newline at end of file diff --git a/problems/0561_array-partition-i/dachuan.java b/problems/0561_array-partition-i/dachuan.java new file mode 100644 index 00000000..61e7b00e --- /dev/null +++ b/problems/0561_array-partition-i/dachuan.java @@ -0,0 +1,11 @@ +class Solution { + public int arrayPairSum(int[] nums) { + //综合来看,先对数组进行排序,然后从第一个元素起,隔两个元素加一次,这样能得到的和最大; + Arrays.sort(nums); + int sum = 0; + for (int i = 0; i < nums.length; i += 2) { + sum += nums[i]; + } + return sum; + } +} \ No newline at end of file diff --git a/submittors/22k.txt b/submittors/22k.txt deleted file mode 100644 index e51bc2ae..00000000 --- a/submittors/22k.txt +++ /dev/null @@ -1 +0,0 @@ -60c50e5bec558714e7e51517ed3333af5bba3660 \ No newline at end of file diff --git a/submittors/ALOHA.txt b/submittors/ALOHA.txt deleted file mode 100644 index 904f3a81..00000000 --- a/submittors/ALOHA.txt +++ /dev/null @@ -1 +0,0 @@ -8ee80b35f31a6383f2182052b9d9b77d87ac07fd \ No newline at end of file diff --git a/submittors/Ashbringer.txt b/submittors/Ashbringer.txt deleted file mode 100644 index 549d0cdf..00000000 --- a/submittors/Ashbringer.txt +++ /dev/null @@ -1 +0,0 @@ -1584e1d58e81377d3977fd99aa49e853e0305c7e \ No newline at end of file diff --git a/submittors/Barlowwang.txt b/submittors/Barlowwang.txt deleted file mode 100644 index 438302ec..00000000 --- a/submittors/Barlowwang.txt +++ /dev/null @@ -1 +0,0 @@ -2f6ed3241822cc31dea3661e462e128db45c5e27 \ No newline at end of file diff --git a/submittors/Decadence.txt b/submittors/Decadence.txt deleted file mode 100644 index 040cdcc1..00000000 --- a/submittors/Decadence.txt +++ /dev/null @@ -1 +0,0 @@ -CommitID:e70497ea295a17c936a8c3a4b13a0ab4c8796926 diff --git a/submittors/ELon.py b/submittors/ELon.py deleted file mode 100644 index a0f6e31e..00000000 --- a/submittors/ELon.py +++ /dev/null @@ -1 +0,0 @@ -26483af05c42d335255ffdbd8070f9093bf446d9 diff --git a/submittors/HH.txt b/submittors/HH.txt deleted file mode 100644 index c3df73d2..00000000 --- a/submittors/HH.txt +++ /dev/null @@ -1 +0,0 @@ -a072da348c33a0712c7a9f14c95f8a82accac14b \ No newline at end of file diff --git a/submittors/Lonecloud.md b/submittors/Lonecloud.md deleted file mode 100644 index db13c2ea..00000000 --- a/submittors/Lonecloud.md +++ /dev/null @@ -1 +0,0 @@ -4c958570230d378f188e4c86f2514a9748f74b49 diff --git a/submittors/abelalan.txt b/submittors/abelalan.txt deleted file mode 100644 index 4e79fcd6..00000000 --- a/submittors/abelalan.txt +++ /dev/null @@ -1 +0,0 @@ -bc3667b..57281a8 diff --git "a/submittors/boom\347\261\263\350\212\261.txt" "b/submittors/boom\347\261\263\350\212\261.txt" deleted file mode 100644 index 36e30ea1..00000000 --- "a/submittors/boom\347\261\263\350\212\261.txt" +++ /dev/null @@ -1 +0,0 @@ -4a57c40213223fecdd4ec8a2962d54707a46375e \ No newline at end of file diff --git a/submittors/hainingbaby.md b/submittors/hainingbaby.md deleted file mode 100644 index c443d739..00000000 --- a/submittors/hainingbaby.md +++ /dev/null @@ -1,2 +0,0 @@ -commit id: -096e83b9de5684a713341c74e115607f627f3057 diff --git a/submittors/jacklynd.c b/submittors/jacklynd.c deleted file mode 100644 index 2407754d..00000000 --- a/submittors/jacklynd.c +++ /dev/null @@ -1 +0,0 @@ -b365122 diff --git a/submittors/jasonvv.txt b/submittors/jasonvv.txt deleted file mode 100644 index 9c43de62..00000000 --- a/submittors/jasonvv.txt +++ /dev/null @@ -1 +0,0 @@ -c72966a4e9cafe2b162b6be947d4039f25d72b11 diff --git a/submittors/liangxu.c b/submittors/liangxu-1.c similarity index 100% rename from submittors/liangxu.c rename to submittors/liangxu-1.c diff --git a/submittors/liangxu-2.c b/submittors/liangxu-2.c new file mode 100644 index 00000000..e69de29b diff --git a/submittors/lionfly.txt b/submittors/lionfly.txt deleted file mode 100644 index b5baebab..00000000 --- a/submittors/lionfly.txt +++ /dev/null @@ -1 +0,0 @@ -2f607b3d43b159fe7351de4c9535b50651eeb603 diff --git a/submittors/merwinlin.txt b/submittors/merwinlin.txt deleted file mode 100644 index 6251ad89..00000000 --- a/submittors/merwinlin.txt +++ /dev/null @@ -1 +0,0 @@ -68a8dfa2a5fce5d47bab6ecaade6f5a4075788fa diff --git a/submittors/nian4week b/submittors/nian4week deleted file mode 100644 index 8b137891..00000000 --- a/submittors/nian4week +++ /dev/null @@ -1 +0,0 @@ - diff --git a/submittors/nicefuture.txt b/submittors/nicefuture.txt deleted file mode 100644 index 43a5e12d..00000000 --- a/submittors/nicefuture.txt +++ /dev/null @@ -1 +0,0 @@ -c201dfacbdc64d3b9978b6c15156df654fd1c79f diff --git "a/submittors/\345\215\247\350\226\252\345\260\235\350\203\206" "b/submittors/\345\215\247\350\226\252\345\260\235\350\203\206" deleted file mode 100644 index d0f002d8..00000000 --- "a/submittors/\345\215\247\350\226\252\345\260\235\350\203\206" +++ /dev/null @@ -1 +0,0 @@ -59a9358430b8a422fa26606830c753dba4a2e535 diff --git "a/submittors/\345\244\247\350\210\271.txt" "b/submittors/\345\244\247\350\210\271.txt" deleted file mode 100644 index c1415b32..00000000 --- "a/submittors/\345\244\247\350\210\271.txt" +++ /dev/null @@ -1 +0,0 @@ -d9201266a3a780ed35d6b99c692697fe4d4d44cc \ No newline at end of file diff --git "a/submittors/\346\265\201\346\230\237.txt" "b/submittors/\346\265\201\346\230\237.txt" deleted file mode 100644 index 2b760008..00000000 --- "a/submittors/\346\265\201\346\230\237.txt" +++ /dev/null @@ -1 +0,0 @@ -8a4501508c67ba36496ef0fb6801c3facb8aa51b \ No newline at end of file diff --git "a/submittors/\346\265\201\346\230\237\346\263\2523.22" "b/submittors/\346\265\201\346\230\237\346\263\2523.22" deleted file mode 100644 index aba31b64..00000000 --- "a/submittors/\346\265\201\346\230\237\346\263\2523.22" +++ /dev/null @@ -1 +0,0 @@ -76e7cdd552c40bb9e1d36aa56c8b907a97397360 diff --git "a/submittors/\347\216\213\350\210\252.txt" "b/submittors/\347\216\213\350\210\252.txt" deleted file mode 100644 index 3eced0cf..00000000 --- "a/submittors/\347\216\213\350\210\252.txt" +++ /dev/null @@ -1 +0,0 @@ -commit 5fe056770ce82823e0622a8efa6a40b98452a92e diff --git "a/submittors/\347\237\263\346\210\220\347\216\211" "b/submittors/\347\237\263\346\210\220\347\216\211" deleted file mode 100644 index 7e0bc387..00000000 --- "a/submittors/\347\237\263\346\210\220\347\216\211" +++ /dev/null @@ -1 +0,0 @@ -f81c17bc61a96089c91c2904be6c16ad7705fcb8 diff --git "a/submittors/\350\277\275\350\265\266.txt" "b/submittors/\350\277\275\350\265\266.txt" deleted file mode 100644 index 6c729187..00000000 --- "a/submittors/\350\277\275\350\265\266.txt" +++ /dev/null @@ -1,11 +0,0 @@ -3bcf1b2087cbf7585d4fa5b369771a26da9f0ac8 -dc27d2de0b3cc138879b04cd6ee07698bc1d1c9e -1eb92576e30f2b4fd65b3aa2a231b9ea9cb42a78 -9998650247a1150ff3892b0f8a329ac484c80413 -c179539c1a1dc25f904e4ce07791e3e1660683ae -f83c3fe86c44a7e82af1c84e2a053b4273997245 -8b09e40a27bcefa99fe9c74f28058b58387bdfd4 -b1cd19ad6ab3851cdb9c06d4b7adfec8b8b5513a -4fd6e631fbd577790b27c2bb523f9941904e8585 -040e193dbcbad32df30015468d3c3ed19e8102c4 -e2e4bf437ceeeb2bb6c724895b808b69a9639f93 \ No newline at end of file diff --git "a/submittors/\351\230\277\346\265\251.c" "b/submittors/\351\230\277\346\265\251.c" deleted file mode 100644 index 40d05d75..00000000 --- "a/submittors/\351\230\277\346\265\251.c" +++ /dev/null @@ -1 +0,0 @@ -dc476c2fe2e313c8b3ba1412c98175ddedddc06c From b8a23a55a6e26f0c0f23f10afa0851053445ef59 Mon Sep 17 00:00:00 2001 From: Jackistang Date: Wed, 3 Apr 2019 20:37:30 +0800 Subject: [PATCH 4/5] Jackistang add Jackis.cpp, Jackis-2.txt --- .../0152_Maximum_Product_Subarray/Jackis.cpp | 83 +++++++++++++++++++ submittors/Jackis-2.txt | 0 2 files changed, 83 insertions(+) create mode 100644 problems/0152_Maximum_Product_Subarray/Jackis.cpp create mode 100644 submittors/Jackis-2.txt diff --git a/problems/0152_Maximum_Product_Subarray/Jackis.cpp b/problems/0152_Maximum_Product_Subarray/Jackis.cpp new file mode 100644 index 00000000..a392001c --- /dev/null +++ b/problems/0152_Maximum_Product_Subarray/Jackis.cpp @@ -0,0 +1,83 @@ +//1 +class Solution { +public: + int maxProduct(vector& nums) { + //dp Solution + int n = nums.size(); + vector f(n); //max + vector g(n); //min + int res; + res = f[0] = g[0] = nums[0]; + for(int i=1; i res) + res = f[i]; + } + return res; + } +}; + +//2 +class Solution { +public: + int maxProduct(vector& nums) { + //dp Solution + int res=nums[0], mx=res, mn=res; + for(int i=1; i 0) + { + mx = max(mx*nums[i], nums[i]); + mn = min(mn*nums[i], nums[i]); + } + else + { + int tmp = mx; + mx = max(mn*nums[i], nums[i]); + mn = min(tmp*nums[i], nums[i]); + } + res = max(res, mx); + } + return res; + } +}; +//3 +class Solution { +public: + int maxProduct(vector& nums) { + //dp Solution + int res=nums[0], mx=res, mn=res; + for(int i=1; i& nums) { + int res=nums[0], prod=1, n=nums.size(); + for(int i=0; i=0; i--) + { + res = max(res, prod*=nums[i]); + if(nums[i] == 0) + prod = 1; + } + return res; + } +}; \ No newline at end of file diff --git a/submittors/Jackis-2.txt b/submittors/Jackis-2.txt new file mode 100644 index 00000000..e69de29b From 4ee3bd5afaa9f5f634c3d0dbe0635afcd9855253 Mon Sep 17 00:00:00 2001 From: Jackistang Date: Wed, 3 Apr 2019 20:40:59 +0800 Subject: [PATCH 5/5] rm c.c --- c.c | 1 - 1 file changed, 1 deletion(-) delete mode 100644 c.c diff --git a/c.c b/c.c deleted file mode 100644 index 9d349d40..00000000 --- a/c.c +++ /dev/null @@ -1 +0,0 @@ -This file is added by Jackis