add: 只出现一次的数字等

This commit is contained in:
2019-03-10 16:43:04 +08:00
committed by yi-ge
parent 930ff1be11
commit 8239e7afd5
106 changed files with 7978 additions and 0 deletions

View 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
}

View File

@ -0,0 +1,7 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
export const containsDuplicate = function (nums) {
return new Set(nums).size !== nums.length
}

View 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
}

View 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)
}

View 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)
}

View 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为1LeetCode测试用例
// search(cur.concat([tmp * 1]), sub.substr(i + 1))
// }
// }
// }
// }
// search([], str)
// return r
// }

View 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(' ')
}

View File

@ -0,0 +1,3 @@
export const isUnique = (str) => {
return new Set(str.split('')).size === str.length
}