Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the integer power of a value by LeetCode

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report