leetcode palindrome number(easy) /java

题:

把x翻转后,判断是否与x相等。需考虑溢出。但是如果此数翻转后溢出,那么说明其不是回文数。

推理入下,如果翻转后的数溢出且是回文数,那么原数也溢出。矛盾。

public class Solution {
    public boolean isPalindrome(int x) {
        if(x<0)
            return false;
        int y=0,z=x;
        while(x!=0)
        {
            y=y*10+x%10;
            x=x/10;
        }
        return y==z;
    }
}

以及复数不是回文数。

时间: 2024-08-29 23:16:38

leetcode palindrome number(easy) /java的相关文章

[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 r

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] 009. Palindrome Number (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 009.Palindrome_Number (Easy) 链接: 题目:https://oj.leetcode.com/problems/palindrome-number/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数是否是回文数. 分析: 按自己想

【leetcode】Palindrome Number (easy)

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 Add Binary (easy) /java

二进制的加法 经过测试之后,发现没有考虑整型数据溢出. leetcode经常会有一些意想不到的例外.我以为是一道解法很丰富的题,选择了:把string转为int,用int的加法,再转为string返回.因为我讨厌用字符串来进位.但是溢出了.可以改进一下,用BigInteger这个类. 先贴上我的错误代码. import java.io.*; import java.util.*; import java.lang.*; public class Solution { public static

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 验证回文数字

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 Implement strStr()(easy) /java

我以为,当时我用c++写这个函数的时候,整个人如同乱麻. 这次用java写.先查的SE 8中String的方法.找到两个与此函数有关的方法:matches()和substring(). import java.io.*; import java.util.*; public class Solution { public static int strStr(String haystack, String needle) { int r=-1; int len1=haystack.length()