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