Skip to content

Commit 33a7f01

Browse files
committed
50 51 60 70
1 parent 5ccbaf4 commit 33a7f01

File tree

8 files changed

+226
-1
lines changed

8 files changed

+226
-1
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
## 动态规划
4242

4343
+ No.46 [cpp]() [python](python/46_dp.py)
44+
+ No.70 [cpp](cpp/70.cpp) [python](python/70.py) easy
4445
+ No.53 [cpp](cpp/53.cpp) [python](python/53.py)
4546
+ No.120 [cpp](cpp/120.cpp) [python](python/120.py)
4647
+ No.837 [cpp](cpp/837.cpp) [python](python/837.py)
48+
+ No.126 [cpp](cpp/126.cpp) [python](python/126.py)
4749

4850
## 递归
4951

@@ -57,10 +59,11 @@
5759

5860
## 二分
5961

62+
+ No.50 快速幂 [cpp](cpp/50.cpp) [python](python/50.py)
6063
+ No.128 [cpp](cpp/128.cpp) [python](python/128.py)
6164

6265
### 剑指offer系列
6366

6467
+ No.45 [cpp](#) [python](剑指offer/python/45.py)
65-
+ No.51 [cpp](剑指offer/cpp/51.cpp) [python]
68+
+ No.51 [cpp](剑指offer/cpp/51.cpp) [python](剑指offer/python/51.py)
6669
+ No.59 [cpp](剑指offer/cpp/59.cpp)

cpp/50.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <climits>
2+
#include <cmath>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
double myPow(double x, int n) {
8+
/**
9+
* 用时: 4ms
10+
* 内存: 5.9MB
11+
*/
12+
// printf("%d", INT_MIN);
13+
bool flag = false;
14+
if (n < 0) {
15+
if (n != INT_MIN)
16+
n = -n;
17+
else {
18+
n = -(n+1);
19+
flag = true;
20+
}
21+
x = 1/x;
22+
}
23+
double base = x;
24+
double res = 1.0;
25+
for (;n; n = n >> 1, base *= base) {
26+
if (n % 2) {
27+
res *= base;
28+
}
29+
}
30+
if (flag)
31+
res *= x;
32+
return res;
33+
}
34+
double myPow2(double x, int n) {
35+
/*优化*/
36+
/**
37+
* 用时: 0ms
38+
* 内存: 6MB
39+
*/
40+
if (n == 1.0) return 1.0;
41+
if (n < 0) x = 1/x;
42+
unsigned int m = abs(n); // 取代了复杂的变换
43+
double res = 1.0;
44+
double base = x;
45+
for (; m; m = m>>1, base *= base)
46+
if (m%2) res *= base;
47+
return res;
48+
}
49+
50+
};

cpp/51.cpp

Whitespace-only changes.

cpp/70.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int climbStairs(int n) {
4+
int a = 1;
5+
int b = 1;
6+
int temp;
7+
for (int i = 2; i <= n; ++i) {
8+
temp = b;
9+
b = a + b;
10+
a = temp;
11+
}
12+
return b;
13+
}
14+
};

python/50.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def myPow(self, x: float, n: int) -> float:
3+
# 快速幂算法
4+
if n < 0:
5+
x = 1/x
6+
n = -n
7+
base = x
8+
res = 1.0
9+
while n:
10+
if n % 2:
11+
res *= base
12+
n = n >> 1
13+
base *= base
14+
return res

