parent
ff96d8cc08
commit
8ef273330d
@ -1016,6 +1016,40 @@ private void initDigitSum() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
class Solution:
|
||||||
|
def __init__(self):
|
||||||
|
self.acc = 0
|
||||||
|
|
||||||
|
def movingCount(self, threshold, rows, cols):
|
||||||
|
# write code here
|
||||||
|
self.threshold = threshold
|
||||||
|
self.rows = rows
|
||||||
|
self.cols = cols
|
||||||
|
self.board = [[0 for _ in range(cols)] for _ in range(rows)]
|
||||||
|
|
||||||
|
self.traverse(0,0)
|
||||||
|
return self.acc
|
||||||
|
|
||||||
|
def block(self, r, c):
|
||||||
|
s = sum(map(int,str(r)+str(c)))
|
||||||
|
return s>self.threshold
|
||||||
|
|
||||||
|
def traverse(self, r, c):
|
||||||
|
if not (0<=r<self.rows and 0<=c<self.cols): return
|
||||||
|
if self.board[r][c]!=0: return
|
||||||
|
if self.board[r][c]==-1 or self.block(r,c):
|
||||||
|
self.board[r][c]=-1
|
||||||
|
return
|
||||||
|
self.board[r][c] = 1
|
||||||
|
self.acc += 1
|
||||||
|
self.traverse(r+1,c)
|
||||||
|
self.traverse(r-1,c)
|
||||||
|
self.traverse(r,c+1)
|
||||||
|
self.traverse(r,c-1)
|
||||||
|
```
|
||||||
|
|
||||||
# 14. 剪绳子
|
# 14. 剪绳子
|
||||||
|
|
||||||
[Leetcode](https://leetcode.com/problems/integer-break/description/)
|
[Leetcode](https://leetcode.com/problems/integer-break/description/)
|
||||||
@ -1069,6 +1103,21 @@ public int integerBreak(int n) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
def integerBreak(n):
|
||||||
|
if n < 2:
|
||||||
|
return 0
|
||||||
|
if n == 2:
|
||||||
|
return 1
|
||||||
|
if n == 3:
|
||||||
|
return 2
|
||||||
|
timesOf3 = n // 3
|
||||||
|
if n - timesOf3 * 3 == 1:
|
||||||
|
timesOf3 -= 1
|
||||||
|
timesOf2 = (n - timesOf3 * 3) // 2
|
||||||
|
return pow(3, timesOf3) * pow(2, timesOf2)
|
||||||
|
```
|
||||||
|
|
||||||
# 参考文献
|
# 参考文献
|
||||||
|
|
||||||
- 何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.
|
- 何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user