add: 只出现一次的数字等
This commit is contained in:
20
src/string/compare-strings.js
Normal file
20
src/string/compare-strings.js
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @param A: A string
|
||||
* @param B: A string
|
||||
* @return: if string A contains all of the characters in B return true else return false
|
||||
*/
|
||||
export const compareStrings = function (A, B) {
|
||||
const a = A.split('')
|
||||
const b = B.split('')
|
||||
|
||||
for (const n in b) {
|
||||
const tmp = a.indexOf(b[n])
|
||||
if (tmp === -1) {
|
||||
return false
|
||||
} else {
|
||||
a.splice(tmp, 1)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
7
src/string/contains-duplicate.js
Normal file
7
src/string/contains-duplicate.js
Normal file
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const containsDuplicate = function (nums) {
|
||||
return new Set(nums).size !== nums.length
|
||||
}
|
67
src/string/count-binary-substrings.js
Normal file
67
src/string/count-binary-substrings.js
Normal file
@ -0,0 +1,67 @@
|
||||
// LeetCode 696. 计数二进制子串 https://leetcode-cn.com/problems/count-binary-substrings/
|
||||
// LintCode 1079. 连续子串计数 https://www.lintcode.com/problem/count-binary-substrings/description
|
||||
|
||||
// 效率低
|
||||
// export default (s) => {
|
||||
// let count = 0
|
||||
// for (let n = 0, len = s.length; n < len; n++) {
|
||||
// let last = s.charAt(n)
|
||||
// let time = 0
|
||||
// let lastTime = 0
|
||||
// for (let i = n + 1; i < len; i++) {
|
||||
// if (s.charAt(i) !== last && time === lastTime) {
|
||||
// count++
|
||||
// break
|
||||
// } else if (s.charAt(i) !== last) {
|
||||
// time++
|
||||
// } else if (time === 0) {
|
||||
// lastTime++
|
||||
// } else {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count
|
||||
// }
|
||||
|
||||
// 报RegExp too big
|
||||
// export default (s) => {
|
||||
// // 建立数据结构,堆栈,保存数据
|
||||
// let r = []
|
||||
// // 给定任意子输入都返回第一个符合条件的子串
|
||||
// let match = (s) => {
|
||||
// let j = s.match(/^(0+|1+)/)[0]
|
||||
// let o = (j[0] ^ 1).toString().repeat(j.length)
|
||||
// let reg = new RegExp(`^(${j}${o})`)
|
||||
// if (reg.test(s)) {
|
||||
// return RegExp.$1
|
||||
// } else {
|
||||
// return ''
|
||||
// }
|
||||
// }
|
||||
// // 通过for循环控制程序运行的流程
|
||||
// for (let i = 0, len = s.length - 1; i < len; i++) {
|
||||
// let sub = match(s.slice(i))
|
||||
// if (sub) r.push(sub)
|
||||
// }
|
||||
// return r.length
|
||||
// }
|
||||
|
||||
export default (s) => {
|
||||
// 来自 Somnus
|
||||
// pre 前一个数字连续出现的次数,cur 当前数字连续出现的次数,result 结果子串个数
|
||||
let pre = 0; let cur = 1; let result = 0
|
||||
for (let i = 0, len = s.length - 1; i < len; i++) {
|
||||
// 判断当前数字是否与后一个数字相同
|
||||
if (s[i] === s[i + 1]) { // 相同,则当前数字出现的次数cur加1
|
||||
cur++
|
||||
} else { // 不同,则当前数字事实上变成了前一个数字,当前数字的次数重置为1
|
||||
pre = cur
|
||||
cur = 1
|
||||
}
|
||||
if (pre >= cur) { // 前一个数字出现的次数 >= 后一个数字出现的次数,则一定包含满足条件的子串
|
||||
result++
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
58
src/string/regular-expression-matching.js
Normal file
58
src/string/regular-expression-matching.js
Normal file
@ -0,0 +1,58 @@
|
||||
// LeetCode 10. 正则表达式匹配 https://leetcode-cn.com/problems/regular-expression-matching/
|
||||
// LintCode 154. 正则表达式匹配 https://www.lintcode.com/problem/regular-expression-matching/description
|
||||
|
||||
// 'mississippi', 'mis*is*ip*.' 测试不通过
|
||||
// export default (str, mode) => {
|
||||
// const strLen = str.length
|
||||
// const modeArr = mode.match(/([a-z.]\*)|([a-z]+(?=([a-z.]\*)|$))/g) // 筛选无模式和有模式
|
||||
|
||||
// if (!modeArr) return false
|
||||
|
||||
// let cur = 0
|
||||
// for (let n = 0, len = modeArr.length; n < len; n++) {
|
||||
// // 匹配模式结果分类
|
||||
// // 分三类:.*|a*|bcde
|
||||
// const s = modeArr[n].split('')
|
||||
// if (s[1] === '*') { // 如果第二位是'*',则为有模式
|
||||
// if (s[0] === '.') { // 第一种模式
|
||||
// return true
|
||||
// } else { // 第二种模式,直接移到相等的最后面
|
||||
// while (str[cur] === s[0]) {
|
||||
// cur++
|
||||
// }
|
||||
// }
|
||||
// } else { // 无模式
|
||||
// for (let i = 0, sLen = s.length; i < sLen; i++) {
|
||||
// if (s[i] !== str[cur++]) { // 注意,cur++每一次都会被执行
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cur === strLen
|
||||
// }
|
||||
|
||||
export default (str, mode) => {
|
||||
const isMatch = (s, p) => {
|
||||
// 边界情况,如果s和p都为空,说明处理结束了,返回true,否则返回false
|
||||
if (p.length <= 0) {
|
||||
return !s.length
|
||||
}
|
||||
// 判断p模式字符串的第一个字符和s字符串的第一个字符是不是匹配
|
||||
let match = false
|
||||
if (s.length > 0 && (p[0] === s[0] || p[0] === '.')) {
|
||||
match = true
|
||||
}
|
||||
// p有模式的
|
||||
if (p.length > 1 && p[1] === '*') {
|
||||
// 第一种情况:s*匹配0个字符
|
||||
// 第二种情况:s*匹配1个字符,递归下去,用来表示s*匹配多个s
|
||||
return isMatch(s, p.slice(2)) || (match && isMatch(s.slice(1), p))
|
||||
} else {
|
||||
return match && isMatch(s.slice(1), p.slice(1))
|
||||
}
|
||||
}
|
||||
|
||||
return isMatch(str, mode)
|
||||
}
|
6
src/string/repeated-substring-pattern.js
Normal file
6
src/string/repeated-substring-pattern.js
Normal file
@ -0,0 +1,6 @@
|
||||
// LeetCode 459. 重复的子字符串 https://leetcode-cn.com/problems/repeated-substring-pattern/
|
||||
// LintCode 1227. 重复的子串模式 https://www.lintcode.com/problem/repeated-substring-pattern/description
|
||||
|
||||
export default (str) => {
|
||||
return /^(\w+)\1+$/.test(str)
|
||||
}
|
56
src/string/restore-ip-addresses.js
Normal file
56
src/string/restore-ip-addresses.js
Normal file
@ -0,0 +1,56 @@
|
||||
// LeetCode 93. 复原IP地址 https://leetcode-cn.com/problems/restore-ip-addresses/
|
||||
// LintCode 426. 恢复IP地址 https://www.lintcode.com/problem/restore-ip-addresses/description
|
||||
|
||||
export default (str) => {
|
||||
const result = []
|
||||
|
||||
if (str === '0000') return ['0.0.0.0']
|
||||
|
||||
// 递归函数
|
||||
const recur = (cur, sub) => {
|
||||
if (cur.length === 4 && cur.join('') === str) {
|
||||
if (!cur.every(item => Number(item) === 0)) {
|
||||
result.push(cur.join('.'))
|
||||
}
|
||||
} else {
|
||||
for (let n = 0, len = Math.min(3, sub.length); n < len; n++) {
|
||||
const start = Number(sub.substr(0, n + 1))
|
||||
const end = sub.substr(n + 1)
|
||||
if (start < 256 && end.length <= (9 - cur.length * 3)) {
|
||||
recur([...cur, start], end)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
recur([], str)
|
||||
return result
|
||||
}
|
||||
|
||||
// 快乐动起来老师的解法:
|
||||
// export default (str) => {
|
||||
// // 保存所有符合条件的IP地址
|
||||
// let r = []
|
||||
// // 分四步递归处理ip分段
|
||||
// let search = (cur, sub) => {
|
||||
// // 非法输入过滤,LeetCode测试用例(111111111111111111111111111111111111111111111111111111111111)
|
||||
// if (sub.length > 12) {
|
||||
// return
|
||||
// }
|
||||
// // 边界条件
|
||||
// if (cur.length === 4 && cur.join('') === str) {
|
||||
// r.push(cur.join('.'))
|
||||
// } else {
|
||||
// // 正常的处理过程
|
||||
// for (let i = 0, len = Math.min(3, sub.length), tmp; i < len; i++) {
|
||||
// tmp = sub.substr(0, i + 1)
|
||||
// if (tmp - 256 < 0) {
|
||||
// // 转换下数据类型,如 01为1(LeetCode测试用例)
|
||||
// search(cur.concat([tmp * 1]), sub.substr(i + 1))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// search([], str)
|
||||
// return r
|
||||
// }
|
12
src/string/reverse-words-in-a-string.js
Normal file
12
src/string/reverse-words-in-a-string.js
Normal file
@ -0,0 +1,12 @@
|
||||
// LeetCode 557. 反转字符串中的单词 III https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/
|
||||
// LintCode 1173. 反转字符串 III https://www.lintcode.com/problem/reverse-words-in-a-string-iii/description
|
||||
|
||||
export default (s) => {
|
||||
return s.split(' ').map(item => {
|
||||
let tmp = ''
|
||||
for (let n = item.length; n >= 0; n--) {
|
||||
tmp += item.charAt(n)
|
||||
}
|
||||
return tmp
|
||||
}).join(' ')
|
||||
}
|
3
src/string/unique-characters.js
Normal file
3
src/string/unique-characters.js
Normal file
@ -0,0 +1,3 @@
|
||||
export const isUnique = (str) => {
|
||||
return new Set(str.split('')).size === str.length
|
||||
}
|
Reference in New Issue
Block a user