Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to find the integer power of a value by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:03:19 09月25日 Update

Editor to share with you how to calculate the integer power of LeetCode, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Brief introduction of the problem.

Realize the function double Power (double base, int exponent) and find the exponent power of base. Library functions are not allowed, and large numbers do not need to be considered.

2, example

Example 1:

Input: 2.00000, 10 output: 1024.00000 example 2:

Input: 2.10000, 3 output: 9.26100 example 3:

Input: 2.00000,-2 output: 0.25explanation: 2-2 = 1max 22 = 1max 4 = 0.25000

Description:

-100.0

< x < 100.0n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。 3,题解思路 快速幂方法,java已有的api两种方法 4,题解程序 public class MyPowTest { public static void main(String[] args) { double x = 2.00000; int n = 10; double myPow = myPow(x, n); System.out.println("myPow = " + myPow); } public static double myPow(double x, int n) { if (x == 0) { return 0; } if (n == 0) { return 1; } if (n >

0) {return pow (x, n);} else {return pow (1 / x,-n);}}

Private static double pow (double x, int n) {if (n = = 0) {return 1;} double r = pow (x, n / 2); if ((n & 1) = = 1) {return r * r * x;} else {return r * r;}

5. Picture version of the problem solving program.

These are all the contents of the article "how to calculate the integer power of LeetCode". Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Examples numerical values integers articles problem solutions input output content functions methods programs problems not much at the same time pictures large numbers most ideas more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker Microsoft OPPO Reno Shulou Technology