Skip to content

Commit b66ea1c

Browse files
committed
leetcode
1 parent 2338ae2 commit b66ea1c

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

PowOfXN/pow_of_x_n.dart

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
3+
4+
-* 50. Pow(x, n) *-
5+
6+
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
7+
8+
9+
10+
Example 1:
11+
12+
Input: x = 2.00000, n = 10
13+
Output: 1024.00000
14+
Example 2:
15+
16+
Input: x = 2.10000, n = 3
17+
Output: 9.26100
18+
Example 3:
19+
20+
Input: x = 2.00000, n = -2
21+
Output: 0.25000
22+
Explanation: 2-2 = 1/22 = 1/4 = 0.25
23+
24+
25+
Constraints:
26+
27+
-100.0 < x < 100.0
28+
-231 <= n <= 231-1
29+
n is an integer.
30+
Either x is not zero or n > 0.
31+
-104 <= xn <= 104
32+
33+
34+
35+
*/
36+
37+
class A {
38+
double solve(double x, int n) {
39+
if (n == 0) {
40+
return 1.0; // power of 0 is 1
41+
}
42+
double temp = solve(x, n ~/ 2);
43+
temp = temp * temp;
44+
45+
if (n % 2 == 0) {
46+
// if even, return just without doing anything
47+
return temp;
48+
} else {
49+
return temp *
50+
x; // if odd, return by multiplying once more with the given number
51+
}
52+
}
53+
54+
double myPow(double x, int n) {
55+
if (x == 1) return 1;
56+
57+
int longN = n;
58+
double ans = solve(x, longN.abs().toUnsigned(64).toInt());
59+
60+
if (longN < 0) return 1 / ans;
61+
62+
return ans;
63+
}
64+
}
65+
66+
class B {
67+
double solve(double x, int n) {
68+
double ans = 1;
69+
while (n > 0) {
70+
if (n & 1 == 1) {
71+
ans *= x;
72+
}
73+
x *= x;
74+
n >>= 1;
75+
}
76+
return ans;
77+
}
78+
79+
double myPow(double x, int n) {
80+
if (x == 1) {
81+
return 1;
82+
}
83+
84+
int longN = n;
85+
double ans = solve(x, longN.abs().toUnsigned(64).toInt());
86+
87+
if (longN < 0) {
88+
return 1 / ans;
89+
}
90+
return ans;
91+
}
92+
}
93+
94+
class Solution {
95+
double myPow(double x, int n) {
96+
if (x == 1) {
97+
return 1;
98+
}
99+
100+
if (n == 0) {
101+
return 1;
102+
}
103+
104+
if (n < 0) {
105+
x = 1 / x;
106+
n = -n;
107+
}
108+
109+
double ans = 1;
110+
while (n > 0) {
111+
if ((n & 1) == 1) {
112+
ans *= x;
113+
}
114+
x *= x;
115+
n >>= 1;
116+
}
117+
118+
return ans;
119+
}
120+
}

PowOfXN/pow_of_x_n.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
/*
4+
5+
6+
func solve(x float64, n int64) float64 {
7+
ans := 1.0
8+
for n > 0 {
9+
if n&1 == 1 {
10+
ans *= x
11+
}
12+
x *= x
13+
n >>= 1
14+
}
15+
return ans
16+
}
17+
18+
func myPow(x float64, n int) float64 {
19+
if x == 1 {
20+
return 1
21+
}
22+
23+
longN := int64(n)
24+
var ans float64
25+
if longN < 0 {
26+
ans = 1 / solve(x, -longN)
27+
} else {
28+
ans = solve(x, longN)
29+
}
30+
return ans
31+
}
32+
33+
------------------------------------------------------Solution----------
34+
35+
func solve(x float64, n int64) float64 {
36+
if n == 0 {
37+
return 1.0 // power of 0 is 1
38+
}
39+
temp := solve(x, n/2)
40+
temp *= temp
41+
42+
if n%2 == 0 {
43+
return temp
44+
} else {
45+
return temp * x
46+
}
47+
}
48+
49+
func myPow(x float64, n int) float64 {
50+
if x == 1 {
51+
return 1
52+
}
53+
54+
longN := int64(n)
55+
ans := solve(x, int64(longN))
56+
57+
if longN < 0 {
58+
return 1 / ans
59+
}
60+
return ans
61+
}
62+
63+
64+
*/
65+
66+
67+
68+
// Iterative BitWise
69+
func myPow(x float64, n int) float64 {
70+
if x == 1 {
71+
return 1
72+
}
73+
74+
if n == 0 {
75+
return 1
76+
}
77+
78+
if n < 0 {
79+
x = 1 / x
80+
n = -n
81+
}
82+
83+
ans := 1.0
84+
for n > 0 {
85+
if n&1 == 1 {
86+
ans *= x
87+
}
88+
x *= x
89+
n >>= 1
90+
}
91+
92+
return ans
93+
}

PowOfXN/pow_of_x_n.md

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
250250
- [**146.** LRU Cache](LRUCache)
251251
- [**735.** Asteroid Collision](AsteroidCollision)
252252
- [**673.** Number of Longest Increasing Subsequence](NumberofLongestIncreasingSubsequence)
253+
- [**50. Pow(x, n)**](PowOfXN)
253254

254255
## Reach me via
255256

0 commit comments

Comments
 (0)