判断整数回文 2025-02-28 Leetcode 1 min 75 字 12345678910111213141516171819202122#include<iostream>#include<string>using namespace std;class Solution{ public: bool isPalindrome(int x){ string str = to_string(x); int left =0, right = str.size() - 1; while(left < right){ if(str[left] != str[right]){ return false; } left++; right--; } return true; }}; 算是最简单的一道题了 评论区看到了不转换成字符串的做法 123456789bool isPalindrome(int x) { if(x<0||x%10==0&&x!=0)return false; int reversedHalf=0; while(x>reversedHalf){ reversedHalf=reversedHalf*10+x%10; x/=10; } return x==reversedHalf||x==reversedHalf/10;} #算法#Leetcode copyright copyright_text 本文链接:https://afogsheep-github-io.pages.dev2025/02/28/Leetcode_判断整数回文/ prev两数相加 next 合并两个有序链表