Java for LeetCode 050 Pow(x, n)

Implement pow(x, n).

解题思路:

直接使用乘法实现即可,注意下,如果n很大的话,递归次数会太多,因此在n=10和n=-10的地方设置一个检查点,JAVA实现如下:

static public double myPow(double x, int n) {
        if(n==1)
        	return x;
        else if(n>1&&n<=10)
        	return myPow(x,n-1)*x;
        if(n>10)
        	return myPow(myPow(x,10),n/10)*myPow(x,n%10);
        else if(n==-1)
        	return 1/x;
        else if(n<-1&&n>=-10)
        	return myPow(x,n+1)*(1/x);
        else if(n<-10)
        	return myPow(myPow(x,10),n/10)*myPow(x,n%10);
        else return 1;
    }
时间: 2024-10-05 05:41:27

Java for LeetCode 050 Pow(x, n)的相关文章

LeetCode 050 Pow(x, n)

题目要求:Pow(x, n) Implement pow(x, n). 代码如下: class Solution { public: //采用二分法 //时间复杂度 O(logn),空间复杂度 O(1) double pow(double x, int n) { //要考虑n < 0的情况! if(n < 0) return 1.0 / power(x, -n); else return power(x, n); } double power(double x, int n){ if(n ==

LeetCode:Pow(x, n)

1.题目名称 Pow(x, n)(求指定数字x的整数次幂) 2.题目地址 https://leetcode.com/problems/powx-n/ 3.题目内容 英文:Implement pow(x, n) 中文:给定底数x和指数n,求x的n次幂 4.解题方法1 在Java中,有一个偷懒的办法是这样实现的: /**  * 功能说明:LeetCode 50 - Pow(x, n)   * 开发人员:Tsybius2014  * 开发时间:2015年8月8日  */ public class So

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【LeetCode】Pow(x, n)

Implement pow(x, n). 思路:快速幂运算,需要考虑指数为负数,同时底数为0的情况,这种属于异常数据,代码里没有体现. class Solution { public: double pow_abs(double x, unsigned int n) { if (n == 0) { return 1; } if (n == 1) { return x; } double res = pow(x, n>>1); res *= res; if (n & 0x1 == 1)

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Java for LeetCode 059 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave