【发布时间】:2022-07-06 13:06:22
【问题描述】:
这是我的代码,它是俄罗斯农民乘法算法。我发现时间和空间的复杂性非常令人困惑,所以我需要一些帮助。
这也适用于java语言
谢谢。
int num1 = Integer.parseInt(jTextField1.getText());
int num2 = Integer.parseInt(jTextField2.getText());
int res=0;
// While second number doesn't become 1
while (num2 > 0)
{
// If second number becomes odd,
// add the first number to result
if ((num2 & 1) != 0)
res = res + num1;
// Double the first number
// and halve the second number
num1 = num1 << 1;
num2 = num2 >> 1;
}
jTextField3.setText(String.valueOf(res));
}
【问题讨论】:
标签: java time-complexity space-complexity