Skip to content

Commit f35c5c5

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 53a4a17 + e7c8d01 commit f35c5c5

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

problems/0093.复原IP地址.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,37 @@ class Solution:
467467
num = int(s[start:end+1])
468468
return 0 <= num <= 255
469469

470+
回溯(版本三)
470471

471-
472-
472+
```python
473+
class Solution:
474+
def restoreIpAddresses(self, s: str) -> List[str]:
475+
result = []
476+
self.backtracking(s, 0, [], result)
477+
return result
478+
479+
def backtracking(self, s, startIndex, path, result):
480+
if startIndex == len(s):
481+
result.append('.'.join(path[:]))
482+
return
483+
484+
for i in range(startIndex, min(startIndex+3, len(s))):
485+
# 如果 i 往后遍历了,并且当前地址的第一个元素是 0 ,就直接退出
486+
if i > startIndex and s[startIndex] == '0':
487+
break
488+
# 比如 s 长度为 5,当前遍历到 i = 3 这个元素
489+
# 因为还没有执行任何操作,所以此时剩下的元素数量就是 5 - 3 = 2 ,即包括当前的 i 本身
490+
# path 里面是当前包含的子串,所以有几个元素就表示储存了几个地址
491+
# 所以 (4 - len(path)) * 3 表示当前路径至多能存放的元素个数
492+
# 4 - len(path) 表示至少要存放的元素个数
493+
if (4 - len(path)) * 3 < len(s) - i or 4 - len(path) > len(s) - i:
494+
break
495+
if i - startIndex == 2:
496+
if not int(s[startIndex:i+1]) <= 255:
497+
break
498+
path.append(s[startIndex:i+1])
499+
self.backtracking(s, i+1, path, result)
500+
path.pop()
473501
```
474502

475503
### Go

problems/0343.整数拆分.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,29 @@ class Solution {
243243
}
244244
}
245245
```
246+
贪心
247+
```Java
248+
class Solution {
249+
public int integerBreak(int n) {
250+
// with 贪心
251+
// 通过数学原理拆出更多的3乘积越大,则
252+
/**
253+
@Param: an int, the integer we need to break.
254+
@Return: an int, the maximum integer after breaking
255+
@Method: Using math principle to solve this problem
256+
@Time complexity: O(1)
257+
**/
258+
if(n == 2) return 1;
259+
if(n == 3) return 2;
260+
int result = 1;
261+
while(n > 4) {
262+
n-=3;
263+
result *=3;
264+
}
265+
return result*n;
266+
}
267+
}
268+
```
246269

247270
### Python
248271
动态规划(版本一)

0 commit comments

Comments
 (0)