菜鸟前端学算法
function isPalidrome(word){ var s=new Stack();for(var i=0;i<word.length;i++){
s.push(word)
}
var rword="";
while(s.length()>0){
rword +=s.pop()
}
if(word==rword){
return true
}else{
return false
}
}
//用js构建stack
var Stack=function(){
this.dataStore=[];
this.top=0;
};
Stack.prototype.push=function(element){
return this.dataStore.push(element);
}
Stack.prototype.pop=function(){
return this.dataStore.pop();
}
Stack.prototype.peek=function(){
return this.dataStore;
}
Stack.prototype.length=function(){
return this.dataStore.length;
}
Stack.prototype.isEmpty=function(){
return this.dataStore.length===0;
}
Stack.prototype.seeAll=function(){
return this.dataStore.join("\n");
}
页:
[1]