-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtwoNums.js
More file actions
65 lines (55 loc) · 1.74 KB
/
twoNums.js
File metadata and controls
65 lines (55 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 给出一个整数数组, 请在数组中找出两个加起来等于目标值的数,
* 你给出的函数twoSum 需要返回这两个数字的下标( index1, index2), 需要满足 index1 小于index2.。注意: 下标是从1开始的
* 假设给出的数组中只存在唯一解
*/
export function captureThreeNumbers(str) {
// 切割转化
const arr = str.split('')
const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for (let index = 0; index < arr.length - 1; index++) {
if (num.includes(arr[index]) && num.includes(arr[index + 1]) && num.includes(arr[index + 2])) {
return Number.parseInt(`${arr[index]}${arr[index + 1]}${arr[index + 2]}`)
}
}
return false
}
console.log(captureThreeNumbers('abc123'))
export function matchesPattern(str) {
if (str.length !== 12) {
return false
}
// console.log(str.slice(2, 3), str.slice(6, 7))
if (str.slice(3, 4) !== '-' || str.slice(7, 8) !== '-') {
return false
}
const arr = str.split('-')
console.log(arr)
if (arr.length !== 3) {
return false
}
const [low, mid, high] = arr
const rule = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
const lowArr = low.split('')
const midArr = mid.split('')
const highArr = high.split('')
console.log(lowArr, midArr, highArr)
// 逐一比对
for (let index = 0; index < lowArr.length; index++) {
if (!rule.includes(lowArr[index])) {
return false
}
}
for (let index = 0; index < midArr.length; index++) {
if (!rule.includes(midArr[index])) {
return false
}
}
for (let index = 0; index < highArr.length; index++) {
if (!rule.includes(highArr[index])) {
return false
}
}
return true
}
console.log(matchesPattern('800-555-1212'))