标签:一个 etc 常见 int class color rom 回文数 get
题目链接:https://leetcode.com/problems/palindrome-number/description/
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121 Output: true
Example 2:
Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
思路:
编码如下:
1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if (x < 0) return false; // 负数不满足 5 if (x < 10) return true; // 0满足 6 7 // 方法一:直接转换为string类型进行处理 8 string s = to_string(x); 9 10 for (int i = 0, j = s.length() - 1; i <= j; ++i, --j) 11 { 12 if (s[i] != s[j]) return false; 13 } 14 15 16 // 方法二:对数字进行“逐层剥离” 17 // vector<int> ivec; 18 // while (x != 0) 19 // { 20 // int cur = x % 10; 21 // ivec.push_back(cur); 22 // x = x / 10; 23 // } 24 25 // for (int i = 0, j = ivec.size() - 1; i <= j; ++i, --j) 26 // { 27 // if (ivec[i] != ivec[j]) return false; 28 // } 29 30 return true; 31 } 32 };
标签:一个 etc 常见 int class color rom 回文数 get
原文地址:https://www.cnblogs.com/ming-1012/p/9929186.html