[LeetCode] [Palindrome Number 2012-01-04]

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

if use recursive, like check the first dig and last dig, then remove them,
check the rest, it will fail when digint like "1021", when remove the first and
last one, the remaining would simply be "2", not "02".

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

class
Solution {

public:

    bool
isPalindrome(int
x) {

        //Palindrome means 回文

        

        if(x < 0) return
false;

        if(x <= 9) return
true;

        int
t = x;

        int
n=0;

        while(t > 0)

        {

            n++;

            int
d = t%10;

            t=(t-d)/10;

        }

        

        

        int* s = new
int[n];

        t = x;

        n=0;

        while(t > 0)

        {

            int
d = t%10;

            s[n++] = d;

            t=(t-d)/10;

        }

        

        for(int
i = 0; i<n/2;i++)

        {

            if(s[i] != s[n-i-1])

            {

                return
false;

            }

        }

        return
true;

    }

};

[LeetCode] [Palindrome Number 2012-01-04],布布扣,bubuko.com

时间: 2024-12-05 22:47:50

[LeetCode] [Palindrome Number 2012-01-04]的相关文章

LeetCode——Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

[LeetCode] Palindrome Number 验证回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

(LeetCode)Palindrome Number -- 判断回文数

Determine whether an integer is a palindrome. Do this without extra space. 解题分析: 题目很简单,但是对于要求需要看清楚. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ex

LeetCode—Palindrome Number 数字是否是回文数字

Determine whether an integer is a palindrome. Do this without extra space. 检测当前数字是否是回文数字,同时不能增加额外的内存空间,这里一个注意的点就是 负数 都不可能是回文数字 然后是检测出来每一位数字进行比较 代码还是写得比较繁琐,主要的一个点就是数字的位数是基数位和偶数位的时候处理的过程是不同的 class Solution { public: int GetH(int x,int num)//获得对应位置的数字 {

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

[leetcode] Palindrome Number(不使用额外空间)

本来判断回文串是一件很容易的事情,只需要反转字符串后在与原字符串相比较即可.这道题目明确说明不能使用额外的空间,那么使用将其分解连接成字符串的方法便不是可行的.只好采用数学的方式: 每次取最高位和最低位相比较,总的位数可以用一个while先处理出来,循环直至取余和除数相等. 具体见代码: class Solution { public: bool isPalindrome(int x) { if(x<0) //special due return false; if(x<10) return

LeetCode:Palindrome Number

基本思路:把 int 转换成string  借用stringstream  时间复杂度O(n) 1 class Solution { 2 public: 3 public: 4 bool isPalindrome(int x) { 5 6 stringstream ss; 7 ss<<x; 8 string str=ss.str(); 9 int sfront = 0; 10 int sback=str.length()-1; 11 while(sfront<sback) 12 { 13

leetcode:Palindrome Number【Python版】

一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: 1 class Solution: 2 # @return a boolean 3 def isPalindrome(self, x): 4 o = x 5 ret = 0 6 flag = 1 7 if x < 0: 8 return False 9 while(x!=0): 10 ret = ret*10+x%10 11 x = x/10 12 return ret