File tree Expand file tree Collapse file tree 4 files changed +108
-1
lines changed Expand file tree Collapse file tree 4 files changed +108
-1
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.13 )
2
+ project (cpp_0050 )
3
+
4
+ set (CMAKE_CXX_STANDARD 11 )
5
+
6
+ add_executable (cpp_0050 main.cpp )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/powx-n/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-12-20
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < cassert>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Classic Divide and Conquer to get power
13
+ // / Only deal with positive n
14
+ // /
15
+ // / Time Complexity: O(logn)
16
+ // / Space Complexity: O(logn)
17
+ class Solution {
18
+ public:
19
+ double myPow (double x, int n) {
20
+
21
+ if (n == 0 ) return 1.0 ;
22
+
23
+ double res = myPositivePow (x, abs ((long long )n));
24
+ if (n < 0 ) res = 1.0 / res;
25
+ return res;
26
+ }
27
+
28
+ private:
29
+ double myPositivePow (double x, long long n){
30
+
31
+ assert (n >= 0 );
32
+ if (!n) return 1.0 ;
33
+
34
+ double t = myPositivePow (x, n / 2 );
35
+ double res = t * t;
36
+ if (n % 2 ) res *= x;
37
+ return res;
38
+ }
39
+ };
40
+
41
+
42
+ int main () {
43
+
44
+ cout << Solution ().myPow (2.0 , -2 ) << endl;
45
+ // 0.25
46
+
47
+ cout << Solution ().myPow (-2.0 , 2 ) << endl;
48
+ // 4.0
49
+
50
+ cout << Solution ().myPow (34.00515 , -3 ) << endl;
51
+ // 3e-05
52
+
53
+ cout << Solution ().myPow (1.0 , -2147483648 ) << endl;
54
+ // 3e-05
55
+
56
+ return 0 ;
57
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/powx-n/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-12-20
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+
8
+ using namespace std ;
9
+
10
+
11
+ // / Classic Divide and Conquer to get power
12
+ // / Deal with both positive and negative n correctly
13
+ // /
14
+ // / Time Complexity: O(logn)
15
+ // / Space Complexity: O(logn)
16
+ class Solution {
17
+ public:
18
+ double myPow (double x, int n) {
19
+
20
+ if (n == 0 ) return 1.0 ;
21
+
22
+ double t = myPow (x, n / 2 );
23
+ if (n % 2 == 0 )
24
+ return t * t;
25
+ else if ( n > 0 )
26
+ return t * t * x;
27
+ return t * t / x;
28
+ }
29
+ };
30
+
31
+
32
+ int main () {
33
+
34
+ cout << Solution ().myPow (2.0 , -2 ) << endl;
35
+ // 0.25
36
+
37
+ cout << Solution ().myPow (-2.0 , 2 ) << endl;
38
+ // 4.0
39
+
40
+ cout << Solution ().myPow (34.00515 , -3 ) << endl;
41
+ // 3e-05
42
+
43
+ return 0 ;
44
+ }
Original file line number Diff line number Diff line change @@ -88,7 +88,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
88
88
| 047 | [ Permutations II] ( https://leetcode.com/problems/permutations-ii/description/ ) | [ 无] | [ C++] ( 0047-Permutations-II/cpp-0047/ ) | | |
89
89
| 048 | [ Rotate Image] ( https://leetcode.com/problems/rotate-image/description/ ) | [ solution] ( https://leetcode.com/problems/rotate-image/ ) | [ C++] ( 0048-Rotate-Image/cpp-0048/ ) | | |
90
90
| 049 | [ Group Anagrams] ( https://leetcode.com/problems/group-anagrams/description/ ) | [ solution] ( https://leetcode.com/problems/group-anagrams/solution/ ) | [ C++] ( 0049-Group-Anagrams/cpp-0049/ ) | | |
91
- | | | | | | |
91
+ | 050 | [ Pow(x, n) ] ( https://leetcode.com/problems/powx-n/ ) | [ 无 ] | [ C++ ] ( 0050-Pow-x-n/cpp-0050/ ) | | |
92
92
| 051 | [ N-Queens] ( https://leetcode.com/problems/n-queens/description/ ) | [ 缺:N皇后问题整理] | [ C++] ( 0051-N-Queens/cpp-0051/ ) | [ Java] ( 0051-N-Queens/java-0051/src/ ) | |
93
93
| | | | | | |
94
94
| 054 | [ Spiral Matrix] ( https://leetcode.com/problems/spiral-matrix/description/ ) | [ solution] ( https://leetcode.com/problems/spiral-matrix/solution/ ) | [ C++] ( 0054-Spiral-Matrix/cpp-0054/ ) | | |
You can’t perform that action at this time.
0 commit comments