Home > Back-end >  Java palindrome
Java palindrome

Time:12-08

The Description
Good post for the first time, the eldest brother eldest sister-in-law for

To determine whether an integer palindrome, palindrome is the correct order (from left to right) and reverse (read from right to left) is the same integer,


Input
The input contains only a string


The Output
If it is a palindrome, output output true, otherwise false
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The first thought:
import java.util.Scanner;

Public class Main {
Public static void main (String [] args) {
Scanner sc=new Scanner(System.in);
//the user to enter an integer
Int shu1=sc. NextInt ();
//integer can be converted to string
String shu=Integer. ToString (shu1);
//define two Pointers, one refers to the first word, refers to the second word
Int l=0;
Int r=shu. Length () - 1;
//assume that this number is a palindrome
Boolean bool=true;
//will be the first and the last one, and then loop,
While (lIf (shu. CharAt (l)!=shu. CharAt (r)) {
Bool=false;
L++;
R -;
}
}
System. The out. Println (bool);
}
}
The second thought

This is a kind of mathematical thought

CodePudding user response:

 
String STR="XXX".
Int length=STR. Length ();
Int index=length/2;
Strings str1=STR. The substring (0, index);
String str2=STR. The substring ((length & amp; 1)==1? Index + 1: index);
String str3=new StringBuilder (str2). Reverse (). The toString ();
System. The out. Println (str1. Equals (str3));
  • Related