1. 问题
Determine whether an integer is a palindrome. Do this without extra space.
思路
【解答】Palindrome指的是回文,而这里需要找的是回文数,指的是1、121、34543这样从左往右看和从右往左看都相等的数。
先找到数字总共有几位,然后判断高位和低位是否相等,相等是回文数。getDigit方法是用来取数x的第i位的数字的,i从低到高记位,最低位为1。
程序代码
package cn.zhm.day2;
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* Determine whether an integer is a palindrome. Do this without extra space.
* */
Integer num = 1234321;
Solution solution = new Solution();
System.out.println(solution.isPalindrome(num.toString()));
}
public boolean isPalindrome(String num ){
boolean flag = true;
int length = num.length();
int low = 0;
int heigh = length -1;
while(low <= heigh){
if(num.charAt(low)==num.charAt(heigh)){
low++;
heigh--;
}else {
flag = false;
return flag;
}
}
return flag;
}
}