LeetCode--palindrome-number回文数

博客围绕判断一个整数是否为回文数展开,要求不使用额外空间。分析了负数不能是回文数,探讨了整数转字符串、反转整数等方法,还提及了可能遇到的整数溢出问题,同时给出了错误思路及正确利用字符串反转功能的解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(内部重写了,比较的是值)。

基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值