f*********m 发帖数: 726 | 1 题目如下。
我的问题:
如何理解提示中的最后一句话:“ You would then have to re-design the function
(ie, add an extra parameter).”加什么样的extra parameter?
谢谢。
题目:
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). | f*********m 发帖数: 726 | 2 顶。
function
【在 f*********m 的大作中提到】 : 题目如下。 : 我的问题: : 如何理解提示中的最后一句话:“ You would then have to re-design the function : (ie, add an extra parameter).”加什么样的extra parameter? : 谢谢。 : 题目: : Reverse digits of an integer. : Example1: x = 123, return 321 : Example2: x = -123, return -321 : Have you thought about this?
| e****e 发帖数: 418 | 3 和算法没关吧。我想可能是一旦有错误(overflow)发生,因为不能抛出异常,需要一个
extra parameter来记录错误信息的。例如,extra parameter可以是个List类。
function
【在 f*********m 的大作中提到】 : 题目如下。 : 我的问题: : 如何理解提示中的最后一句话:“ You would then have to re-design the function : (ie, add an extra parameter).”加什么样的extra parameter? : 谢谢。 : 题目: : Reverse digits of an integer. : Example1: x = 123, return 321 : Example2: x = -123, return -321 : Have you thought about this?
| l*****a 发帖数: 14598 | 4 why get a list?
大概是这样吧?
bool reverse(int input, int* output,int* error)
{
if(overflow)
{
*error=ERROR_OVER_FLOW;
return false;
}
if(***) {
*error=****;
return false;
}
*output=***;
return true;
}
类。
【在 e****e 的大作中提到】 : 和算法没关吧。我想可能是一旦有错误(overflow)发生,因为不能抛出异常,需要一个 : extra parameter来记录错误信息的。例如,extra parameter可以是个List类。 : : function
| f*********m 发帖数: 726 | 5 哦,了解,多谢各位。我以为要什么特殊的算法或design pattern。 |
|