Algorithm-palindrome decryption, determine whether a vegetarian group is a palindrome

Source: Internet
Author: User

Algorithm, the queue is the FIFO principle, and the stack is the LIFO principle, the stack limit can only be inserted and deleted at one end, and what is the role of the stack?

Can be seen through a set of palindrome strings: "Xyzyx", with the stack to determine whether the string is a palindrome

Case: package test;

/** * @authorDayu Decryption Palindrome--Stack *@versioncreated: November 13, 2017 2:15:01 * Class Description*/ Public classzhan{//Palindrome--there must be an axis of symmetry, so it must be singular.     Public Static voidMain (string[] args) {String str= "Azhohza";//given a random set, you can        intlen=str.length (); intMID = LEN/2;//get midpoint positionSystem.out.println ("Mid=" +mid); //takes two array chara,charb, length is the length of a given string        byte[] bytes =str.getbytes (); Char[] Charb =New Char[Len]; Char[] Chara =New Char[Len]; //Charb array all assigned values         for(inti = 0; i < Len; i++) {Charb[i]= (Char) bytes[i]; }        //Initialize the stack, top=0, then put all values before the midpoint into the stack        inttop = 0;  for(inti = 0; I < mid; i++) {chara[++top] = (Char) Charb[i]; System.out.print ("-"+Charb[i]); } System.out.println ("\ntop===" +top);
//Judging by the symmetry of the center, if top=0, all characters in the stack are one by one matched.
for(inti = mid + 1; i < Len; i++) { if(Charb[i]! =Chara[top]) { Break; } Top--; } if(top = = 0) System.out.println ("It's a palindrome."); ElseSystem.out.println ("Not a palindrome."); }}

Algorithm-palindrome decryption, determine whether a vegetarian group is a palindrome

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.