a to the power of b

 1 public class Solution {
 2   public long power(int a, int b) {
 3     // Write your solution here
 4     if (b == 0) return 1;
 5     long half = power(a, b/2);
 6     if (b % 2 == 0) {
 7         return half * half;
 8     } else {
 9         return half * half * a;
10     }
11   }
12 }

Q: Evaluate a to the power of b, assuming both a and b are integers and b is non-negative. 

 

User recursion to advance the process, reduce the time complexity to O(logb), instead of O(b).

Inside recursion:

  Base case is when b == 0, return 1.

  use b % 2 to determine if odd or even.

  notice the type of return value. In this problem, is "long".

posted on 2018-03-11 09:51  野原新之助0 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/chrislyr/p/8543262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答下面问题Part 1: Multiple Choice Questions (2 points each, total 20 points) Choose the best answer for each question. 1.Which of the following is NOT a passive component? a) resistor b) capacitor c) inductor d) transistor 2.What is the primary purpose of modulation in communication systems? a) to reduce noise b) to increase the frequency of the signal c) to improve the quality of the signal d) to transmit the signal over long distances 3.What is the purpose of a voltage regulator? a) to amplify the voltage of the input signal b) to regulate the output voltage to a constant level c) to provide DC power to the load d) to convert AC power to DC power 4.Which theorem is used to simplify complex circuits for analysis? a) Kirchhoff's law b) The venin's theorem c) Faraday's law d) Ohm's law 5.What is the function of a decoder circuit? a) to convert digital signals into analogue signals b) to convert analogue signals into digital signals c) to select one of several output signals based on the input code d) to amplify signals to a higher power level 6.Which is an advantage of using optical fibers over copper wires in communication systems? a) higher cost b) higher bandwidth c) higher resistance to interference d) easier installation and maintenance 7.What does a low-pass filter do? a) allows low-frequency signals to pass through b) allows high-frequency signals to pass through c) blocks all signals above a certain frequency d) blocks all signals below a certain frequency 8.Which type of transistor has higher input impedance: common-emitter or common-base? a) common-emitter b) common-base c) they have the same input impedance d) it depends on the specific circuit configuration 9.What is the function of a mixer in radio communication systems? a) to amplify the received signal b) to combine two or more signals of different frequencies c) to filter out unwanted signals d) to transmit the signal over long distances 10.Which of the following is NOT a type of noise in electronic circuits? a) shot noise b) thermal noise c) cosmic noise d) flicker noise
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值