Java
1
自己做出来的naive solution,直接把整数转换成字符串,然后逆转字符串
1 | class Solution { |
Submission Detail
- 1032 / 1032 test cases passed.
2
做 9. Palindrome Number ,提示Coud you solve it without converting the integer to a string? 发现人家的方法更直接利落,温故而知新,重新实现下,结果被卡在 java int out of range 的问题里,1534236469 逆序数超过Integer.MAX_VALUE 返回 1056389759,题目里就提示了 Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 最后想到不如把逆序数变量定义为 long类型,再判断是否超过2147483647(0x7fffffff, 2^31-1),再把long 安全地转换为int返回,java的long安全转换为int的小问题,google关键字 java long to int
参考了下 Safely casting long to int in Java 这样就ok了,在leetcode上运行通过。
1 | class Solution { |
Submission Detail
- 1032 / 1032 test cases passed.
- Runtime: 21 ms
- Your runtime beats 97.62 % of java submissions.