鱼C论坛

 找回密码
 立即注册
查看: 2202|回复: 0

[技术交流] 【朱迪的LeetCode刷题笔记】9. Palindrome Number #Easy #C

[复制链接]
发表于 2021-3-18 07:24:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
9. Palindrome Number #Easy

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.


Example 1:

Input: x = 121
Output: true

Example 2:

Input: x = -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: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:

Input: x = -101
Output: false
 

Constraints:

-231 <= x <= 231 - 1
 

Follow up: Could you solve it without converting the integer to a string?

C
#include <limits.h>

bool isPalindrome(int x){
    if (x < 0) {
        return false;
    }
    if (x == 0) {
        return true;
    }
    int temp_x = x;
    int reverse_x = 0;
    while (temp_x) {
        if (reverse_x > (INT_MAX - (temp_x % 10)) / 10) {
            return x / 10 == reverse_x && x % 10 == temp_x;
        }
        reverse_x = (reverse_x * 10) + (temp_x % 10);
        temp_x = temp_x / 10;
    }
    return reverse_x == x;
}

WeChat Image_20210317192349.png

评分

参与人数 1荣誉 +2 鱼币 +3 贡献 +3 收起 理由
隔壁繁星吖 + 2 + 3 + 3 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-22 09:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表