使用栈判断给定字符串是否是回文的算法
.dataStore = [];
.top = 0;
.push = push;
.pop = pop;
.peek = peek;
.clear = clear;
.length = length;
<span style="color: #0000ff;">function<span style="color: #000000;"> push(element){
<span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top++] =<span style="color: #000000;"> element;
} <span style="color: #0000ff;">function<span style="color: #000000;"> pop(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[--<span style="color: #0000ff;">this<span style="color: #000000;">.top];
} <span style="color: #0000ff;">function<span style="color: #000000;"> peek(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top-1<span style="color: #000000;">];
} <span style="color: #0000ff;">function<span style="color: #000000;"> clear(){
<span style="color: #0000ff;">this.top = 0<span style="color: #000000;">;
} <span style="color: #0000ff;">function<span style="color: #000000;"> length(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this<span style="color: #000000;">.top;
} <span style="color: #008000;">/<span style="color: #008000;">使用栈判断给定字符串是否是回文的算法<span style="color: #008000;">/
<span style="color: #0000ff;">function<span style="color: #000000;"> isPalindrome(word){
<span style="color: #0000ff;">var s = <span style="color: #0000ff;">new<span style="color: #000000;"> stack();
<span style="color: #0000ff;">for(<span style="color: #0000ff;">var i = 0;i < word.length;i++<span style="color: #000000;">){
s.push(word[i]);
}
<span style="color: #0000ff;">var rword = ""<span style="color: #000000;">;
<span style="color: #0000ff;">while(s.length() > 0<span style="color: #000000;">){
rword +=<span style="color: #000000;"> s.pop();
}
<span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top++] =<span style="color: #000000;"> element;
} <span style="color: #0000ff;">function<span style="color: #000000;"> pop(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[--<span style="color: #0000ff;">this<span style="color: #000000;">.top];
} <span style="color: #0000ff;">function<span style="color: #000000;"> peek(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top-1<span style="color: #000000;">];
} <span style="color: #0000ff;">function<span style="color: #000000;"> clear(){
<span style="color: #0000ff;">this.top = 0<span style="color: #000000;">;
} <span style="color: #0000ff;">function<span style="color: #000000;"> length(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this<span style="color: #000000;">.top;
} <span style="color: #008000;">/<span style="color: #008000;">使用栈判断给定字符串是否是回文的算法<span style="color: #008000;">/
<span style="color: #0000ff;">function<span style="color: #000000;"> isPalindrome(word){
<span style="color: #0000ff;">var s = <span style="color: #0000ff;">new<span style="color: #000000;"> stack();
<span style="color: #0000ff;">for(<span style="color: #0000ff;">var i = 0;i < word.length;i++<span style="color: #000000;">){
s.push(word[i]);
}
<span style="color: #0000ff;">var rword = ""<span style="color: #000000;">;
<span style="color: #0000ff;">while(s.length() > 0<span style="color: #000000;">){
rword +=<span style="color: #000000;"> s.pop();
}
</span><span style="color: #0000ff;">if</span>(word ==<span style="color: #000000;"> rword){
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}
}
<span style="color: #0000ff;">var word1 = "racecar"<span style="color: #000000;">;
<span style="color: #0000ff;">if<span style="color: #000000;">(isPalindrome(word1)){
console.log(word1 + " is a palindrome")<span style="color: #008000;">//<span style="color: #008000;">racecar is a palindrome
}
原文链接:https://www.f2er.com/js/403384.html