-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
javascript 解决回文索引问题 #15
Labels
Comments
比较笨的方法,也可以实现
|
|
if (check(word, sample)){ res.push(i); i = i + length} else{ i++ } |
function findAnswer(word, s) { |
function findAnswer(word, s) {
return [...getStr(word, s), ...getStr(reverseString(word), s)];
}
function getStr(word, s) {
let s_copy = s;
let arr = [];
let temp = 0;
while (true) {
let index = s_copy.search(word);
if (index !== -1) {
s_copy = s_copy.slice(index + word.length);
arr.push(temp + index);
temp = temp + index + word.length; //指针,开始在字符串首位置
} else {
break;
}
}
return arr;
}
function reverseString(str) {
var newstr = str.split("").reverse().join("");
return newstr;
}
findAnswer('abc', 'abcxabcbacc'); // [0, 4, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
给定一个单词 word 和一个字符串 S,找到 S 中的所有 word 及其回文(逆序)的起始索引
The text was updated successfully, but these errors were encountered: