[LeetCode] Palindrome Number
The most obvious idea is to maintain two divisors to get the most and least significant digits and compare them. Well, there are much more clever ideas, like this one, whose code is rewritten below. Play with it :-)
1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if (x < 0 || (x && !(x % 10))) return false; 5 int s = 0; 6 while (x > s) { 7 s = s * 10 + x % 10; 8 x /= 10; 9 } 10 return x == s || x == s / 10; 11 } 12 };
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步