LeetCode 9.Palindrome Numbe

来源:互联网 发布:linux 重启网络服务 编辑:程序博客网 时间:2024/06/04 01:25

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 try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

该题目要求不使用额外的空间,看一个数字是不是回文整数。题目要注意负数不算回文,如果超过最大整数范围就返回false.

func isPalindrome(x int) bool {    if x < 0 {        return false    }    var max int = 0x7fffffff    m := x    var result int = 0    for x > 0 {        result = result*10 + x%10        x = x / 10    }    if result != m {        return false    }    if result > max {        return false    }    return true}
原创粉丝点击