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
标签:
Leetcode Easy
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现