【LeetCode算法-9】Palindrome Number

LeetCode第9题

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

这题正好用到上一题的方法

【LeetCode算法】Reverse Integer

代码:

复制代码
class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        }else{
            int y = 0;
            int z = x;
            while(x/10!=0){
                y*=10;
                y+=x%10;
                x/=10;
            }
            y=y*10+x;
            if(y==z){
                return true;
            }else{
                return false;
            }
        }
    }
}
复制代码

负数都不成功,所以直接报false

posted @   嘉禾世兴  阅读(119)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2017-05-19 Binder
2017-05-19 多渠道打包(友盟统计)
点击右上角即可分享
微信分享提示