Determine if a string is a palindrome: use stack &reverse ()

Source: Internet
Author: User

first, the use of stacks
    functionStack () { This. DataStore = [];  This. top = 0;//the value of top is equal to the number of elements in the array         This. Push =push;  This. Pop =Pop; }    functionpush (Element) { This. datastore[ This. top++] =element; }    functionpop () {return  This. datastore[-- This. Top]; }    functionIspalindrome (word) {vars =NewStack ();//creates a new stack so that the elements in the array are pressed into the stack         for(vari = 0; i < word.length; ++i) {s.push (word[i]); }        varRWord = "";  while(S.top > 0) {RWord+ = S.pop ();//flips the elements in the new stack through the Pop () method        }        if(Word = =RWord) {            return true; } Else {            return false; }    }    varWord = "Hello"; if(Ispalindrome (Word)) {alert (Word+ "is a palindrome."); } Else{alert (Word+ "is not a palindrome."); } Word= "Racecar"; if(Ispalindrome (Word)) {alert (Word+ "is a palindrome."); } Else{alert (Word+ "is not a palindrome."); } 
second, indirect use of the reverse () method of the array
     functionIspalindrome (word) {varA = Word.split ("");//Convert the string to an array, split () "" is necessary        varb = A.join ();//Convert an array to a string        varA1 =A.reverse (); varB1 =A1.join (); if(b = = B1) {//string can use "= =", array can not            return true; } Else {            return false; }    }    varWord = "Hello"; if(Ispalindrome (Word)) {alert (Word+ "is a palindrome."); } Else{alert (Word+ "is not a palindrome."); } Word= "Racecar"; if(Ispalindrome (Word)) {alert (Word+ "is a palindrome."); } Else{alert (Word+ "is not a palindrome."); } 

Determine if a string is a palindrome: use stack &reverse ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.