Leetcode第9题Palindrome Number

Question:Determine whether an integer is a palindrome. Do this without extra space.


/************************************************************************************/

package Palidrome;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Palidrome {
public static void main(String[] args) throws NumberFormatException, IOException{
int num;
boolean result;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input a integer number, and this programe will " +
"check whether this number is a palidrome.");
num = (new Integer(stdin.readLine()));

result = isPalindrome(num);

System.out.println("This integer number is a palindrome ? ");
System.out.println(result);
}

        //方法
public static boolean isPalindrome(int num) {
//将整形数字num转化为一个名为s的字符串
String s = Integer.toString(num);

//将字符串转化为一个名为charNum的字符数组
char[] charNum = s.toCharArray();

int i = 0;
int j = charNum.length - 1;
char temp;

//设置表置位,并初始化为false。当symbol = true时,输入数字为palindrome,否则不是。
boolean symbol = false;

if(charNum.length <= 1){
return true;
}

if(num <= 0 || num >= 2147483647){
return false;
}

while(i < j){
temp = charNum[i];
charNum[i] = charNum[j];
charNum[j] = temp;
i++;
j--;
}

//将字符数组合并为一个新的字符串
String resultString = new String(charNum);
//将字符串转化为一个整形数
Double resultNum = Double.parseDouble(resultString);

if(resultNum == num){
symbol = true;
}
else{
symbol = false;
}
return symbol;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值