From d09df35169705a05b758fb245c0611421182226b Mon Sep 17 00:00:00 2001 From: bajdcc Date: Mon, 16 Jan 2017 12:51:40 +0800 Subject: [PATCH 01/13] Add hard problems --- leetcode/04/note.md | 65 +++++++++++++++++++++++++++++++++++++++++++ leetcode/10/note.md | 65 +++++++++++++++++++++++++++++++++++++++++++ leetcode/37/note.md | 56 +++++++++++++++++++++++++++++++++++++ leetcode/41/note.md | 50 +++++++++++++++++++++++++++++++++ leetcode/51/note.md | 67 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/65/note.md | 53 +++++++++++++++++++++++++++++++++++ 6 files changed, 356 insertions(+) create mode 100644 leetcode/04/note.md create mode 100644 leetcode/10/note.md create mode 100644 leetcode/37/note.md create mode 100644 leetcode/41/note.md create mode 100644 leetcode/51/note.md create mode 100644 leetcode/65/note.md diff --git a/leetcode/04/note.md b/leetcode/04/note.md new file mode 100644 index 0000000..42b09db --- /dev/null +++ b/leetcode/04/note.md @@ -0,0 +1,65 @@ +## 链接 + +4.Median of Two Sorted Arrays(Hard) + +## 题目 + +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)). + +**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 +``` + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/10/note.md b/leetcode/10/note.md new file mode 100644 index 0000000..220e116 --- /dev/null +++ b/leetcode/10/note.md @@ -0,0 +1,65 @@ +## 链接 + +10.Regular Expression Matching(Hard) + +## 题目 + +Implement regular expression matching with support for `'.'` and `'*'`. + +``` +'.' Matches any single character. +'*' Matches zero or more of the preceding element. + +The matching should cover the entire input string (not partial). + +The function prototype should be: +bool isMatch(const char *s, const char *p) + +Some examples: +isMatch("aa","a") → false +isMatch("aa","aa") → true +isMatch("aaa","aa") → false +isMatch("aa", "a*") → true +isMatch("aa", ".*") → true +isMatch("ab", ".*") → true +isMatch("aab", "c*a*b") → true +``` + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/37/note.md b/leetcode/37/note.md new file mode 100644 index 0000000..d6e0bb2 --- /dev/null +++ b/leetcode/37/note.md @@ -0,0 +1,56 @@ +## 链接 + +37.Sudoku Solver(Hard) + +## 题目 + +Write a program to solve a Sudoku puzzle by filling the empty cells. + +Empty cells are indicated by the character `'.'`. + +You may assume that there will be only one unique solution. + +![img](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png) + +A sudoku puzzle... + +![img](http://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png) + +...and its solution numbers marked in red. + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/41/note.md b/leetcode/41/note.md new file mode 100644 index 0000000..20029b6 --- /dev/null +++ b/leetcode/41/note.md @@ -0,0 +1,50 @@ +## 链接 + +41.First Missing Positive(Hard) + +## 题目 + +Given an unsorted integer array, find the first missing positive integer. + +For example, +Given `[1,2,0]` return `3`, +and `[3,4,-1,1]` return `2`. + +Your algorithm should run in *O*(*n*) time and uses constant space. + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/51/note.md b/leetcode/51/note.md new file mode 100644 index 0000000..14feca9 --- /dev/null +++ b/leetcode/51/note.md @@ -0,0 +1,67 @@ +## 链接 + +51.N-Queens(Hard) + +## 题目 + +The *n*-queens puzzle is the problem of placing *n* queens on an *n*×*n* chessboard such that no two queens attack each other. + +![img](http://www.leetcode.com/wp-content/uploads/2012/03/8-queens.png) + +Given an integer *n*, return all distinct solutions to the *n*-queens puzzle. + +Each solution contains a distinct board configuration of the *n*-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space respectively. + +For example, +There exist two distinct solutions to the 4-queens puzzle: + +``` +[ + [".Q..", // Solution 1 + "...Q", + "Q...", + "..Q."], + + ["..Q.", // Solution 2 + "Q...", + "...Q", + ".Q.."] +] +``` + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/65/note.md b/leetcode/65/note.md new file mode 100644 index 0000000..d9f2e33 --- /dev/null +++ b/leetcode/65/note.md @@ -0,0 +1,53 @@ +## 链接 + +65.Valid Number(Hard) + +## 题目 + +Validate if a given string is numeric. + +Some examples: +`"0"` => `true` +`" 0.1 "` => `true` +`"abc"` => `false` +`"1 a"` => `false` +`"2e10"` => `true` + +**Note:** It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file From 3d7f6b6ae85b7ee8fd7dafe28b07c931a78c94d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sat, 21 Jan 2017 09:51:57 +0800 Subject: [PATCH 02/13] Add note Median of Two Sorted Arrays --- leetcode/04/note_bajdcc.md | 174 +++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 leetcode/04/note_bajdcc.md diff --git a/leetcode/04/note_bajdcc.md b/leetcode/04/note_bajdcc.md new file mode 100644 index 0000000..77072a8 --- /dev/null +++ b/leetcode/04/note_bajdcc.md @@ -0,0 +1,174 @@ +## 链接 + +4.Median of Two Sorted Arrays(Hard) + +## 题目 + +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)). + +**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 +``` + +## 释义 + +将两个有序数列进行归并,找到其中位数。 + +先计算中位数: + +- 对总长n为奇数而言,中位数位置为n/2 +- 对总长n为偶数而言,中位数位置为(n-1)/2和(n-1)/2+1 + +问题在于如何求出指定位置的数。 + +### 特例 + +特例肯定有,想一想: + +- 其中一个数列为空,那么问题简单,在另一个数列中找即可 +- 一个数列的最大值小于另一数列的最小值,即数列1最末位小于数列2最前位,这时将数列1和2看作整体,再进行查找 + +### 另外情况 + +这时只能从前面开始一个个比较。设当前需要找到第index位。 + +分别用两个指针记录当前数列1和2的位置。哪边的值较小,就将相应的位置前进一位,index递减。不断循环。 + +什么时候跳出循环: + +- 某个数列被遍历到末尾了,这时只需要根据剩余的index在另外一个数列中查找即可 +- index为-1了,即相应的位置已被找到 + +下面有几种情况: + +- 其中一个数列遍历到末尾。若此时index为-1,那么位置就是这个数列的最末位,如果是奇数(中位数只要1位),就直接返回了;如果是偶数,就算上另一个数列的当前位置。若此时index不为-1,那么说明还要再往前面找几位,那么已遍历完的那个数列就out了,接下来只要在另一数列中查找即可。 +- 若不是上面的情况,就是最典型的情况了。先根据first知道上一个值是在哪一个数列中,然而找出这个值num[ptr-1],为什么要减一呢,这是因为在while循环中ptr需要向前移一位。如果是奇数,那么直接返回这个值;如果是偶数,那需要找出下一个值,下一个值在当前值后面一位和另外一数列当前位置中以较小值选择出来。 + +为什么这题难度为hard,我想不是因为思路想不到(归并),而是因为其中的步骤很烦,什么+1什么-1还要各种判断。 + +当然,还有可以优化的地方。如其中一数列元素只有一个,那么归并就变成了二分插入,能节省时间。 + + +## 补充描述 + +83.14% https://leetcode.com/submissions/detail/89933853/ + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 +#define MIN(a,b) (((a)<(b))?(a):(b)) + +class Solution { +public: + static double findIdx(const vector &nums1, const vector &nums2, int index, bool odd) + { + int len1 = nums1.size(), len2 = nums2.size(); + auto ptr1 = 0, ptr2 = 0; + if (len1 == 0) + { + return odd ? nums2[index] : ((nums2[index] + nums2[index + 1]) / 2.0); + } + if (len2 == 0) + { + return odd ? nums1[index] : ((nums1[index] + nums1[index + 1]) / 2.0); + } + if (nums1.back() <= nums2.front()) + { + if (odd) + return index < len1 ? nums1[index] : nums2[index - len1]; + if (index < len1 - 1) + return (nums1[index] + nums1[index + 1]) / 2.0; + if (index > len1 - 1) + return (nums2[index - len1] + nums2[index - len1 + 1]) / 2.0; + return (nums1[index] + nums2[0]) / 2.0; + } + if (nums2.back() <= nums1.front()) + { + if (odd) + return index < len2 ? nums2[index] : nums1[index - len2]; + if (index < len2 - 1) + return (nums2[index] + nums2[index + 1]) / 2.0; + if (index > len2 - 1) + return (nums1[index - len2] + nums1[index - len2 + 1]) / 2.0; + return (nums2[index] + nums1[0]) / 2.0; + } + auto first = true; + while (ptr1 < len1 && ptr2 < len2 && index-- >= 0) + { + first = nums1[ptr1] <= nums2[ptr2]; + first ? ptr1++ : ptr2++; + } + if (index == -1) + { + if (ptr1 == len1) + { + return odd ? nums1.back() : (nums2[ptr2] + nums1.back()) / 2.0; + } + if (ptr2 == len2) + { + return odd ? nums2.back() : (nums1[ptr1] + nums2.back()) / 2.0; + } + } + if (ptr1 == len1) + { + return odd ? nums2[ptr2 + index] : (nums2[ptr2 + index] + nums2[ptr2 + index + 1]) / 2.0; + } + if (ptr2 == len2) + { + return odd ? nums1[ptr1 + index] : (nums1[ptr1 + index] + nums1[ptr1 + index + 1]) / 2.0; + } + if (first) + { + if (odd) + return nums1[ptr1 - 1]; + return (nums1[ptr1 - 1] + MIN(nums1[ptr1], nums2[ptr2])) / 2.0; + } + if (odd) + return nums2[ptr2 - 1]; + return (nums2[ptr2 - 1] + MIN(nums2[ptr2], nums1[ptr1])) / 2.0; + } + + double findMedianSortedArrays(vector &nums1, vector &nums2) + { + auto count = nums1.size() + nums2.size(); + auto idx = (count - 1) / 2; + return findIdx(nums1, nums2, idx, count % 2 == 1); + } +}; + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file From 6624ec5676c76e0dd2e3cc4e16b8cb670887277d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sun, 22 Jan 2017 00:50:17 +0800 Subject: [PATCH 03/13] Add note Regular Expression Matching --- leetcode/10/note_bajdcc.md | 293 +++++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 leetcode/10/note_bajdcc.md diff --git a/leetcode/10/note_bajdcc.md b/leetcode/10/note_bajdcc.md new file mode 100644 index 0000000..d20efe0 --- /dev/null +++ b/leetcode/10/note_bajdcc.md @@ -0,0 +1,293 @@ +## 链接 + +10.Regular Expression Matching(Hard) + +## 题目 + +Implement regular expression matching with support for `'.'` and `'*'`. + +``` +'.' Matches any single character. +'*' Matches zero or more of the preceding element. + +The matching should cover the entire input string (not partial). + +The function prototype should be: +bool isMatch(const char *s, const char *p) + +Some examples: +isMatch("aa","a") → false +isMatch("aa","aa") → true +isMatch("aaa","aa") → false +isMatch("aa", "a*") → true +isMatch("aa", ".*") → true +isMatch("ab", ".*") → true +isMatch("aab", "c*a*b") → true +``` + + + + +## 方法一:回溯法 + +这是最容易想到的方法。 + +设模式串为pat,要匹配的串为str。 + +如果pat和str当前全是字母,那就一位位匹配即可。但这里有难点,也就是pat的字母后面可能跟\*,这个\*非常的特殊。 + +比如a\*,它可以匹配连续的a,也可以什么都不匹配,这是比较难的地方。接触过编译原理就会知道,这个\*对应的是epsilon串,这使得整个状态机变成不确定的,不确定状态机的后果就是需要不断回溯。回溯其实可以避免,看方法二。 + +因此,**假如知道当前的字符,万一它后面跟了*呢?**所以,需要**向前看一个字符**,设为ahead。 + +思路就出来了。设遍历pat的指针为i,遍历str的指针为j。 + +- 假如当前字母是单纯的字母,即后面没有跟着\*,那么就比对pat[i]和str[j] +- 假如当前是单纯的匹配单字符".",后面没有跟着\*,那么不需要对比,i++ +- 特殊情况,即单纯字母"a~z"或是匹配单字符"."后面带了个\*,那么就进入回溯模式,递归调用isMatch并且j++,isMatch能返回真,那么匹配成功,否则继续匹配 +- 以上情况,若跳出循环时i和j都到了pat和str的末尾,则匹配成功 + +为了辨别当前字符的类型,用了查找数组patMap。 + +下面是代码,但还有另外一种形式即全递归的,由于两者功能相同,因此略。 + + +```c++ + +//代码放在这个块里面,可以高亮关键字 +class Solution { + enum Pat + { + OTHER = 0, + ALPHABET, + DOT, + STAR, + END + }; + Pat patMap[0xff] = { OTHER }; // map pattern character + +public: + Solution() + { + for (auto i = 'a'; i <= 'z'; i++) + { + patMap[i] = ALPHABET; // [a-z] + } + patMap['.'] = DOT; // . + patMap['*'] = STAR; // * + patMap['#'] = END; // # + } + + bool isMatch(string s, string p) { + return isMatchImpl(s.c_str(), p.c_str(), s.length(), p.length()); // add stop(look ahead) + } + + using pstr = const char *; + + bool isMatchImpl(pstr s, pstr p, int slen, int plen) { + if (plen == 0) + return slen == 0; + auto c = '$', next = p[0]; + auto pat = OTHER, nextPat = patMap[next]; // look ahead + auto j = 0, i = 0, prev = -1; + for (; i < plen && j <= slen;) + { + if (prev == i - 1) + { + c = next; + pat = nextPat; + next = i + 1 == plen ? '#' : p[i + 1]; + nextPat = patMap[next]; + prev = i; + } + else if (prev < i) + { + c = p[i]; + pat = patMap[c]; + next = i + 1 == plen ? '#' : p[i + 1]; + nextPat = patMap[next]; + prev = i; + } + switch (pat) + { + case OTHER: + throw "invalid character"; + break; + case ALPHABET: + switch (nextPat) + { + case OTHER: + throw "invalid character"; + break; + case ALPHABET: + case DOT: + case END: + if (c != s[j]) + return false; // [a-z] not match + i++; j++; + break; + case STAR: + if (c != s[j]) + i += 2; // [a-z]* not match, skip pattern [a-z]* + else + { + // [a-z]* match, got epsilon, backtracking + if (isMatchImpl(s + j, p + i + 2, slen - j, plen - i - 2)) + return true; + j++; + if (j == slen) + i += 2; + } + break; + default: + break; + } + break; + case DOT: + switch (nextPat) + { + case OTHER: + throw "invalid character"; + break; + case ALPHABET: + case DOT: + case END: + i++; j++; // match . + break; + case STAR: + // got epsilon, use backtracking + if (i + 1 == plen) + { + return true; // .* match all + } + if (isMatchImpl(s + j, p + i + 2, slen - j, plen - i - 2)) + return true; + j++; + if (j == slen) + i += 2; + break; + default: + break; + } + break; + case STAR: + throw "invalid character"; // single * + break; + default: + break; + } + } + return i == plen && j == slen; + } +}; + +``` + + + +## 方法二:动态规划法 + +由于方法一存在多余的回溯问题,因此要避免回溯,采用DP方法。 + +DP方法的优点:不需要递归(不复制string),思路清晰,由状态转移方程推导出(先逻辑验证,不易出错),效率高,其结果是简化的状态机 + +DP方法的缺点:推导过程复杂 + +二维DP的一般步骤: + +1. 两个相关变量的表述,须为数字,这里是原串和模式串的长度,同时也决定了dp表的大小以及算法的复杂度 +2. dp表的值类型,这里是bool +3. 根据题意总结规则 +4. 根据规则写出详尽的状态转移方程 +5. 在双重for循环中实现状态转移 +6. 返回dp表的最右下值 + +设模式串为pat,要匹配的串为str。因此dp数组的大小是(patlen+1) x (strlen+1),大小为子串的长度。 + +推导规则: + +1. 空串匹配空串:dp(0,0)=true +2. '.\*'组合匹配空串:消除'.\*'并匹配,dp(0,j)=dp(0,j-2) +3. '.\*'或'a\*'匹配:看上次结果dp(i-1,j),若真则真;若假则看dp(i,j-2) +4. 'a\*'匹配:看上次结果dp(i-1,j),若真则比较最新匹配位s(i-1)=p(j-2);若假则看dp(i,j-2) +5. 非'\*'匹配空串:dp(0,j)=false +6. '.'匹配:看上次结果dp(i-1,j-1) +7. 'a':看上次结果dp(i-1,j-1),同时比较最新匹配位s(i-1)=p(j-1) + +代码如下,耗时是回溯法的1/3左右。 + + +```c++ + +//代码放在这个块里面,可以高亮关键字 +class Solution { +public: + bool isMatch(string s, string p) { + int slen = s.length(), plen = p.length(); + // dp[i,j] -> str[0..i-1] 是否被 pat[0..j-1] 匹配 + vector> dp(slen + 1, vector(plen + 1, false)); + // 二重循环构建dp数组,按部就班 + // dp[i,j] -> pat[0..j-1] matches str[0..i-1] + dp[0][0] = true; // empty matches empty + for (auto i = 0; i <= slen; i++) + { + // 这里省略了j从0开始的部分,因为pat为空时,除非str为空否则匹配失败 + for (auto j = 1; j <= plen; j++) + { + if (p[j - 1] == '*') // '*'不应该在第一位,因此j>1 + { + // 当前为'*',这里情况稍显复杂 + if (p[j - 2] == '.') + { + if (i > 0 && dp[i - 1][j]) + // '.*' matches 'abc' => '.*' == 'ab' + dp[i][j] = true; + if (!dp[i][j]) // else + // 'a.*' matches 'ab' => 'a.*' == 'a' + dp[i][j] = dp[i][j - 2];; + } + else + { + if (i > 0 && dp[i - 1][j]) + // 'a*' matches 'aa' => 'a*' == 'a' && 'a' == 'a' + dp[i][j] = s[i - 1] == p[j - 2]; + if (!dp[i][j]) // else + // 'ab*' matches 'a' => 'a' == 'a' + dp[i][j] = dp[i][j - 2];; + } + } + else + { + // 当前pat[i]是[a-z]或者'.',则单字符匹配 + if (i == 0) + // 非空模式串不能匹配空串 + dp[0][j] = false; + else + { + if (p[j - 1] == '.') + { + // 当前为'.' + // 'a.' matches 'aa' => 'a' == 'a' + dp[i][j] = dp[i - 1][j - 1]; + } + else + { + // 当前为[a-z] + // 'ab' matches 'ab' => 'a' == 'a' && 'b' == 'b' + dp[i][j] = dp[i - 1][j - 1] && s[i - 1] == p[j - 1]; + } + } + } + } + } + return dp[slen][plen]; + } +}; +``` + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file From 2347e79e5845ff39697378d95d4c2e85048012b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sun, 22 Jan 2017 00:59:18 +0800 Subject: [PATCH 04/13] Update note MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 简化代码 --- leetcode/10/note_bajdcc.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/leetcode/10/note_bajdcc.md b/leetcode/10/note_bajdcc.md index d20efe0..94c3eeb 100644 --- a/leetcode/10/note_bajdcc.md +++ b/leetcode/10/note_bajdcc.md @@ -242,19 +242,16 @@ public: if (i > 0 && dp[i - 1][j]) // '.*' matches 'abc' => '.*' == 'ab' dp[i][j] = true; - if (!dp[i][j]) // else - // 'a.*' matches 'ab' => 'a.*' == 'a' - dp[i][j] = dp[i][j - 2];; } else { if (i > 0 && dp[i - 1][j]) // 'a*' matches 'aa' => 'a*' == 'a' && 'a' == 'a' dp[i][j] = s[i - 1] == p[j - 2]; - if (!dp[i][j]) // else - // 'ab*' matches 'a' => 'a' == 'a' - dp[i][j] = dp[i][j - 2];; } + if (!dp[i][j]) // retry + // 'a.*' matches 'a' => 'a' == 'a' + dp[i][j] = dp[i][j - 2];; } else { @@ -290,4 +287,4 @@ public: ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 From d413059a5b83c041473ad9c0c3f1e0d620b7d940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sun, 22 Jan 2017 23:47:15 +0800 Subject: [PATCH 05/13] Add note First Missing Positive --- leetcode/41/note_bajdcc.md | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 leetcode/41/note_bajdcc.md diff --git a/leetcode/41/note_bajdcc.md b/leetcode/41/note_bajdcc.md new file mode 100644 index 0000000..d2c6bb1 --- /dev/null +++ b/leetcode/41/note_bajdcc.md @@ -0,0 +1,90 @@ +## 链接 + +41.First Missing Positive(Hard) + +## 题目 + +Given an unsorted integer array, find the first missing positive integer. + +For example, +Given `[1,2,0]` return `3`, +and `[3,4,-1,1]` return `2`. + +Your algorithm should run in *O*(*n*) time and uses constant space. + + + +## 方法一:排序查缺法 + +思路:将原数组排序,然后从前向后查找缺漏的,不过此方法复杂度为线性对数阶,不满足题意,但也能AC。 + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + +class Solution { +public: + int firstMissingPositive(vector &nums) { + sort(nums.begin(), nums.end()); + auto min = 1; + for (auto &n : nums) { + if (n > 0) { + if (n == min) + min++; + else if (n > min) + return min; + } + } + return min; + } +}; +``` + +## 方法二:按位置换法 + +听说过希尔伯特旅馆悖论吧?那么这里,从前往后遍历nums,数组大小len,设当前为n。 + +这时思考,如果n<=0或者n>len,那么n必然是无效的。在这个范围内(1~len)的n,把它们放到nums[n-1]的位置上。 + +问题来了:新的位置上又有其他数字怎么办呢?答案是用递归去解决。递归解决后然后再置换。 + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + +class Solution { +public: + int firstMissingPositive(vector &nums) { + for (auto &n : nums) { + firstMissingPositive(nums, n); + } + for (int i = 0; i < nums.size(); ++i) { + if (nums[i] != -2333) + return i + 1; + } + return nums.size() + 1; + } + + void firstMissingPositive(vector &nums, int& n) { + if (n > 0 && n <= nums.size()) { + auto j = nums[n-1], &k = nums[n-1]; + if (j < 1) { + k = -2333; + } else if (j != -2333) { + k = -666; + firstMissingPositive(nums, j); + k = -2333; + } + } + } +}; +``` + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file From b768dcd7f6c11e821546e332ea2ba7532eb7c1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Mon, 23 Jan 2017 01:00:03 +0800 Subject: [PATCH 06/13] Add note Valid Number ** DFA table solution --- leetcode/65/note_bajdcc.md | 243 +++++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 leetcode/65/note_bajdcc.md diff --git a/leetcode/65/note_bajdcc.md b/leetcode/65/note_bajdcc.md new file mode 100644 index 0000000..81d3e91 --- /dev/null +++ b/leetcode/65/note_bajdcc.md @@ -0,0 +1,243 @@ +## 链接 + +65.Valid Number(Hard) + +## 题目 + +Validate if a given string is numeric. + +Some examples: +`"0"` => `true` +`" 0.1 "` => `true` +`"abc"` => `false` +`"1 a"` => `false` +`"2e10"` => `true` + +**Note:** It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. + +## 思路 + +这是数字的匹配,一般涉及编译原理的题目都是hard难度。一般而言,可以采用多重嵌套if判断的方法解决,不过这样没有挑战性,代码也丑,能不能另辟蹊径呢? + +**下面的方法非典型,初学者会感到吃力**。要学会这种方法,需要熟练掌握正则表达式的使用,以及一定的编译原理基础。 + +正则表达式可以表示3型文法,它有一定的语法结构,这样不细说。像电子邮件、网址、电话号码、用户名等都可以用正则表达式进行匹配。而正则表达式常用的功能也就是匹配或者捕获。 + +这里,数字的正则表达式是`[ ]*[+-]?(\d*\.?\d+|\d+\.?\d*)([e][+-]?\d+)?[ ]*`。 + +既然是3型文法,那就可以用确定性有限自动机来表示。根据常用方法,要先画出NFA,然后去epsilon边,再进行最小化,那么这里不可能写出这么复杂的代码,因此我偷懒了 :) + +参见自己的(解释库)[https://github.com/bajdcc/jMinilang]中的正则匹配部分`priv.bajdcc.util.lexer.test.TestRegex`,直接运行它,然后输入上述正则表达式,那么具体信息就出来了。 + +详细信息(程序自动生成): + +> #### 正则表达式语法树 #### +> 序列 { +> 循环{0,-1} { +> 字符 [\u0020,' '] +> } +> 循环{0,1} { +> 字符 [\u002b,'+'],[\u002d,'-'] +> } +> 分支 { +> 序列 { +> 循环{0,-1} { +> 字符 [\u0030,'0']-[\u0039,'9'] +> } +> 循环{0,1} { +> 字符 [\u002e,'.'] +> } +> 循环{1,-1} { +> 字符 [\u0030,'0']-[\u0039,'9'] +> } +> } +> 序列 { +> 循环{1,-1} { +> 字符 [\u0030,'0']-[\u0039,'9'] +> } +> 循环{0,1} { +> 字符 [\u002e,'.'] +> } +> 循环{0,-1} { +> 字符 [\u0030,'0']-[\u0039,'9'] +> } +> } +> } +> 循环{0,1} { +> 序列 { +> 字符 [\u0065,'e'] +> 循环{0,1} { +> 字符 [\u002b,'+'],[\u002d,'-'] +> } +> 循环{1,-1} { +> 字符 [\u0030,'0']-[\u0039,'9'] +> } +> } +> } +> 循环{0,-1} { +> 字符 [\u0020,' '] +> } +> } + +> #### 状态集合 #### +> [\u0020,' '] +> [\u002b,'+'] +> [\u002d,'-'] +> [\u002e,'.'] +> [\u0030,'0']-[\u0039,'9'] +> [\u0065,'e'] + +> #### 最小化 #### +> 状态[0] => 0, +> 边 => [1] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 边 => [0] +> 类型 => 字符区间 [\u0020,' '] +> 边 => [2] +> 类型 => 字符区间 [\u002e,'.'] +> 边 => [3] +> 类型 => 字符区间 [\u002b,'+'] +> 边 => [3] +> 类型 => 字符区间 [\u002d,'-'] +> 状态[1][结束] => 3,4,6, +> 边 => [4] +> 类型 => 字符区间 [\u002e,'.'] +> 边 => [5] +> 类型 => 字符区间 [\u0065,'e'] +> 边 => [6] +> 类型 => 字符区间 [\u0020,' '] +> 边 => [1] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 状态[2] => 5, +> 边 => [7] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 状态[3] => 2, +> 边 => [2] +> 类型 => 字符区间 [\u002e,'.'] +> 边 => [1] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 状态[4][结束] => 5,8, +> 边 => [5] +> 类型 => 字符区间 [\u0065,'e'] +> 边 => [6] +> 类型 => 字符区间 [\u0020,' '] +> 边 => [4] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 状态[5] => 10, +> 边 => [8] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 边 => [9] +> 类型 => 字符区间 [\u002b,'+'] +> 边 => [9] +> 类型 => 字符区间 [\u002d,'-'] +> 状态[6][结束] => 11, +> 边 => [6] +> 类型 => 字符区间 [\u0020,' '] +> 状态[7][结束] => 6, +> 边 => [5] +> 类型 => 字符区间 [\u0065,'e'] +> 边 => [7] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 边 => [6] +> 类型 => 字符区间 [\u0020,' '] +> 状态[8][结束] => 14, +> 边 => [6] +> 类型 => 字符区间 [\u0020,' '] +> 边 => [8] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +> 状态[9] => 13, +> 边 => [8] +> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] + +> #### 状态转移矩阵 #### +> 0 3 3 2 1 -1 +> 6 -1 -1 4 1 5 +> -1 -1 -1 -1 7 -1 +> -1 -1 -1 2 1 -1 +> 6 -1 -1 -1 4 5 +> -1 9 9 -1 8 -1 +> 6 -1 -1 -1 -1 -1 +> 6 -1 -1 -1 7 5 +> 6 -1 -1 -1 8 -1 +> -1 -1 -1 -1 8 -1 + +## 代码 + +利用DFA状态转移表进行匹配。 + + +```c++ + +//代码放在这个块里面,可以高亮关键字 +class Solution { + inline int getCharMap(const char& c) { + switch (c) { + case ' ': + return 0; + case '+': + return 1; + case '-': + return 2; + case '.': + return 3; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return 4; + case 'e': + return 5; + } + return -1; + } + +public: + bool isNumber(string s) { + using t = int(*)[6]; + int mm[] = { + 0 ,3 ,3 ,2 ,1 ,-1, + 6 ,-1 ,-1 ,4 ,1 ,5, + -1 ,-1 ,-1 ,-1 ,7 ,-1, + -1 ,-1 ,-1 ,2 ,1 ,-1, + 6 ,-1 ,-1 ,-1 ,4 ,5, + -1 ,9 ,9 ,-1 ,8 ,-1, + 6 ,-1 ,-1 ,-1 ,-1 ,-1, + 6 ,-1 ,-1 ,-1 ,7 ,5, + 6 ,-1 ,-1 ,-1 ,8 ,-1, + -1 ,-1 ,-1 ,-1 ,8 ,-1, + }; + auto m = (t)mm; + bool final[] = {0, 1, 0, 0, 1, 0, 1, 1, 1, 0}; + int status = 0; + auto c = s.c_str(); + for (;;) { + auto local = *c++; + int charClass = getCharMap(local); + int refer = -1; + if (charClass != -1) { + refer = m[status][charClass]; + } + if (refer == -1) { + return local == 0 && final[status]; + } else { + status = refer; + } + } + } +}; + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file From bc04d1b57791f75789e928b605c2ef732187339d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Mon, 23 Jan 2017 01:04:53 +0800 Subject: [PATCH 07/13] Change typo --- leetcode/65/note_bajdcc.md | 263 +++++++++++++++++++------------------ 1 file changed, 132 insertions(+), 131 deletions(-) diff --git a/leetcode/65/note_bajdcc.md b/leetcode/65/note_bajdcc.md index 81d3e91..3cc5c9a 100644 --- a/leetcode/65/note_bajdcc.md +++ b/leetcode/65/note_bajdcc.md @@ -27,139 +27,140 @@ Some examples: 既然是3型文法,那就可以用确定性有限自动机来表示。根据常用方法,要先画出NFA,然后去epsilon边,再进行最小化,那么这里不可能写出这么复杂的代码,因此我偷懒了 :) -参见自己的(解释库)[https://github.com/bajdcc/jMinilang]中的正则匹配部分`priv.bajdcc.util.lexer.test.TestRegex`,直接运行它,然后输入上述正则表达式,那么具体信息就出来了。 +参见自己的[解释库](https://github.com/bajdcc/jMinilang)中的正则匹配部分`priv.bajdcc.util.lexer.test.TestRegex`,直接运行它,然后输入上述正则表达式,那么具体信息就出来了。 详细信息(程序自动生成): -> #### 正则表达式语法树 #### -> 序列 { -> 循环{0,-1} { -> 字符 [\u0020,' '] -> } -> 循环{0,1} { -> 字符 [\u002b,'+'],[\u002d,'-'] -> } -> 分支 { -> 序列 { -> 循环{0,-1} { -> 字符 [\u0030,'0']-[\u0039,'9'] -> } -> 循环{0,1} { -> 字符 [\u002e,'.'] -> } -> 循环{1,-1} { -> 字符 [\u0030,'0']-[\u0039,'9'] -> } -> } -> 序列 { -> 循环{1,-1} { -> 字符 [\u0030,'0']-[\u0039,'9'] -> } -> 循环{0,1} { -> 字符 [\u002e,'.'] -> } -> 循环{0,-1} { -> 字符 [\u0030,'0']-[\u0039,'9'] -> } -> } -> } -> 循环{0,1} { -> 序列 { -> 字符 [\u0065,'e'] -> 循环{0,1} { -> 字符 [\u002b,'+'],[\u002d,'-'] -> } -> 循环{1,-1} { -> 字符 [\u0030,'0']-[\u0039,'9'] -> } -> } -> } -> 循环{0,-1} { -> 字符 [\u0020,' '] -> } -> } - -> #### 状态集合 #### -> [\u0020,' '] -> [\u002b,'+'] -> [\u002d,'-'] -> [\u002e,'.'] -> [\u0030,'0']-[\u0039,'9'] -> [\u0065,'e'] - -> #### 最小化 #### -> 状态[0] => 0, -> 边 => [1] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 边 => [0] -> 类型 => 字符区间 [\u0020,' '] -> 边 => [2] -> 类型 => 字符区间 [\u002e,'.'] -> 边 => [3] -> 类型 => 字符区间 [\u002b,'+'] -> 边 => [3] -> 类型 => 字符区间 [\u002d,'-'] -> 状态[1][结束] => 3,4,6, -> 边 => [4] -> 类型 => 字符区间 [\u002e,'.'] -> 边 => [5] -> 类型 => 字符区间 [\u0065,'e'] -> 边 => [6] -> 类型 => 字符区间 [\u0020,' '] -> 边 => [1] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 状态[2] => 5, -> 边 => [7] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 状态[3] => 2, -> 边 => [2] -> 类型 => 字符区间 [\u002e,'.'] -> 边 => [1] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 状态[4][结束] => 5,8, -> 边 => [5] -> 类型 => 字符区间 [\u0065,'e'] -> 边 => [6] -> 类型 => 字符区间 [\u0020,' '] -> 边 => [4] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 状态[5] => 10, -> 边 => [8] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 边 => [9] -> 类型 => 字符区间 [\u002b,'+'] -> 边 => [9] -> 类型 => 字符区间 [\u002d,'-'] -> 状态[6][结束] => 11, -> 边 => [6] -> 类型 => 字符区间 [\u0020,' '] -> 状态[7][结束] => 6, -> 边 => [5] -> 类型 => 字符区间 [\u0065,'e'] -> 边 => [7] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 边 => [6] -> 类型 => 字符区间 [\u0020,' '] -> 状态[8][结束] => 14, -> 边 => [6] -> 类型 => 字符区间 [\u0020,' '] -> 边 => [8] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] -> 状态[9] => 13, -> 边 => [8] -> 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] - -> #### 状态转移矩阵 #### -> 0 3 3 2 1 -1 -> 6 -1 -1 4 1 5 -> -1 -1 -1 -1 7 -1 -> -1 -1 -1 2 1 -1 -> 6 -1 -1 -1 4 5 -> -1 9 9 -1 8 -1 -> 6 -1 -1 -1 -1 -1 -> 6 -1 -1 -1 7 5 -> 6 -1 -1 -1 8 -1 -> -1 -1 -1 -1 8 -1 +```c++ +#### 正则表达式语法树 #### +序列 { + 循环{0,-1} { + 字符 [\u0020,' '] + } + 循环{0,1} { + 字符 [\u002b,'+'],[\u002d,'-'] + } + 分支 { + 序列 { + 循环{0,-1} { + 字符 [\u0030,'0']-[\u0039,'9'] + } + 循环{0,1} { + 字符 [\u002e,'.'] + } + 循环{1,-1} { + 字符 [\u0030,'0']-[\u0039,'9'] + } + } + 序列 { + 循环{1,-1} { + 字符 [\u0030,'0']-[\u0039,'9'] + } + 循环{0,1} { + 字符 [\u002e,'.'] + } + 循环{0,-1} { + 字符 [\u0030,'0']-[\u0039,'9'] + } + } + } + 循环{0,1} { + 序列 { + 字符 [\u0065,'e'] + 循环{0,1} { + 字符 [\u002b,'+'],[\u002d,'-'] + } + 循环{1,-1} { + 字符 [\u0030,'0']-[\u0039,'9'] + } + } + } + 循环{0,-1} { + 字符 [\u0020,' '] + } +} + +#### 状态集合 #### +[\u0020,' '] +[\u002b,'+'] +[\u002d,'-'] +[\u002e,'.'] +[\u0030,'0']-[\u0039,'9'] +[\u0065,'e'] +#### 最小化 #### +状态[0] => 0, + 边 => [1] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] + 边 => [0] + 类型 => 字符区间 [\u0020,' '] + 边 => [2] + 类型 => 字符区间 [\u002e,'.'] + 边 => [3] + 类型 => 字符区间 [\u002b,'+'] + 边 => [3] + 类型 => 字符区间 [\u002d,'-'] +状态[1][结束] => 3,4,6, + 边 => [4] + 类型 => 字符区间 [\u002e,'.'] + 边 => [5] + 类型 => 字符区间 [\u0065,'e'] + 边 => [6] + 类型 => 字符区间 [\u0020,' '] + 边 => [1] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +状态[2] => 5, + 边 => [7] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +状态[3] => 2, + 边 => [2] + 类型 => 字符区间 [\u002e,'.'] + 边 => [1] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +状态[4][结束] => 5,8, + 边 => [5] + 类型 => 字符区间 [\u0065,'e'] + 边 => [6] + 类型 => 字符区间 [\u0020,' '] + 边 => [4] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +状态[5] => 10, + 边 => [8] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] + 边 => [9] + 类型 => 字符区间 [\u002b,'+'] + 边 => [9] + 类型 => 字符区间 [\u002d,'-'] +状态[6][结束] => 11, + 边 => [6] + 类型 => 字符区间 [\u0020,' '] +状态[7][结束] => 6, + 边 => [5] + 类型 => 字符区间 [\u0065,'e'] + 边 => [7] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] + 边 => [6] + 类型 => 字符区间 [\u0020,' '] +状态[8][结束] => 14, + 边 => [6] + 类型 => 字符区间 [\u0020,' '] + 边 => [8] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] +状态[9] => 13, + 边 => [8] + 类型 => 字符区间 [\u0030,'0']-[\u0039,'9'] + +#### 状态转移矩阵 #### + 0 3 3 2 1 -1 + 6 -1 -1 4 1 5 + -1 -1 -1 -1 7 -1 + -1 -1 -1 2 1 -1 + 6 -1 -1 -1 4 5 + -1 9 9 -1 8 -1 + 6 -1 -1 -1 -1 -1 + 6 -1 -1 -1 7 5 + 6 -1 -1 -1 8 -1 + -1 -1 -1 -1 8 -1 +``` ## 代码 @@ -240,4 +241,4 @@ public: ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 From ba1e5c98988ae9b24c5881110e3af6239c5d0db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:37:27 +0800 Subject: [PATCH 08/13] Update README.md --- README.md | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/README.md b/README.md index 74e9c02..0b31c81 100644 --- a/README.md +++ b/README.md @@ -4,38 +4,6 @@ 2016年12月05日我们将开始第一次活动,大家一起来完成LeetCode第09题,并把解题过程记录下来,Pull到这个仓库。 -# 活动记录 - -## 20161215 百人刷题活动 - -这次刷Leetcode第13题 - -https://leetcode.com/problems/roman-to-integer/ - -大家可以往 leetcode/13 下pull代码 - -https://github.com/githubwoniu/learnprogram/tree/master/leetcode/13 - -## 20161205 百人刷题活动 - -学习群里的小伙伴们经常问我,为什么工作四年之后还要学习C++。 - -其实学习C++并不是我的最终目的,我是希望借助C++来系统的学习一下编程这门学科。 - -100天C++学习计划只是我的第一步。 - -截止到今天(20161204)已经是执行计划的第51天了,我的收获有: -1. 完成了前6章和第8章 -2. 分享了30多篇读书笔记到知乎和个人公众号 -3. 坚持6点半起床,早起已经不会有任何的不适 -4. 知乎有1490个关注,公众号有248个关注,c++学习群刚好100人,这些都是学习上的陪伴,可以经常一起探讨问题,分享学习方法,相互监督。 - -也是为了记念学习群满100人,和小伙伴们商量组织一个活动:百人刷leetcode大作战!! - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/100leetcode.png) - -欢迎大家转发邀请卡,邀请好友一起学习,一起进步。 - ### 具体的GitHub操作如下 #### 1. Fork From f5801dceb4727e41d7917d14b24f230386525ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:44:37 +0800 Subject: [PATCH 09/13] =?UTF-8?q?Create=20=E8=AF=B4=E6=98=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "leetcode/20/\350\257\264\346\230\216.md" | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "leetcode/20/\350\257\264\346\230\216.md" diff --git "a/leetcode/20/\350\257\264\346\230\216.md" "b/leetcode/20/\350\257\264\346\230\216.md" new file mode 100644 index 0000000..b22c1ec --- /dev/null +++ "b/leetcode/20/\350\257\264\346\230\216.md" @@ -0,0 +1,48 @@ +### 具体的GitHub操作如下 + +#### 1. Fork + +https://github.com/githubwoniu/learnprogram + +#### 2. 创建一个新文件 + +1. 进入自己的仓库 +2. 进入目录learnprogram/leetcode/09/ +3. 点击Create New File +4. 文件名可以是`note_用户名.md`的形式,如`note_githubwoniu.md` + +#### 3. 添加解题笔记 + +这次要解的题目是 + +[leetcode 20](https://leetcode.com/problems/valid-parentheses/#/description) + +笔记格式参考 + +note.md + +**不要修改此文件** + +#### 4. Commit + +#### 5. Pull Request + +# 更多github操作指南 + +请查看,仓库首页的文章 + +https://github.com/githubwoniu/learnprogram + +# 注意 + +公众号随机选取本仓库的提交供大家吐糟,以便相互学习提高。 + +凡提交到本仓库的文章即认为授权公众号使用 + +# 关注公众号 + +一起学习吧~ + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +由于微信群二维码只有七天有效期,这里就不放了,请到公众号底部菜单栏查找。 From 1696fc81ae1175399d794843b01c85ba5f4dd4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:49:03 +0800 Subject: [PATCH 10/13] =?UTF-8?q?Create=20=E7=AD=94=E9=A2=98=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=5F=E4=B8=8D=E8=A6=81=E4=BF=AE=E6=94=B9=E6=AD=A4?= =?UTF-8?q?=E6=96=87=E4=BB=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\346\255\244\346\226\207\344\273\266.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" diff --git "a/\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" "b/\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" new file mode 100644 index 0000000..99d9a83 --- /dev/null +++ "b/\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" @@ -0,0 +1,48 @@ +## 链接 + + +这里更新本次题目的链接 + + +## 题目 + +这里对题目做出翻译 + + + +## 释义 + +对该题目的理解 + + + + +## 补充描述 + + +有其他思路等可以写在这里 + + + +## 代码 + + +具体的实现代码放这,如果有多个实现,可复制该块多份,并添加编号,如“## 代码1” + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 From fe7c9b3bf49834c149d55df7e53d70c824489cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:50:38 +0800 Subject: [PATCH 11/13] =?UTF-8?q?Update=20=E8=AF=B4=E6=98=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "leetcode/20/\350\257\264\346\230\216.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/leetcode/20/\350\257\264\346\230\216.md" "b/leetcode/20/\350\257\264\346\230\216.md" index b22c1ec..692da96 100644 --- "a/leetcode/20/\350\257\264\346\230\216.md" +++ "b/leetcode/20/\350\257\264\346\230\216.md" @@ -19,7 +19,7 @@ https://github.com/githubwoniu/learnprogram 笔记格式参考 -note.md +[答题格式](https://github.com/githubwoniu/learnprogram/blob/master/%E7%AD%94%E9%A2%98%E6%A0%BC%E5%BC%8F_%E4%B8%8D%E8%A6%81%E4%BF%AE%E6%94%B9%E6%AD%A4%E6%96%87%E4%BB%B6.md) **不要修改此文件** From 35204b1b8f70c43b59c6f82c2472b2ee7781256d Mon Sep 17 00:00:00 2001 From: NashWong <791745839@qq.com> Date: Sat, 3 Jun 2017 13:25:24 +0800 Subject: [PATCH 12/13] Create node_Wanghonglu.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不太会用github,代码写的也有点low,不要见笑。。。。 --- leetcode/20/node_Wanghonglu.md | 112 +++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 leetcode/20/node_Wanghonglu.md diff --git a/leetcode/20/node_Wanghonglu.md b/leetcode/20/node_Wanghonglu.md new file mode 100644 index 0000000..45d5a4d --- /dev/null +++ b/leetcode/20/node_Wanghonglu.md @@ -0,0 +1,112 @@ +class Solution { +public: + bool isValid(string s) { + char elem=0,left_elem=0,right_elem=0; + Stack *ptr = new Stack( (int)s.length() ); + for( int i=0;iPushStack(elem); + break; + case ']': + case ')': + case '}': + { + ptr->GetTopStack(left_elem); + switch( left_elem ) + { + case '[': + right_elem = ']'; + break; + case '(': + right_elem = ')'; + break; + case '{': + right_elem = '}'; + break; + } + if( right_elem == elem ) + { + ptr->PopStack(); + break; + } + else + { + delete ptr; + return false; + } + } + default: + { + delete ptr; + return false; + } + + } + } + if(ptr->IsEmptyStack()) + { + delete ptr; + return true; + } + delete ptr; + return false; + } +class Stack{ +public: + Stack( int size ) + { + m_size = size; + m_ptr = new char[size]; + memset( m_ptr, 0x00, size ); + m_top = 0; + } + ~Stack() + { + delete []m_ptr; + m_top = 0; + m_size = 0; + } + bool IsFullStack() + { + return m_size == m_top; + } + bool IsEmptyStack() + { + return m_top ==0; + } + bool PushStack( char elem ) + { + if( IsFullStack() ) + return false; + m_ptr[m_top] = elem; + m_top++; + return true; + } + bool PopStack( ) + { + if( IsEmptyStack()) + return false; + m_top--; + return true; + } + bool GetTopStack( char& elem ) + { + if( IsEmptyStack() ) + return false; + elem = m_ptr[m_top-1]; + return true; + } +private: + char* m_ptr; + int m_top; + int m_size; +}; + + +}; From 7e9d63649bcb28fc6cdd94af3c3a09e642754881 Mon Sep 17 00:00:00 2001 From: An Chen Date: Sat, 3 Jun 2017 14:01:33 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/20/node_Wanghonglu.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/leetcode/20/node_Wanghonglu.md b/leetcode/20/node_Wanghonglu.md index 45d5a4d..180008e 100644 --- a/leetcode/20/node_Wanghonglu.md +++ b/leetcode/20/node_Wanghonglu.md @@ -1,3 +1,4 @@ +```c++ class Solution { public: bool isValid(string s) { @@ -110,3 +111,4 @@ private: }; +```