Redian新闻
>
How to compute power(x,y) in O(1) space
avatar
How to compute power(x,y) in O(1) space# JobHunting - 待字闺中
g*8
1
It is easy to compute power(x,y) like this (without considering overflow and
negative number)
int power(int x, int y){
if(y==0) return 1;
if(y%2==0){
int t=power(x,y/2);
return t*t;
}
else{
int t=power(x,y-1);
return t*x;
}
}
My question is how to computer power in O(1) space because it uses recursion
, the space complexity is O(logy).
My first thought is to reduce the problem by Dynamic Programming. But dp is
notoriously bad in space complexity. But we don't want to know the
intermediate result. Any thoughts? Thank you
avatar
a*m
2
可以把递归写成循环。

and

【在 g*********8 的大作中提到】
: It is easy to compute power(x,y) like this (without considering overflow and
: negative number)
: int power(int x, int y){
: if(y==0) return 1;
: if(y%2==0){
: int t=power(x,y/2);
: return t*t;
: }
: else{
: int t=power(x,y-1);

avatar
q*x
3
loop

and

【在 g*********8 的大作中提到】
: It is easy to compute power(x,y) like this (without considering overflow and
: negative number)
: int power(int x, int y){
: if(y==0) return 1;
: if(y%2==0){
: int t=power(x,y/2);
: return t*t;
: }
: else{
: int t=power(x,y-1);

avatar
g*8
4
But how to still keep time complexity of O(logy)
avatar
a*m
5
递归一般都可以直接改成循环,尤其这种单递归。时间复杂不会变。

【在 g*********8 的大作中提到】
: But how to still keep time complexity of O(logy)
avatar
g*8
6
谢谢,我磋磨琢磨代码去
avatar
p*2
7
public double Pow(int x, int y)
{
bool neg = y < 0;
y = Math.Abs(y);
int p = 1;
if (x == 0)
return 0;
if (y == 0)
return 1;
if (x == 1)
return 1;
while (y > 0)
{
if (y % 2 == 0)
{
x = x * x;
y = y / 2;
}
else
{
p *= x;
y = y - 1;
}
}
return neg ? 1.0 / p : p;
}
}
avatar
g*8
8
int power(int x, int y){
if(y==0) return 1;
int t=x,i=1;
while(1){
if(i==y) return t;
if(i*2t*=t;
i*=2;
}
else break;
}

for(int j=i+1;j<=y;j++)
t*=x;
return t;
}
谢谢大家的帮助,
测试用例:x=2, y=5, x=2, y=0, x=2, y=7, x=3, y=3
avatar
g*8
9
peking2 is right. My code is not that great.
avatar
l*a
10
what happen when x==0 and y<0?

【在 p*****2 的大作中提到】
: public double Pow(int x, int y)
: {
: bool neg = y < 0;
: y = Math.Abs(y);
: int p = 1;
: if (x == 0)
: return 0;
: if (y == 0)
: return 1;
: if (x == 1)

avatar
g*8
11
The problem doesn't concern about negative parameters
avatar
p*2
12

试了一下,C# return Infinity。

【在 l*****a 的大作中提到】
: what happen when x==0 and y<0?
avatar
p*2
13

good case。how to write it in interview? return error?

【在 p*****2 的大作中提到】
:
: 试了一下,C# return Infinity。

avatar
l*a
14
难道不是DivideByZeroException?
按照数学来讲应该是这个

【在 p*****2 的大作中提到】
:
: good case。how to write it in interview? return error?

avatar
p*2
15

C#没有throw, 估计java也一样。

【在 l*****a 的大作中提到】
: 难道不是DivideByZeroException?
: 按照数学来讲应该是这个

avatar
l*a
16
要多想想各种可能
面试时估计不会给你那么多限制

【在 g*********8 的大作中提到】
: The problem doesn't concern about negative parameters
avatar
q*x
17
白板先给限制版,然后讨论加强版。

【在 l*****a 的大作中提到】
: 要多想想各种可能
: 面试时估计不会给你那么多限制

avatar
g*8
18
恩,是我狭隘了,lolhaha,谢谢提醒
avatar
p*2
19

是。lolhaha可是本版大牛。

【在 g*********8 的大作中提到】
: 恩,是我狭隘了,lolhaha,谢谢提醒
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。