python/51.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
def conflict(state, nextX):
2+
nextY = len(state)
3+
for i in range(nextY):
4+
if abs(state[i] - nextX) in (0, nextY-i):
5+
return True
6+
return False
7+
8+
def queens(num, state):
9+
for pos in range(num):
10+
if not conflict(state, pos):
11+
if len(state) == num-1:
12+
yield (pos, )
13+
else:
14+
for result in queens(num, state+(pos,)):
15+
yield (pos, ) + result
16+
17+
def prettyprint(solution):
18+
def line(pos, lenght=len(solution)):
19+
return '.'*(pos) + 'Q' + '.' * (lenght-pos-1)
20+
res = []
21+
for pos in solution:
22+
res.append(line(pos))
23+
24+
25+
class Solution:
26+
def solveNQueens1(self, n: int) -> List[List[str]]:
27+
28+
res = []
29+
for s in queens(num=n, state=()):
30+
print(s)
31+
res.append(prettyprint(s))
32+
print(len(res))
33+
return res
34+
35+
def solveNQueens2(self, n: int) -> List[List[str]]:
36+
arr = list(range(n))
37+
38+
def _check(arr):
39+
for i in range(0, n):
40+
for j in range(i+1, n):
41+
if abs(i-j) == abs(arr[i] - arr[j]):
42+
return False
43+
return True
44+
45+
_res = []
46+
def _dfs(pos):
47+
48+
if pos == n-1:
49+
if _check(arr):
50+
_res.append(arr[:])
51+
else:
52+
for i in range(pos, n):
53+
54+
arr[pos], arr[i] = arr[i], arr[pos]
55+
_dfs(pos+1)
56+
arr[pos], arr[i] = arr[i], arr[pos]
57+
58+
_dfs(0)
59+
res = []
60+
61+
def _replace(pos):
62+
str_res = ['.']*n
63+
str_res[pos] = 'Q'
64+
return ''.join(str_res)
65+
66+
for res_one in _res:
67+
res.append([_replace(i) for i in res_one])
68+
return res
69+
70+
def solveNQueens3(self, n: int) -> List[List[str]]:
71+
72+
hashtable = [False] * (n+1)
73+
P = list(range(n+1))
74+
res = []
75+
76+
def _replace(pos):
77+
str_res = ['.']*n
78+
str_res[pos-1] = 'Q'
79+
return ''.join(str_res)
80+
81+
def dfs(index):
82+
if index == n+1:
83+
temp = []
84+
for i in range(1, n+1):
85+
temp.append(_replace(P[i]))
86+
res.append(temp)
87+
88+
for x in range(1, n+1):
89+
if not hashtable[x]:
90+
flag = True
91+
for pre in range(1, index):
92+
if abs(index - pre) == abs(x - P[pre]):
93+
flag = False
94+
break
95+
96+
if flag:
97+
P[index] = x
98+
hashtable[x] = True
99+
dfs(index+1)
100+
hashtable[x] = False
101+
dfs(1)
102+
return res
103+

python/70.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
a, b = 1, 1
4+
for i in range(1, n):
5+
a, b = b, a+b
6+
return b

剑指offer/python/60.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def twoSum(self, n):
3+
dp = [[0 for j in range(n*6+1)] for i in range(n)]
4+
dp[0][1:6] = ([1] * 6)
5+
for i in range(1, n):
6+
for j in range(i, i*6+7):
7+
for k in range(1, 7):
8+
if j >= k+i-1 and j-k <= 6*n: # 优化 因为 n 不需要再计算n-1
9+
dp[i][j] += dp[i-1][j-k]
10+
11+
res = []
12+
for i in range(n, n*6+1):
13+
res.append(dp[n-1][i]*1.0/6**n)
14+
return res
15+
16+
def twoSum_2(self, n):
17+
dp = [0 for j in range(n*6+1)]
18+
dp[1:6] = ([1] * 6)
19+
for i in range(2, n+1):
20+
for j in range(i*6, i-1, -1):
21+
dp[j] = 0
22+
for k in range(1, 7):
23+
if j >= k+i-1 : # 优化
24+
dp[j] += dp[j-k] # 压缩存储
25+
res = []
26+
for i in range(n, n*6+1):
27+
res.append(dp[i]*1.0/6**n)
28+
29+
return res
30+
31+
32+
33+
if __name__ == "__main__":
34+
s = Solution()
35+
s.twoSum_2(2)

0 commit comments

Comments
 (0)