LeetCode9. 回文数

题目

分析

代码

LeetCode7.整数反转 的变形

复制代码
 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x < 0) return false;
 5         int n = 0,o = x;
 6         while(x){
 7             if(n >= (INT_MAX - x % 10) / 10) return false;
 8             n = n*10 + x % 10;
 9             x /= 10;
10             
11         }
12         if(n == o ) 
13             return true;
14         else
15             return false;
16     }
17 };
复制代码

将整形编程字符串,然后字符串反转

1 class Solution {
2 public:
3     bool isPalindrome(int x) {
4         if(x < 0) return false;
5         string s = to_string(x);
6         return s == string(s.rbegin(),s.rend());
7     }
8 };

 

这里使用了反向迭代器 rbegin(), rend() 见  https://blog.csdn.net/xingyanxiao/article/details/45317409

 

posted @   Uitachi  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示