// string <=> vectorint main(int argc, char *argv[]) {string str = "123456";vector<char> v;v.assign(str.begin(), str.end());for (char c : v) cout << c << ' ';cout << endl;}
char <=> int char c = n + ‘0’; int n = c - ‘0’;
// 通过反转数字来与原数字对比判断是否为回文数// 只需要反转后半段数组与前半段数字进行比较class Solution {public:bool isPalindrome(int x) {// 首先排除负数和10的倍数,它们都不是回文数if (x < 0 || (x != 0 && x % 10 == 0)) return false;int res = 0;// 如果是回文数的话,刚好反转一半退出循环// 如果不是回文数的话,循环退出后x和res也不会相等// 当反转的后半段数字大于前半段数字时退出循环,此时刚好反转到一半// 此时x是前半段数字,res是后半段数字while (x > res) {res = res * 10 + x % 10;x /= 10;}// 最后如果原数字长度为奇数时,要去掉前半段数组最后一位再进行判断,// 也就是原数字中处于中间位置的数字// 对比前半段数字和后半段数字是否相等return res == x || res/10 == x;}};
