鱼C论坛

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

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

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

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

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

x

  1. 9. Palindrome Number #Easy

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

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


  4. Example 1:

  5. Input: x = 121
  6. Output: true

  7. Example 2:

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

  11. Example 3:

  12. Input: x = 10
  13. Output: false
  14. Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

  15. Example 4:

  16. Input: x = -101
  17. Output: false


  18. Constraints:

  19. -231 <= x <= 231 - 1


  20. Follow up: Could you solve it without converting the integer to a string?
复制代码


C

  1. #include <limits.h>

  2. bool isPalindrome(int x){
  3.     if (x < 0) {
  4.         return false;
  5.     }
  6.     if (x == 0) {
  7.         return true;
  8.     }
  9.     int temp_x = x;
  10.     int reverse_x = 0;
  11.     while (temp_x) {
  12.         if (reverse_x > (INT_MAX - (temp_x % 10)) / 10) {
  13.             return x / 10 == reverse_x && x % 10 == temp_x;
  14.         }
  15.         reverse_x = (reverse_x * 10) + (temp_x % 10);
  16.         temp_x = temp_x / 10;
  17.     }
  18.     return reverse_x == x;
  19. }
复制代码


WeChat Image_20210317192349.png

评分

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

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-28 20:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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