LeetCode--palindrome-number回文数

1、问题

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

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

2、分析

判断一个整数是否为回文数,不要用额外的空间。

负数可以是回文数吗?(不可以)

如果你想用整数转为字符串,注意不可以使用额外的空间。

你也可以尝试反转一个整数,但是要考虑整数溢出的问题。

3、正解

class Huiwenshu {
	public static void main(String[] args) {
		boolean b1 = isPalindrome(5);
		boolean b2 = isPalindrome(-10);
		boolean b3 = isPalindrome(200);
		boolean b4 = isPalindrome(12321);
		System.out.println(b4);
	}

	static boolean isPalindrome(int x) {
		int res = 0;//定义反向数
		if(x<0 || x!=0 && x%10==0){
			//当x小于0,或者x是10的整数倍时,不是回文数
			return false;
		}
		while(x > res){//只要正向剩下的数大于反向得到的数,就继续判断
			res = res *10 + x%10;//反向得到的数
			x = x/10;//正向剩下的数
		}
		return(res == x)||(res/10 == x);
			//偶数位时两者相等,奇数位时res/10==x
	}
}

4、第一次错误思路:

2332,把最低位移到最高位,移4次会得到2332,判断改变前后是否相等。但是!所有数都可以满足。。。

不过其中也学习到一些细节:

(1)Java中求幂:double d = Math.pow(double a,double b);

(2)强制转换:int  i =(int)d;

//方法是错误的
class Palindrome {
	public static void main(String[] args) {
		boolean b = isPalindrome(123);
		System.out.println(b);
	}
	public static boolean isPalindrome(int x) {
        if(x<0){
           return false;
        }
        int n = 1;//获取位数
		int wei = x;
        while(wei>10){
            wei = wei/10;//改变了x的值
            n++;   
        }
		System.out.println("x="+x);
        int temp = x;
        for(int i=1;i<n;i++){
            temp = temp% 10 * (int)Math.pow(10,n-1) + temp/10;
			System.out.println("temp="+temp);
        }
        //if(x==1)
        //   System.out.println(temp);
        if(temp == x)
            return true;
        else
            return false;
    }
}

5、第二次错误解法(AC,但是题目说到了不要用整数转字符串)

用栈实现整数的反转,且不会溢出,但是不符合要求。

import java.util.Stack;
//利用栈实现回文数的判断
class Palindrome2 {
	public static void main(String[] args) {
		int x = 12121;
		boolean b = isPalindrome(x);
		System.out.println(b);
	}

	public static boolean isPalindrome(int x) {
        if(x<0){
           return false;
        }
		String in = x+"";//x转为字符串
		char [] arr = in.toCharArray();//字符串转为字符数组
		Stack<String> st = new Stack<String>();//创建一个String类型的栈
		for(int i = 0;i<arr.length;i++){
			st.push(arr[i]+"");//字符进栈
		}
		String out = "";//出栈序列转为字符串
		while(!st.isEmpty()){
			out = out + st.pop();//字符出栈并连接
		}
		int res = Integer.parseInt(out);//字符串转为整数

		return res == x;
    }
}

6、利用字符串的反转功能

	static boolean isPalindrome2(int x){
		//利用字符串的反转
		if(x<0 || x!=0 && x%10==0){
			return false;//此时不能字符串反转
		}
		StringBuffer sb = new StringBuffer(x+"");
		sb.reverse();//反转
		System.out.println(sb.toString());
		System.out.println(x+"");
		return sb.toString().equals(x+"");

	}

注意最后比较时,不能用==(比较的是地址),要用equals(内部重写了,比较的是值)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值