LeetCode 9 Palindrome Number (回文数)(Math)

来源:互联网 发布:nginx 目录配置 编辑:程序博客网 时间:2024/06/05 04:51

翻译

确定一个整数是否是回文数。不能使用额外的空间。

一些提示:

负数能不能是回文数呢?(比如,-1)

如果你想将整数转换成字符串,但要注意限制使用额外的空间。

你也可以考虑翻转一个整数。
然而,如果你已经解决了问题“翻转整数(译者注:LeetCode 第七题),
那么你应该知道翻转的整数可能会造成溢出。
你将如何处理这种情况?

这是一个解决该问题更通用的方法。

原文

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.

分析

created at 2015/09/27

一开始的想法,大不了不新建一个字符串,直接在循环里用就好了,结果居然也可以accept。

    public boolean isPalindrome2(int x) {        String s = String.valueOf(x);        int len = s.length();        for (int i = 0; i < len / 2; i++) {            if (s.charAt(i) != s.charAt(len - i - 1))                return false;        }        return true;    }

可是叻,居然花了244毫秒,效率太低了。

然后修改了一下,效率还是太低了。不过还是说一下这里的分析:

以4224为例,第一次循环时div等于1000,所以left等于4,right等于4。然后下一次x % div之后得到224,但通过继续除以10取整得到22,所以下一次继续循环就OK了。

    public boolean isPalindrome(int x) {        if (x < 0) return false;        int div = 1;        while (x / div >= 10)            div *= 10;        while (x != 0) {            int left = x / div;            int right = x % 10;            if (left != right)                return false;            x = (x % div) / 10;            div /= 100;        }        return true;    }
updated at 2016/09/24
    public boolean isPalindrome(int x) {        if (x < 0) return false;        long newX = 0; // 最后比较的数字,防止2147483647的情况,因为会转换成746***,这就溢出了        long copyX = x;  // 用于处理的X        int pow = 0;  // 用于计算10的pow次幂        int last = (int)copyX % 10;  // 用于得到copyX的最后一个数字        while (copyX != 0) {            copyX /= 10;            pow++;        }        copyX = x;  // 重新赋值        while (pow != 0) {            copyX = (copyX - last) / 10;            newX += last * Math.pow(10, --pow);  // 以4224为例,4位数,但求10的平方时是从3次方到0次方            last = (int)copyX % 10;        }        if (newX == x)            return true;        else return false;    }

继续想办法,想到用两个栈做比较,可以实现,但居然还是要250ms。啊崩溃……都凌晨一点了。

    public boolean isPalindrome(int x) {        if (x < 0) return false;        int copyX = x;        int last = copyX % 10;        Stack<Integer> stack1 = new Stack<>();        Stack<Integer> stack2 = new Stack<>();        while (copyX != 0) {            stack1.add(last);            copyX = (copyX - last) / 10;            last = copyX % 10;        }        Stack<Integer> copyStack = (Stack<Integer>)stack1.clone();        while (!stack1.empty()) {            stack2.add(stack1.pop());        }        while (!copyStack.empty()) {            if (copyStack.pop() != stack2.pop())                return false;        }        return true;    }

如果在最开始的if判断后面再添加两个就可以缩短13ms的时间,这也说明,算法的确是需要优化。

if (x < 0) return false;if (x < 10) return true;if (x % 10 == 0) return false;

如果继续再加上下面这两个判断,还可以继续缩短14ms的时间,但仍然有两百多毫秒,主干部分的逻辑效率太低。

2 0
原创粉丝点击