Daily one yard - Analyzing palindrome

       A yard daily topic today talking about the judgment of a number is not a palindrome, so-called palindrome is whether it is read from left to right or right to left reading results are the same, say 12321. There are many ways, here to share a few together.

       Mainly from two aspects to solve it: one is the input string as a string, and then use some functions of the string to be processed. Personally I feel that such a method can be credited in mind, during the interview process often encounter a string of programming.

      Another is the integer processing, the highest and lowest bits are compared, so proceed step by step.

 


the first method:


 

{class palindromeNumber public
public static void main (String [] args) {
/ *
* from the console of the input digital call isPalindrome () method for determining outputs the judgment result
* /
Scanner Scanner Scan new new = (the System.in);
System.out.println ( "enter a number:");
string strNum scan.next = ();
// as strings
System.out.println (isPalindrome (strNum));
}

static Boolean isPalindrome public (String STR) {
Boolean = Result to false;
for (int I = 0; I <str.length () / 2; I ++) {
// use the charAt () function returns the index of the character
if (str .charAt (I) == str.charAt (str.length () -. 1 - I)) {
Result = to true;
} the else {
Result = to false;
}
}
return Result;
}
}

      The most critical point of the method is to use the charAt () method for each string of characters corresponding bit decision, if there is a corresponding pair of the characters are not equal, then the result is false.

 


The second method:


 

{class palindromeNumber public
public static void main (String [] args) {
/ * string inverting input digital determination and outputs the judgment result * /
Scanner Scanner Scan new new = (the System.in);
System.out.println ( "Enter number: ");
String strNum1 scan.next = ();
System.out.println (isPalindrome (strNum1));
}

public static boolean isPalindrome(String strNum) {
boolean result = false;
StringBuilder strNum2 = new StringBuilder(strNum);
strNum2.reverse();//字符串反转并判断
for (int i = 0; i < strNum.length() / 2; i++) {
if (strNum.charAt(i) != strNum2.charAt(i)) {
result = false;
} else {
result = true; } }
return result;
}}

The second method is basically the same as the first, but here also use a reverse string () method, knowledge of string also has in-depth analysis on the basis of public numbers in java. Here reminder, String class is immutable, but StringBuilder is mutable sequence of characters. More information inquiry remember Oh!

 


The third method:


 

public class palindromeNumber {
public static void main(String[] args) {
/*
* 从控制台输入数字 调用isPalindrome()方法判断 输出判断结果
*/
Scanner scan = new Scanner(System.in);
System.out.println("请输入数字: ");
int num = scan.nextInt();
System.out.println(

isPalindrome(num));
}

public static boolean isPalindrome(int num) {
//将整数转换成字符串,并反转作比较
return String.valueOf(num).equals(new StringBuffer(num + "").reverse().toString());
}
}

       本方法与第二种方法原理相同,只不过编程实现不同。可见编程魅力所在,代码简洁。但并不是所有简洁的代码都是最有效的,要理解其中的原理深入分析。

 


第四种方法:


 

public class palindromeNumber {
public static void main(String[] args) {
/*
* 从控制台输入数字 调用isPalindrome() 方法判断 输出判断结果
*/
int num;
Scanner scan = new Scanner(System.in);
System.out.println("请输入数字: ");
num = scan.nextInt();
System.out.println(isPalindrome(num));
}

public static boolean isPalindrome(int num) {
boolean result = false;
int[] arr = new int[10];
int i = 0;
// 将数字的每一位输入数组中
while (num > 0) {
arr[i] = num % 10;
num /= 10;
i++;
}
// 利用数组判断是否是回文数
for (int j = 0; j < i / 2; j++) {
if (arr[j] != arr[i - 1 - j]) {
result = false;
} else {
result = true;
}
}
return result;
}

}

       该方法将整数取余数,分别把每一位存入数组中,再将数组每一对应位上数值进行比较判断,最后得出结果。

 


       当然不仅仅可以判断数字哦,还可以试一试字符串是不是回文,比方说“你是我是你”。大家可以打开电脑,试着编一编代码哦!!!微信搜索公众号油墨山,即可获取更多资讯哦!!!

Guess you like

Origin www.cnblogs.com/skylife/p/11087889.html