From 0ab400b0fc8c7864230c32d6f27b2efd8dc5c108 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Tue, 24 Aug 2021 14:43:28 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0131.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=B8=B2python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 6fe2758b..afb8cbb7 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -292,7 +292,8 @@ class Solution { ``` Python: -```py +```python +# 版本一 class Solution: def partition(self, s: str) -> List[List[str]]: res = [] @@ -310,7 +311,36 @@ class Solution: return res ``` - +```python +# 版本二 +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + path = [] #放已经回文的子串 + # 双指针法判断是否是回文串 + def isPalindrome(s): + n = len(s) + i, j = 0, n - 1 + while i < j: + if s[i] != s[j]:return False + i += 1 + j -= 1 + return True + + def backtrack(s, startIndex): + if startIndex >= len(s): # 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了 + res.append(path[:]) + return + for i in range(startIndex, len(s)): + p = s[startIndex:i+1] # 获取[startIndex,i+1]在s中的子串 + if isPalindrome(p): # 是回文子串 + path.append(p) + else: continue #不是回文,跳过 + backtrack(s, i + 1) + path.pop() #回溯过程,弹出本次已经填在path的子串 + backtrack(s, 0) + return res +``` Go: > 注意切片(go切片是披着值类型外衣的引用类型) From b57023340989dd6070628ba09766e4ea6b305f59 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Tue, 24 Aug 2021 15:41:42 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80python3=E7=89=88=E6=9C=AC=EF=BC=8C=E6=80=9D?= =?UTF-8?q?=E8=B7=AF=E6=B8=85=E6=99=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 9a95af22..1d39df75 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -309,7 +309,34 @@ class Solution { ``` python版本: +```python +class Solution: + def restoreIpAddresses(self, s: str) -> List[str]: + res = [] + path = [] # 存放分割后的字符 + # 判断数组中的数字是否合法 + def isValid(p): + if p == '0': return True # 解决"0000" + if p[0] == '0': return False + if int(p) > 0 and int(p) <256: return True + return False + + def backtrack(s, startIndex): + if len(s) > 12: return # 字符串长度最大为12 + if len(path) == 4 and startIndex == len(s): # 确保切割完,且切割后的长度为4 + res.append(".".join(path[:])) # 字符拼接 + return + for i in range(startIndex, len(s)): + p = s[startIndex:i+1] # 分割字符 + if isValid(p): # 判断字符是否有效 + path.append(p) + else: continue + backtrack(s, i + 1) # 寻找i+1为起始位置的子串 + path.pop() + backtrack(s, 0) + return res +``` ```python class Solution(object): def restoreIpAddresses(self, s): From 649fe7a202b348300853a059bb1426035c7f2563 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Tue, 24 Aug 2021 16:15:36 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80python3=E7=89=88=E6=9C=AC=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=89=AA=E6=9E=9D=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 1d39df75..74c6df77 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -320,14 +320,15 @@ class Solution: if p[0] == '0': return False if int(p) > 0 and int(p) <256: return True return False - + def backtrack(s, startIndex): if len(s) > 12: return # 字符串长度最大为12 if len(path) == 4 and startIndex == len(s): # 确保切割完,且切割后的长度为4 res.append(".".join(path[:])) # 字符拼接 - return + return for i in range(startIndex, len(s)): + if len(s) - startIndex > 3*(4 - len(path)): continue # 剪枝,剩下的字符串大于允许的最大长度则跳过 p = s[startIndex:i+1] # 分割字符 if isValid(p): # 判断字符是否有效 path.append(p) From 816e8afacadfd6b61b10fb02edeeb6dc657d436e Mon Sep 17 00:00:00 2001 From: ironartisan Date: Wed, 25 Aug 2021 16:51:59 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=9B=B4=E6=AD=A30491.=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0491.递增子序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 8eeb434d..ea113f4b 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -184,7 +184,7 @@ public: 这份代码在leetcode上提交,要比版本一耗时要好的多。 -**所以正如在[哈希表:总结篇!(每逢总结必经典)](https://programmercarl.com/哈希表总结.html)中说的那样,数组,set,map都可以做哈希表,而且数组干的活,map和set都能干,但如何数值范围小的话能用数组尽量用数组**。 +**所以正如在[哈希表:总结篇!(每逢总结必经典)](https://programmercarl.com/哈希表总结.html)中说的那样,数组,set,map都可以做哈希表,而且数组干的活,map和set都能干,但如果数值范围小的话能用数组尽量用数组**。 From 55bf380c4527f16985ea00cf1373c612217061d5 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 26 Aug 2021 09:13:13 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E8=B0=83=E6=95=B4N=E7=9A=87=E5=90=8EMarkdo?= =?UTF-8?q?wn=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 15 ++++++++++++--- problems/0052.N皇后II.md | 11 +++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index 5404b620..fd2ac4fa 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -21,18 +21,27 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并 每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。 示例: + 输入: 4 -输出: [ - [".Q..", // 解法 1 + +输出: + +解法 1 + +[ + [".Q..", "...Q", "Q...", "..Q."], - ["..Q.", // 解法 2 +解法 2 + + ["..Q.", "Q...", "...Q", ".Q.."] ] + 解释: 4 皇后问题存在两个不同的解法。 提示: diff --git a/problems/0052.N皇后II.md b/problems/0052.N皇后II.md index 81f5c7ea..1a34f763 100644 --- a/problems/0052.N皇后II.md +++ b/problems/0052.N皇后II.md @@ -22,15 +22,22 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并 示例: 输入: 4 + 输出: 2 + 解释: 4 皇后问题存在如下两个不同的解法。 + +解法 1 + [ - [".Q..",  // 解法 1 + [".Q..",   "...Q",   "Q...",   "..Q."], - ["..Q.",  // 解法 2 +解法 2 + + ["..Q.",   "Q...",   "...Q",   ".Q.."]