[Leetcode]50. Pow(x, n)

Implement pow(xn).

#define EPSINON 0.00001
#define Max 2147483647
#define Min -2147483648
#define DBL_MAX 1.7976931348623159e+308 

class Solution {
public:
    double myPow(double x, int n) {
        /*
        three special case
        */
        if(n==1)
            return x;
        if(n==0)
            return 1.0;
        if(abs(x)==1.00000){
            if(n%2==0)
                return 1.0;
            else
                return x;
        }

        if(n>=Max){
            if(abs(x)<=EPSINON)
                return 0.0;
            else
                return DBL_MAX;
        }

        if(n<=Min){
            if(abs(x)<=EPSINON)
                return DBL_MAX;
            else
                return 0.0;
        }

        int size=abs(n);
        double tmp=x;
        for(int i=2;i<=size;i++){
            tmp*=x;
        }
        if(n>=0)
            return tmp;
        else
            return 1.0/tmp;
    }
};
时间: 2024-12-06 03:15:46

[Leetcode]50. Pow(x, n)的相关文章

[Leetcode]Add Two Numbers

题目 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&g

【一天一道LeetCode】#50. Pow(x, n)

一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回值为1:x==-1时,根据n的奇偶来判断 3.n==-2147483648,特殊情况,int的范围时-2147483648-2147483647, */ class Solution { public: double myPow(double x, int n) { if(n==0||x==1) r

【Leetcode】50. Pow(x, n)

Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 package medium; public class L50MyPow { // 调用Math.pow() 函数 public double mypow(double x, int n) { double nn = n; return Math.pow(x, nn)

[LeetCode] 237. Delete Node in a Linked Lis解题小结

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -

[LeetCode]383. Ransom Note 解题小结

题目: ?Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?it

[Leetcode]141. Linked List Cycle

题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 让head不断走一步,如果cur不为null,走两步,这样如果存在cycle,cur会追上head. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *

[LeetCode] 83. Remove Duplicates from Sorted List

题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 简单题.第一次通过的时候没有加入对head是null的判断,运行23ms,加入了以后只有12ms.必要的判断加入还

[LeetCode]206. Reverse Linked List 解题小结

题目: Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 用循环来做 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *nex

[LeetCode蠕动系列]Sort List

这题前一阵子就看到了,一直没时间做,昨晚睡前想了想,要求n*log(n)以内的时间复杂度,第一时间想到的就是归并.快排和希尔排序(注:希尔排序时间为O(n^1.3),在数据量大于2的情况下小于n*log(n)),个人以为,链表的特性更适合归并,所以采用归并排序,实现的merge代码如下: public static ListNode merge(ListNode rhead, ListNode lhead) { ListNode head = null; if (rhead.val <= lhe