Showing
2 changed files
with
45 additions
and
1 deletions
1078-findOcurrences.js
0 → 100644
| 1 | +/** | ||
| 2 | + * @param {string} text | ||
| 3 | + * @param {string} first | ||
| 4 | + * @param {string} second | ||
| 5 | + * @return {string[]} | ||
| 6 | + */ | ||
| 7 | +var findOcurrences2 = function(text, first, second) { | ||
| 8 | + let target = `${first} ${second} `; | ||
| 9 | + let rs = []; | ||
| 10 | + | ||
| 11 | + while (text) { | ||
| 12 | + let i = text.indexOf(target); | ||
| 13 | + if (i >= 0) { | ||
| 14 | + console.info(text, i); | ||
| 15 | + text = text.substr(i + target.length); | ||
| 16 | + console.info('==>', text); | ||
| 17 | + let m = /^(\w+)\b.*/.exec(text); | ||
| 18 | + if (m) { | ||
| 19 | + rs.push(m[1]); | ||
| 20 | + } | ||
| 21 | + } else { | ||
| 22 | + break; | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + return rs; | ||
| 27 | +}; | ||
| 28 | + | ||
| 29 | +var findOcurrences = function(text, first, second) { | ||
| 30 | + let words = text.split(' '); | ||
| 31 | + let rs = []; | ||
| 32 | + | ||
| 33 | + for (let i = 0; i < words.length - 2; i++) { | ||
| 34 | + if (words[i] === first && words[i+1] === second && words[i+2]) { | ||
| 35 | + rs.push(words[i+2]); | ||
| 36 | + i++; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + return rs; | ||
| 41 | +}; | ||
| 42 | + | ||
| 43 | +console.info(findOcurrences("we will we will rock you", "we", "will")); | ||
| 44 | +console.info(findOcurrences("alice is a good girl she is a good student", "a", "good")); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -1073,7 +1073,7 @@ leetcode-cn.com上的题库代码实现 | ... | @@ -1073,7 +1073,7 @@ leetcode-cn.com上的题库代码实现 |
| 1073 | | | 1072 | [按列翻转得到最大值等行数](https://leetcode-cn.com/problems/flip-columns-for-maximum-number-of-equal-rows) | **中等** | | 1073 | | | 1072 | [按列翻转得到最大值等行数](https://leetcode-cn.com/problems/flip-columns-for-maximum-number-of-equal-rows) | **中等** | |
| 1074 | | | 1073 | [负二进制数相加](https://leetcode-cn.com/problems/adding-two-negabinary-numbers) | **中等** | | 1074 | | | 1073 | [负二进制数相加](https://leetcode-cn.com/problems/adding-two-negabinary-numbers) | **中等** | |
| 1075 | | | 1074 | [元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target) | **困难** | | 1075 | | | 1074 | [元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target) | **困难** | |
| 1076 | -| | 1078 | [Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram) | **简单** | | 1076 | +| 1078-findOcurrences.js | 1078 | [Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram) | **简单** | |
| 1077 | | | 1079 | [活字印刷](https://leetcode-cn.com/problems/letter-tile-possibilities) | **中等** | | 1077 | | | 1079 | [活字印刷](https://leetcode-cn.com/problems/letter-tile-possibilities) | **中等** | |
| 1078 | | | 1080 | [根到叶路径上的不足节点](https://leetcode-cn.com/problems/insufficient-nodes-in-root-to-leaf-paths) | **中等** | | 1078 | | | 1080 | [根到叶路径上的不足节点](https://leetcode-cn.com/problems/insufficient-nodes-in-root-to-leaf-paths) | **中等** | |
| 1079 | | | 1081 | [不同字符的最小子序列](https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters) | **中等** | | 1079 | | | 1081 | [不同字符的最小子序列](https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters) | **中等** | |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment