Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4428aec

Browse files
committed
feat: 验证回文字符串2
1 parent 0867cbf commit 4428aec

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

算法/验证回文字符串2.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var validPalindrome = function(s) {
6+
var i = 0;
7+
var j = s.length - 1
8+
9+
// 两个指针往中间缩进
10+
while (i < j && s[i] === s[j]) {
11+
i++
12+
j--
13+
}
14+
15+
// 遇到相对位置不相等了 判断删除一位后的情况
16+
if (isPalindrome(i + 1, j)) {
17+
return true
18+
}
19+
if (isPalindrome(i, j - 1)) {
20+
return true
21+
}
22+
23+
// 工具方法,用于判断字符串是否回文
24+
function isPalindrome(st, ed) {
25+
while(st<ed) {
26+
if(s[st] !== s[ed]) {
27+
return false
28+
}
29+
st++
30+
ed--
31+
}
32+
return true
33+
}
34+
35+
// 这样都不满足 那就不符合要求了
36+
return false
37+
};

0 commit comments

Comments
 (0)