leetcode 9. Palindrome Number

Solution1

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

public class Solution {
	
	public static void main(String[] args) {
		int a = 1234321;
		boolean result = isPalindrome(a);
		System.out.println(result);
	}
	
	public static boolean isPalindrome(int x) {
        String s = Integer.toString(x);
    	int i = s.length()/2;
        if(s.length()%2==1){						//根据整型数的位数来采用不同方式判断是否是回文串
        	return find(s,i-1,i+1);
        }else{
        	return find(s,i-1,i);
        }
    }
	
	public static boolean find(String s,int left,int right){	//从中间到两侧判断是否是回文整型数
		while(left>=0||right<=s.length()-1){
			if(s.charAt(left)!=s.charAt(right)){
				return false;
			}
			left--;
			right++;
		}
		return true;
	}
    
}


Solution2

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

public class Solution {
	
	public static void main(String[] args) {
		int a = 1234321;
		boolean result = isPalindrome(a);
		System.out.println(result);
	}
	
	public static boolean isPalindrome(int x) {
		int y = 0;					//x的逆序整型数
		int num = x;
		if(x<0){					//负数不是回文整型数
			return false;
		}
		while(num!=0){				//求x的逆序整型数
			y = y*10+num%10;
			num = num/10;
		}
		if(x == y){					//若逆序整型数与其相等,则为回文整型数
			return true;
		}else{
			return false;
		}
    }

}



你可能感兴趣的:(LeetCode)