From de4673c0eb366d4a3ff81da694e213c0424969bc Mon Sep 17 00:00:00 2001 From: Spareribs <370835062@qq.com> Date: Wed, 5 Dec 2018 23:30:25 +0800 Subject: [PATCH 01/25] =?UTF-8?q?=E6=B5=8B=E8=AF=95push?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Readme.md | 0 Spareribs/readme.md | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename Spareribs/{53 merge_two_sorted_lists => 0053 merge_two_sorted_lists}/Readme.md (100%) diff --git a/Spareribs/53 merge_two_sorted_lists/Readme.md b/Spareribs/0053 merge_two_sorted_lists/Readme.md similarity index 100% rename from Spareribs/53 merge_two_sorted_lists/Readme.md rename to Spareribs/0053 merge_two_sorted_lists/Readme.md diff --git a/Spareribs/readme.md b/Spareribs/readme.md index 1bebbee..c316253 100644 --- a/Spareribs/readme.md +++ b/Spareribs/readme.md @@ -2,5 +2,5 @@ ## 难度简单 -53 最大子序和 merge_two_sorted_lists [点击跳转](https://github.com/spareribs/learning-algorithm/tree/master/Spareribs/53%20merge_two_sorted_lists) +53 最大子序和 merge_two_sorted_lists [点击跳转](https://github.com/guangcity/learning-algorithm/tree/master/Spareribs/0053%20merge_two_sorted_lists) From 4a913d0dbd68c51085ba09908dc53f50a9477e29 Mon Sep 17 00:00:00 2001 From: H_G <23271365+simkakashi@users.noreply.github.com> Date: Thu, 6 Dec 2018 13:33:34 +0800 Subject: [PATCH 02/25] Update readme.md --- HughGuo/readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HughGuo/readme.md b/HughGuo/readme.md index d6aa085..5d3aedf 100644 --- a/HughGuo/readme.md +++ b/HughGuo/readme.md @@ -1,3 +1,4 @@ # HughGuo贡献内容 -- 两数求和 \ No newline at end of file +- #2. 两数求和 +- #3. 无重复字符的最长子串 From 8148be9f52e7895fc50c06f140e0e46158ec865d Mon Sep 17 00:00:00 2001 From: H_G <23271365+simkakashi@users.noreply.github.com> Date: Fri, 7 Dec 2018 09:32:03 +0800 Subject: [PATCH 03/25] Add files via upload --- ...200\351\225\277\345\255\220\344\270\262.py" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" diff --git "a/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" new file mode 100644 index 0000000..c90a041 --- /dev/null +++ "b/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" @@ -0,0 +1,18 @@ +class Solution: + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + temp = 0 + d = {} + start = 0 + for i in range(len(s)): + if s[i] in d and start <= d[s[i]] : + start = d[s[i]] +1 + temp = max(i-start+1,temp) + d[s[i]] = i + return temp + +## 维护两个字符串,一个是当前最大的字符串l,一个是当前字符串t,最初的时候这两个字符串都为空。每扫一个字符,如果这个字符不在字符串当中,就把当前字符串加上这个字符。如果在,当前字符串就不能再往前加字符了,然后比较当前字符串和当前最大字符串。再把当前字符串的开始地址替换掉因为不能有重复元素,##麻烦 +## 滑动窗口! 机智! From 2848a3dc5d1859c6ae5e6c8b8a404ca5cd6da425 Mon Sep 17 00:00:00 2001 From: ParkFeng <1351315441@qq.com> Date: Sat, 8 Dec 2018 00:40:38 +0800 Subject: [PATCH 04/25] second commit --- ParkFeng/random_pick_with_weight/Learn1.java | 43 ++++++++++++++++++++ ParkFeng/random_pick_with_weight/readme.md | 41 +++++++++++++++++++ ParkFeng/readme.md | 4 +- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 ParkFeng/random_pick_with_weight/Learn1.java create mode 100644 ParkFeng/random_pick_with_weight/readme.md diff --git a/ParkFeng/random_pick_with_weight/Learn1.java b/ParkFeng/random_pick_with_weight/Learn1.java new file mode 100644 index 0000000..6624755 --- /dev/null +++ b/ParkFeng/random_pick_with_weight/Learn1.java @@ -0,0 +1,43 @@ + +class Solution { + + int[] arr; + int maxOfArr; + Random random = new Random(); + public Solution(int[] w) { + arr = new int[w.length]; + arr[0] = w[0]; + for (int i = 1; i nums[end]) { + return end+1; + } else if (m > nums[start]) { + return start+1; + } else { + return start; + } + } +} \ No newline at end of file diff --git a/ParkFeng/random_pick_with_weight/readme.md b/ParkFeng/random_pick_with_weight/readme.md new file mode 100644 index 0000000..9199e8d --- /dev/null +++ b/ParkFeng/random_pick_with_weight/readme.md @@ -0,0 +1,41 @@ +题目: + +给定一个正整数数组 w ,其中 w[i] 代表位置 i 的权重,请写一个函数 pickIndex ,它可以随机地获取位置 i, +选取位置 i 的概率与 w[i] 成正比。 + +说明: + +1 <= w.length <= 10000 +1 <= w[i] <= 10^5 +pickIndex 将被调用不超过 10000 次 +示例1: + +输入: +["Solution","pickIndex"] +[[[1]],[]] +输出: [null,0] +示例2: + +输入: +["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"] +[[[1,3]],[],[],[],[],[]] +输出: [null,0,1,1,1,0] +输入语法说明: + +输入是两个列表:调用成员函数名和调用的参数。Solution 的构造函数有一个参数,即数组 w。 + pickIndex 没有参数。输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。 + + +题目分析: + +1、该题针对:正整数数组---权重数组 + +2、权重随机理解: +eg: int[] arr = {3,4}--->向上抛一个硬币(假设硬币落地一定是平躺)7次,出现正面反面的次数(权重)分别为3,4,那么就可以说这个实验中正面出现概率是 +3/7,反面出现概率是4/7,回到已知的这个数组上,3+4=7,权重随机我们可以理解为一个箱子中有七个分别标有1~7数字的乒乓球,我们随机从 +箱子中每次拿一个乒乓球,那么拿到标有1,2,3的乒乓球我们就认为对应的是数组中的3,拿到4,5,6,7就对应的是4 + +3、获取位置序号:利用二分法去查找 + + + diff --git a/ParkFeng/readme.md b/ParkFeng/readme.md index 48ff99f..e1d1353 100644 --- a/ParkFeng/readme.md +++ b/ParkFeng/readme.md @@ -2,4 +2,6 @@ -1.对角线遍历分析 \ No newline at end of file +1.对角线遍历分析 + +2.按权重随机选择 \ No newline at end of file From ab20a48971d360d9d1d8cfef222b58ee58abf98b Mon Sep 17 00:00:00 2001 From: Majingmin Date: Sat, 8 Dec 2018 10:34:47 +0800 Subject: [PATCH 05/25] the first week on leetcode --- .../longest_common_prefix.txt" | 39 +++++++++ .../Manacher.txt" | 85 +++++++++++++++++++ .../central_expansion_algorithm.txt" | 30 +++++++ .../longest_palindromic_substring/dynamic.py" | 32 +++++++ .../dynamic.txt" | 46 ++++++++++ .../violence_to_solve.txt" | 32 +++++++ .../string_to_integer/string_to_integer.txt" | 51 +++++++++++ .../sum_of_two/Violence_to_solve.txt" | 19 +++++ .../sum_of_two/hash_to_solve.txt" | 22 +++++ .../recursive_solution.txt" | 53 ++++++++++++ 10 files changed, 409 insertions(+) create mode 100644 "\351\251\254\346\231\266\346\225\217/longest_common_prefix/longest_common_prefix.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/Manacher.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/central_expansion_algorithm.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/dynamic.py" create mode 100644 "\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/dynamic.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/violence_to_solve.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/string_to_integer/string_to_integer.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/sum_of_two/Violence_to_solve.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/sum_of_two/hash_to_solve.txt" create mode 100644 "\351\251\254\346\231\266\346\225\217/the_median_of_two_ordered_array/recursive_solution.txt" diff --git "a/\351\251\254\346\231\266\346\225\217/longest_common_prefix/longest_common_prefix.txt" "b/\351\251\254\346\231\266\346\225\217/longest_common_prefix/longest_common_prefix.txt" new file mode 100644 index 0000000..76c7ce4 --- /dev/null +++ "b/\351\251\254\346\231\266\346\225\217/longest_common_prefix/longest_common_prefix.txt" @@ -0,0 +1,39 @@ +public class Solution { + public string LongestCommonPrefix(string[] strs) { + int n=strs.Length; + if(n==0) + return ""; + int minI=0; + int minL=int.MaxValue; + int currl=0; + for(int i=0;i len = new List(); + //计算Len[]数组 + for (int i = 0; i < m; i++) + { + int r = 0; + if (i >= maxR) + { + int j = 0; + while (i - j >= 0 && i + j < m) + { + if (s1[i - j] == s1[i + j]) + { + if (i + j >= maxR) + { + r = i + j; + } + j++; + } + else + break; + } + if (r > maxR) + { + cenI = i; + maxR = r; + } + len.Add(r - i + 1); + if (r - i + 1 >= len.Max()) + cenIL = i; + } + else + { + int iL = 2 * cenI - i; + if (i <= maxR) + { + if (len[iL] < maxR - i) + len.Add(len[iL]); + else + { + int j = 0; + while (i - j >= 0 && i + j < m) + { + if (s1[i - j] == s1[i + j]) + { + if (i + j >= maxR) + { + r = i + j; + } + j++; + } + else + break; + } + if (r > maxR) + { + cenI = i; + maxR = r; + } + len.Add(r - i + 1); + if (r - i + 1 >= len.Max()) + cenIL = i; + } + } + } + } + char[] sp= { '#' }; + string subS1 = s1.Substring(cenIL-len[cenIL]+1,2*(len[cenIL]-1)+1); + string[] subS = subS1.Split(sp, StringSplitOptions.None); + string subSs=string.Join("", subS); + return subSs; + } +} \ No newline at end of file diff --git "a/\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/central_expansion_algorithm.txt" "b/\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/central_expansion_algorithm.txt" new file mode 100644 index 0000000..f91861e --- /dev/null +++ "b/\351\251\254\346\231\266\346\225\217/longest_palindromic_substring/central_expansion_algorithm.txt" @@ -0,0 +1,30 @@ +public class Solution { + public string LongestPalindrome(string s) { + if(s==null || s.Length<1) + return ""; + int n=s.Length; + int start=0; + int end=0; + for(int i=0;ilen2?len1:len2; + if(len>end-start) + { + start=i-(len-1)/2; + end=i+ len /2; + } + } + return s.Substring(start,end-start+1); + } + public int expandAroundCenters(string s,int left,int right) + { + while(left>=0 && right0;i--) + { + for(int j=0;j hash = new Dictionary(); + int[] result = new int[2]; + for (int i = 0; i < nums.Length; i++) + { + int num = target - nums[i]; + if (hash.ContainsKey(num)) + { + result=new int[] { hash[num], i}; + break; + } + else + { + hash[nums[i]] = i; + } + } + return result; + } +} \ No newline at end of file diff --git "a/\351\251\254\346\231\266\346\225\217/the_median_of_two_ordered_array/recursive_solution.txt" "b/\351\251\254\346\231\266\346\225\217/the_median_of_two_ordered_array/recursive_solution.txt" new file mode 100644 index 0000000..7aafa09 --- /dev/null +++ "b/\351\251\254\346\231\266\346\225\217/the_median_of_two_ordered_array/recursive_solution.txt" @@ -0,0 +1,53 @@ +public class Solution { + public double FindMedianSortedArrays(int[] nums1, int[] nums2) { + int len1=nums1.Length; + int len2=nums2.Length; + int maxLeft=0; + int minRight=0; + if(len1>len2) + { + int[] temp=nums1; + nums1=nums2; + nums2=temp; + int tem=len1; + len1=len2; + len2=tem; + //FindMedianSortedArrays(nums2,nums1); + } + int iMin=0; + int iMax=len1; + int halfLen=(len1+len2+1)/2; + while(iMin<=iMax) + { + int i = (iMin+iMax)/2; + int j = halfLen-i; + if(inums1[i]) + { + iMin=i+1; + } + else if(i>iMin && nums1[i-1]>nums2[j]) + { + iMax=i-1; + } + else + { + if(i==0) + maxLeft=nums2[j-1]; + else if(j==0) + maxLeft=nums1[i-1]; + else + maxLeft=Math.Max(nums1[i-1],nums2[j-1]); + if((len1+len2)%2==1) + return maxLeft; + if(i==len1) + minRight=nums2[j]; + else if(j==len2) + minRight=nums1[i]; + else + minRight=Math.Min(nums1[i],nums2[j]); + return (maxLeft+minRight)/2.0; + } + } + return -1; + } +} \ No newline at end of file From b1d171c7f89af44a7190a95f3e9cf741b535b195 Mon Sep 17 00:00:00 2001 From: Majingmin Date: Sat, 8 Dec 2018 10:53:47 +0800 Subject: [PATCH 06/25] add the readme --- "\351\251\254\346\231\266\346\225\217/readme.txt" | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 "\351\251\254\346\231\266\346\225\217/readme.txt" diff --git "a/\351\251\254\346\231\266\346\225\217/readme.txt" "b/\351\251\254\346\231\266\346\225\217/readme.txt" new file mode 100644 index 0000000..488afb2 --- /dev/null +++ "b/\351\251\254\346\231\266\346\225\217/readme.txt" @@ -0,0 +1,7 @@ +Hello,小伙伴你好! +我是curry_girl,马晶敏 +很高兴你能在茫茫人海中点开这篇readme +如果你发现文件后缀是.txt,那这是我用C#语言 +如果你发现文件后缀是.py,就是python啦 +我应该走出舒适区多用python写的 +最后,希望这个能帮到你 \ No newline at end of file From a54cd008d682d7b093150e86ee56efbb478806ef Mon Sep 17 00:00:00 2001 From: Majingmin Date: Sat, 8 Dec 2018 11:28:27 +0800 Subject: [PATCH 07/25] update --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index cb39dbb..af090f0 100644 --- a/readme.md +++ b/readme.md @@ -27,7 +27,7 @@ | 底层搬砖工 | [直通点](https://github.com/guangcity/learning-algorithm/tree/master/%E5%BA%95%E5%B1%82%E6%90%AC%E7%A0%96%E5%B7%A5) | | miss-ann | [直通点](https://github.com/guangcity/learning-algorithm/tree/master/miss-ann) | | woyaoyangguang | [直通点](https://github.com/guangcity/learning-algorithm/tree/master/woyaoyangguang) | -| | | +| 马静敏 | [直通点](https://github.com/guangcity/learning-algorithm/tree/master/马晶敏) | | | | | | | | | | From 274bdc3877f557a520462c9f2e365c7ab48dd7d1 Mon Sep 17 00:00:00 2001 From: SugarChl <738763474@qq.com> Date: Sat, 8 Dec 2018 13:34:05 +0800 Subject: [PATCH 08/25] SugarChl-pr --- ...4\346\234\200\345\260\217\345\200\274_1.c" | 0 ...4\346\234\200\345\260\217\345\200\274_2.c" | 0 .../readme.md" | 6 ++ ...247\346\255\243\346\226\271\345\275\242.c" | 38 ++++++++++++ .../readme.md" | 4 ++ ...236\346\226\207\345\255\220\344\270\262.c" | 59 +++++++++++++++++++ .../readme.md" | 4 ++ SugarChl/readme.md | 3 + ...247\346\255\243\346\226\271\345\275\242.c" | 38 ++++++++++++ .../readme.md" | 4 ++ ...236\346\226\207\345\255\220\344\270\262.c" | 59 +++++++++++++++++++ .../readme.md" | 4 ++ .../readme.md" | 33 +++++++++++ 13 files changed, 252 insertions(+) rename "SugarChl/xuanzhuanSort/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_1.c" => "SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_1.c" (100%) rename "SugarChl/xuanzhuanSort/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_2.c" => "SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_2.c" (100%) create mode 100644 "SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/readme.md" create mode 100644 "SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" create mode 100644 "SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" create mode 100644 "SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" create mode 100644 "SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" create mode 100644 "SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" create mode 100644 "SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" create mode 100644 "SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" create mode 100644 "SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" create mode 100644 "SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" diff --git "a/SugarChl/xuanzhuanSort/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_1.c" "b/SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_1.c" similarity index 100% rename from "SugarChl/xuanzhuanSort/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_1.c" rename to "SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_1.c" diff --git "a/SugarChl/xuanzhuanSort/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_2.c" "b/SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_2.c" similarity index 100% rename from "SugarChl/xuanzhuanSort/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_2.c" rename to "SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274_2.c" diff --git "a/SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/readme.md" "b/SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/readme.md" new file mode 100644 index 0000000..25a9059 --- /dev/null +++ "b/SugarChl/153. \345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/readme.md" @@ -0,0 +1,6 @@ +寻找旋转排序数组中的最小值 + +153. 寻找旋转排序数组中的最小值_1.c 是完全自己写的代码 + +153. 寻找旋转排序数组中的最小值_2.c 是网络上找到的代码 + diff --git "a/SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" "b/SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" new file mode 100644 index 0000000..109fa4c --- /dev/null +++ "b/SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" @@ -0,0 +1,38 @@ +int min(int a,int b) +{ + if(a>b) + return b; + else + return a; +} + + +int maximalSquare(char** matrix, int matrixRowSize, int matrixColSize) { + int row,col; + int max=0; + int m[1000][1000]; + + for(row=0;rowmax) + max=m[row][col]; + } + } + return max*max; +} diff --git "a/SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" "b/SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" new file mode 100644 index 0000000..d63a9c5 --- /dev/null +++ "b/SugarChl/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" @@ -0,0 +1,4 @@ +# 解题思路 + + 参考了网络上的答案, + 使用动态规划的方法。 \ No newline at end of file diff --git "a/SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" "b/SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" new file mode 100644 index 0000000..6e0349c --- /dev/null +++ "b/SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" @@ -0,0 +1,59 @@ +#include +char* longestPalindrome(char* s) { + int len = strlen(s); + int n = 2*len-1; + int i; + int max=0; + int left = 0; + int right = 0; + int L=0,R=0; + for(i=0;i=0&&Rmax) + { + max=R-L; + right=R; + left=L; + } + } + else if(i%2==1) + { + L=(i-1)/2; + R=(i+1)/2; + while(L>=0&&Rmax) + { + max=R-L; + right=R; + left=L; + } + } + } + char *p=malloc(1000*sizeof('x')); + int k=0; + for(i=left;i<=right;i++) + { + //printf("%c",s[i]); + p[k++]=s[i]; + } + p[k]='\0'; + return p; +} \ No newline at end of file diff --git "a/SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" "b/SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" new file mode 100644 index 0000000..d63a9c5 --- /dev/null +++ "b/SugarChl/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" @@ -0,0 +1,4 @@ +# 解题思路 + + 参考了网络上的答案, + 使用动态规划的方法。 \ No newline at end of file diff --git a/SugarChl/readme.md b/SugarChl/readme.md index ba27de3..7194579 100644 --- a/SugarChl/readme.md +++ b/SugarChl/readme.md @@ -8,3 +8,6 @@ 153. 寻找旋转排序数组中的最小值_2.c 是网络上找到的代码 +5.最长回文子串 + +221.最大正方形 \ No newline at end of file diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" new file mode 100644 index 0000000..109fa4c --- /dev/null +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.c" @@ -0,0 +1,38 @@ +int min(int a,int b) +{ + if(a>b) + return b; + else + return a; +} + + +int maximalSquare(char** matrix, int matrixRowSize, int matrixColSize) { + int row,col; + int max=0; + int m[1000][1000]; + + for(row=0;rowmax) + max=m[row][col]; + } + } + return max*max; +} diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" new file mode 100644 index 0000000..d63a9c5 --- /dev/null +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/221. \346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/readme.md" @@ -0,0 +1,4 @@ +# 解题思路 + + 参考了网络上的答案, + 使用动态规划的方法。 \ No newline at end of file diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" new file mode 100644 index 0000000..6e0349c --- /dev/null +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.c" @@ -0,0 +1,59 @@ +#include +char* longestPalindrome(char* s) { + int len = strlen(s); + int n = 2*len-1; + int i; + int max=0; + int left = 0; + int right = 0; + int L=0,R=0; + for(i=0;i=0&&Rmax) + { + max=R-L; + right=R; + left=L; + } + } + else if(i%2==1) + { + L=(i-1)/2; + R=(i+1)/2; + while(L>=0&&Rmax) + { + max=R-L; + right=R; + left=L; + } + } + } + char *p=malloc(1000*sizeof('x')); + int k=0; + for(i=left;i<=right;i++) + { + //printf("%c",s[i]); + p[k++]=s[i]; + } + p[k]='\0'; + return p; +} \ No newline at end of file diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" new file mode 100644 index 0000000..d63a9c5 --- /dev/null +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/readme.md" @@ -0,0 +1,4 @@ +# 解题思路 + + 参考了网络上的答案, + 使用动态规划的方法。 \ No newline at end of file diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" new file mode 100644 index 0000000..8d12e18 --- /dev/null +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" @@ -0,0 +1,33 @@ +# 动态规划 个人总结 + +做了一些题目之后,就无法忍受暴力的方法来解题了。 +针对可以使用动态规划的题目,做一些总结,请各位大佬指正~ + + +当一个大规模问题可以被分解为小规模子问题,那么一般来说,这个问题就可以使用动态规划来求解。 +要使用动态规划,那么就要构建状态转移方程。 +“状态转移方程,是动态规划中本阶段的状态往往是上一阶段状态和上一阶段决策的结果”(来自百度百科) +动态规划,是要将问题分解。 +那么,问题与子问题之间该怎么连接呢。 +这就要使用到动态规划。 + + +比如 +221.最大正方形的问题。 + 选取矩阵中的一个点。x[i][j] + x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 +   ①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; + +  ②若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]均不为0,则DP[i][j]为三者中的最小值+1,因为三者中的最小值一定为三者所共有的不含0的部分,否则会造成缺角; + +因此对于某一点(i,j), 若matrix[i][j]=1,则动态规划表达式为DP[i][j] = min{DP[i-1][j],DP[i][j-1],DP[i-1][j-1]} + 1; + + +5.最长回文子串 + 对于字符串str,假设dp[i,j]=1表示str[i...j]是回文子串 + 那么dp[i+1,j-1] 也必然是回文子串 + 这样最长回文子串就被分解成了一系列规模小的子问题。这样就能求解了。 + + 状态转移方程: dp[i,j]={dp[i+1,j-1] , str[i]==str[j] + 0 , str[i]!=str[j]} + \ No newline at end of file From 45acb3c1482bd035d656e85544c7d5b8c7c2eee2 Mon Sep 17 00:00:00 2001 From: SugarChl <31557469+SugarChl@users.noreply.github.com> Date: Sat, 8 Dec 2018 13:43:12 +0800 Subject: [PATCH 09/25] Update readme.md --- .../readme.md" | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" index 8d12e18..a454c19 100644 --- "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" @@ -1,4 +1,4 @@ -# 动态规划 个人总结 +# 动态规划 个人归纳 做了一些题目之后,就无法忍受暴力的方法来解题了。 针对可以使用动态规划的题目,做一些总结,请各位大佬指正~ @@ -9,25 +9,21 @@ “状态转移方程,是动态规划中本阶段的状态往往是上一阶段状态和上一阶段决策的结果”(来自百度百科) 动态规划,是要将问题分解。 那么,问题与子问题之间该怎么连接呢。 -这就要使用到动态规划。 +这就要构建状态转移方程。 -比如 -221.最大正方形的问题。 - 选取矩阵中的一个点。x[i][j] - x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 -   ①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; +## 221.最大正方形的问题。 +>选取矩阵中的一个点。x[i][j] +>x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 +>①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; +>②若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]均不为0,则DP[i][j]为三者中的最小值+1,因为三者中的最小值一定为三者所共有的不含0的部分,否则会造成缺角; -  ②若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]均不为0,则DP[i][j]为三者中的最小值+1,因为三者中的最小值一定为三者所共有的不含0的部分,否则会造成缺角; +>因此对于某一点(i,j), 若matrix[i][j]=1,则动态规划表达式为DP[i][j] = min{DP[i-1][j],DP[i][j-1],DP[i-1][j-1]} + 1; -因此对于某一点(i,j), 若matrix[i][j]=1,则动态规划表达式为DP[i][j] = min{DP[i-1][j],DP[i][j-1],DP[i-1][j-1]} + 1; - - -5.最长回文子串 - 对于字符串str,假设dp[i,j]=1表示str[i...j]是回文子串 - 那么dp[i+1,j-1] 也必然是回文子串 - 这样最长回文子串就被分解成了一系列规模小的子问题。这样就能求解了。 +## 5.最长回文子串 +>对于字符串str,假设dp[i,j]=1表示str[i...j]是回文子串 +>那么dp[i+1,j-1] 也必然是回文子串 +>这样最长回文子串就被分解成了一系列规模小的子问题。这样就能求解了。 + +>状态转移方程: dp[i,j]={dp[i+1,j-1],str[i]==str[j];0,str[i]!=str[j]} - 状态转移方程: dp[i,j]={dp[i+1,j-1] , str[i]==str[j] - 0 , str[i]!=str[j]} - \ No newline at end of file From e32e8d81fa47932df9714ca3ff962ddd5e80299e Mon Sep 17 00:00:00 2001 From: infisa Date: Sat, 8 Dec 2018 14:00:27 +0800 Subject: [PATCH 10/25] new-pr --- .idea/workspace.xml | 205 +++++++++++++----- woyaoyangguang/add one/add_one.py | 46 ++++ woyaoyangguang/add one/readme.md | 5 + .../{ => delete_duplicate}/readme.md | 0 4 files changed, 207 insertions(+), 49 deletions(-) create mode 100644 woyaoyangguang/add one/add_one.py create mode 100644 woyaoyangguang/add one/readme.md rename woyaoyangguang/{ => delete_duplicate}/readme.md (100%) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 659275b..d2a73e8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,13 +2,10 @@ - - - - - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -58,10 +112,10 @@ - @@ -79,13 +133,18 @@ - + - - + + + + + + +