成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Pow(x, n) [Binary Search]

ideaa / 3439人閱讀

摘要:無(wú)須考慮為的情況,直接轉(zhuǎn)化成正數(shù)計(jì)算倒數(shù)。需要注意的情況,取負(fù)數(shù)之后會(huì)溢出。

Problem

Implement pow(x, n).

Example

Pow(2.1, 3) = 9.261
Pow(0, 1) = 0
Pow(1, 0) = 1

Note

You don"t need to care about the precision of your answer, it"s acceptable if the expected answer and your answer "s difference is smaller than 1e-3.

Challenge

O(logn) time

Solution Update 2018-9
class Solution {
    public double myPow(double x, int n) {
        if (n == 0) return 1;
        double half = myPow(x, n/2);
        if (n % 2 == 0) return half*half;
        else {
            if (n < 0) return 1/x*half*half;
            else return x*half*half;
        }
    }
}

When you see O(logn) time for a obvious O(n) time question, think about binary search and recursion.

Double myPow()

無(wú)須考慮n為Integer.MIN_VALUE的情況,直接轉(zhuǎn)化成正數(shù)計(jì)算倒數(shù)。

public class Solution {
    public double myPow(double x, int n) {
        if (n < 0) return 1/pow(x, -n);
        else return pow(x, n);
    }
    private double pow(double x, int n) {
        if (n == 0) return 1;
        double val = pow(x, n/2);
        if (n % 2 == 0) return val*val;
        else return val*val*x;
    }
}
Double x

需要注意n = Integer.MIN_VALUE的情況,取負(fù)數(shù)之后會(huì)溢出。

public class Solution {
    public double myPow(double x, int n) {
        if (n == 0) return 1;
        if (n < 0) {
            if (n == Integer.MIN_VALUE) {
                n++;
                return 1/(myPow(x, Integer.MAX_VALUE)*x);
            }
            n = -n;
            x = 1/x;
        }
        return (n%2 == 0) ? myPow(x*x, n/2): x*myPow(x*x, n/2);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65503.html

相關(guān)文章

  • [LintCode] Remove Node in Binary Search Tree [理解BS

    Problem Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You sho...

    陳江龍 評(píng)論0 收藏0
  • [LintCode] Search Range in Binary Search Tree

    摘要:重點(diǎn)是根據(jù)的性質(zhì),先左后根最后右。另一重點(diǎn)是,函數(shù)和函數(shù)都要用的的參數(shù),記得在函數(shù)外層定義。 Problem Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. pr...

    draveness 評(píng)論0 收藏0
  • [LintCode/LeetCode] Search for a Range [左右邊界法/一次循環(huán)

    摘要:首先,建立二元結(jié)果數(shù)組,起點(diǎn),終點(diǎn)。二分法求左邊界當(dāng)中點(diǎn)小于,移向中點(diǎn),否則移向中點(diǎn)先判斷起點(diǎn),再判斷終點(diǎn)是否等于,如果是,賦值給。 Problem Given a sorted array of n integers, find the starting and ending position of a given target value. If the target is not...

    zhangrxiang 評(píng)論0 收藏0
  • [LintCode] Insert Node in a Binary Search Tree

    摘要:建立兩個(gè)樹(shù)結(jié)點(diǎn),先用找到在的位置,讓作為的根節(jié)點(diǎn)找到的位置后,指向。此時(shí),用代替與連接就可以了。 Problem Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tr...

    Taste 評(píng)論0 收藏0
  • [LintCode] Binary Search Tree Iterator

    摘要:建立一個(gè)堆棧,先將最左邊的結(jié)點(diǎn)從大到小壓入棧,這樣的話,為了實(shí)現(xiàn)迭代即返回下一個(gè)的函數(shù)就要考慮右邊的結(jié)點(diǎn)。如此,實(shí)現(xiàn)函數(shù)。 Problem Design an iterator over a binary search tree with the following rules: Elements are visited in ascending order (i.e. an in-o...

    silencezwm 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<