leetcode009:Palindrome Number

来源:互联网 发布:送男友什么礼物 知乎 编辑:程序博客网 时间:2024/06/10 18:30

问题描述

Determine whether an integer is a palindrome. Do this without extra space.

分析

从问题描述奇少看出,代码也不会太多。关键点是不使用额外空间的情况下怎么取得对应位的值:除整取余数。如12358,取千位数的数字=>12358/1000%10.

代码

class Solution {public:    bool isPalindrome(int x) {        long long k = x;        if (k < 0) return false;        int i = 9;        while (!((int)(x / pow(10, i)) % 10)) i--;        for (int j = i; j > i / 2; j--){            if ((int)(x / pow(10, j)) % 10 != (int)(x / pow(10, i - j)) % 10) return false;        }        return true;    }};
0 0
原创粉丝点击