1. 程式人生 > >劍指 Offer - 12:數值的整數次方

劍指 Offer - 12:數值的整數次方

題目描述

給定一個double型別的浮點數base和int型別的整數exponent。求base的exponent次方

題目連結:https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00

public class Solution {
    public double Power(double base, int exponent) {
        int r = Math.abs(exponent);
        double temp = 1.0;
        while (r >
0) { temp *= base; base *= base; r = r >> 1; } return exponent >= 0 ? temp : 1/temp; } }