由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一道Leetcode 题, 多谢
相关主题
请教leetcode一道题如何判断一个数是不是回文?
请问如何安全地reverse 一个integer请假大家一个问题
reverse an integer 怎么判断是否 overflow 来着一道关于两倍年龄的题目 (转载)
问个简单的atoi的问题有好的merge有序数组算法么
问一个reverse int的问题帮忙看看我写的atoi有没有bug, 谢谢
问一道leetcode的题什么也不管了,给了一个烙印很差的feedback
请教一个reverse decimal number的问题Divide Two Integers
问个简单C reverse int求教一个, Leetcode 题.
相关话题的讨论汇总
话题: integer话题: reverse话题: leetcode话题: return话题: 123
进入JobHunting版参与讨论
1 (共1页)
I*********e
发帖数: 61
1
Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if
you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such
as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is
a 32-bit integer, then the reverse of 1000000003 overflows. How should you
handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option
? You would then have to re-design the function (ie, add an extra parameter).
遇到overflow时,能想到的是转换成string,但题目要求返回int。没想到怎么样“add
an extra
parameter”
请赐教。
h****n
发帖数: 2094
2
Return value of the func is error code and another extra pointer for the
return integer value.

such
is

【在 I*********e 的大作中提到】
: Reverse Integer
: Reverse digits of an integer.
: Example1: x = 123, return 321
: Example2: x = -123, return -321
: Have you thought about this?
: Here are some good questions to ask before coding. Bonus points for you if
: you have already thought through this!
: If the integer's last digit is 0, what should the output be? ie, cases such
: as 10, 100.
: Did you notice that the reversed integer might overflow? Assume the input is

c*****a
发帖数: 808
3
我是按这解法做的
123 = 100 + 20 + 3
3=123%10
2= 123/10 %10
1 = 123/100%10
321 = 3* 100 + 2* 10 + 1
t********e
发帖数: 344
4
int reverse(int num){
int rev = 0;
while(num != 0){
rev = num%10 + rev*10;
num = num/10;
}
return rev;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
求教一个, Leetcode 题.问一个reverse int的问题
Linked电面分享,挺好的题 应该已挂问一道leetcode的题
leetcode: pow(x,n)请教一个reverse decimal number的问题
求大数加1题目的细节问个简单C reverse int
请教leetcode一道题如何判断一个数是不是回文?
请问如何安全地reverse 一个integer请假大家一个问题
reverse an integer 怎么判断是否 overflow 来着一道关于两倍年龄的题目 (转载)
问个简单的atoi的问题有好的merge有序数组算法么
相关话题的讨论汇总
话题: integer话题: reverse话题: leetcode话题: return话题: 123