From 52750d3a17c85ba006eed636f2fd33b6089dd9fa Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Thu, 25 Nov 2021 11:59:19 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 57aa5a01..6c6f0cb7 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -283,6 +283,47 @@ var isValid = function(s) { }; ``` +C: +```C +//辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False +int notMatch(char par, char* stack, int stackTop) { + switch(par) { + case ']': + return stack[stackTop - 1] != '['; + case ')': + return stack[stackTop - 1] != '('; + case '}': + return stack[stackTop - 1] != '{'; + } + return 0; +} + +bool isValid(char * s){ + int strLen = strlen(s); + //开辟栈空间 + char stack[5000]; + int stackTop = 0; + + //遍历字符串 + int i; + for(i = 0; i < strLen; i++) { + //取出当前下标所对应字符 + char tempChar = s[i]; + //若当前字符为左括号,则入栈 + if(tempChar == '(' || tempChar == '[' || tempChar == '{') + stack[stackTop++] = tempChar; + //若当前字符为右括号,且栈中无元素或右括号与栈顶元素不符,返回False + else if(stackTop == 0 || notMatch(tempChar, stack, stackTop)) + return 0; + //当前字符与栈顶元素为一对括号,将栈顶元素出栈 + else + stackTop--; + } + //若栈中有元素,返回False。若没有元素(stackTop为0),返回True + return !stackTop; +} +``` + -----------------------
From 009662f6350017914db18adcbf0cef8c1aa522d4 Mon Sep 17 00:00:00 2001 From: qiuxuewei Date: Fri, 26 Nov 2021 14:26:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2020.=20=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 57aa5a01..5f597a1a 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -283,6 +283,29 @@ var isValid = function(s) { }; ``` +Swift +```swift +func isValid(_ s: String) -> Bool { + var stack = [String.Element]() + for ch in s { + if ch == "(" { + stack.append(")") + } else if ch == "{" { + stack.append("}") + } else if ch == "[" { + stack.append("]") + } else { + let top = stack.last + if ch == top { + stack.removeLast() + } else { + return false + } + } + } + return stack.isEmpty +} +``` -----------------------
From 9b6e798285eeb28dcd0ab117c25a75ff03e85c38 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sat, 27 Nov 2021 19:39:02 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 95 ++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index f674835d..075734ea 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -450,6 +450,101 @@ var partition = function(s) { }; ``` +##C +```c +char** path; +int pathTop; +char*** ans; +int ansTop = 0; +int* ansSize; + +//将path中的字符串全部复制到ans中 +void copy() { + //创建一个临时tempPath保存path中的字符串 + char** tempPath = (char**)malloc(sizeof(char*) * pathTop); + int i; + for(i = 0; i < pathTop; i++) { + tempPath[i] = path[i]; + } + //保存tempPath + ans[ansTop] = tempPath; + //将当前path的长度(pathTop)保存在ansSize中 + ansSize[ansTop++] = pathTop; +} + +//判断字符串是否为回文字符串 +bool isPalindrome(char* str, int startIndex, int endIndex) { + //双指针法:当endIndex(右指针)的值比startIndex(左指针)大时进行遍历 + while(endIndex >= startIndex) { + //若左指针和右指针指向元素不一样,返回False + if(str[endIndex--] != str[startIndex++]) + return 0; + } + return 1; +} + +//切割从startIndex到endIndex子字符串 +char* cutString(char* str, int startIndex, int endIndex) { + //开辟字符串的空间 + char* tempString = (char*)malloc(sizeof(char) * (endIndex - startIndex + 2)); + int i; + int index = 0; + //复制子字符串 + for(i = startIndex; i <= endIndex; i++) + tempString[index++] = str[i]; + //用'\0'作为字符串结尾 + tempString[index] = '\0'; + return tempString; +} + +void backTracking(char* str, int strLen, int startIndex) { + if(startIndex >= strLen) { + //将path拷贝到ans中 + copy(); + return ; + } + + int i; + for(i = startIndex; i < strLen; i++) { + //若从subString到i的子串是回文字符串,将其放入path中 + if(isPalindrome(str, startIndex, i)) { + path[pathTop++] = cutString(str, startIndex, i); + } + //若从startIndex到i的子串不为回文字符串,跳过这一层 + else { + continue; + } + //递归判断下一层 + backTracking(str, strLen, i + 1); + //回溯,将path中最后一位元素弹出 + pathTop--; + } +} + +char*** partition(char* s, int* returnSize, int** returnColumnSizes){ + int strLen = strlen(s); + //因为path中的字符串最多为strLen个(即单个字符的回文字符串),所以开辟strLen个char*空间 + path = (char**)malloc(sizeof(char*) * strLen); + //存放path中的数组结果 + ans = (char***)malloc(sizeof(char**) * 40000); + //存放ans数组中每一个char**数组的长度 + ansSize = (int*)malloc(sizeof(int) * 40000); + ansTop = pathTop = 0; + + //回溯函数 + backTracking(s, strLen, 0); + + //将ansTop设置为ans数组的长度 + *returnSize = ansTop; + //设置ans数组中每一个数组的长度 + *returnColumnSizes = (int*)malloc(sizeof(int) * ansTop); + int i; + for(i = 0; i < ansTop; ++i) { + (*returnColumnSizes)[i] = ansSize[i]; + } + return ans; +} +``` -----------------------
From 9fd8ea55fa2cc9a0a473481894cffe500c4b2afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Mon, 29 Nov 2021 14:07:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20Swift?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...删除字符串中的所有相邻重复项.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 66f34978..e43ab59f 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -319,6 +319,22 @@ char * removeDuplicates(char * s){ } ``` +Swift: +```swift +func removeDuplicates(_ s: String) -> String { + let array = Array(s) + var stack = [Character]() + for c in array { + let last: Character? = stack.last + if stack.isEmpty || last != c { + stack.append(c) + } else { + stack.removeLast() + } + } + return String(stack) +} +``` -----------------------
From 997ffb1061e6cb78a1004fd4a4718100fd750ba9 Mon Sep 17 00:00:00 2001 From: ORain <58782338+ORainn@users.noreply.github.com> Date: Mon, 29 Nov 2021 18:51:29 +0800 Subject: [PATCH 5/5] =?UTF-8?q?python=E9=80=92=E5=BD=92=E6=B3=95=20?= =?UTF-8?q?=E6=9B=B4=E5=BF=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python递归法 更快 --- problems/0654.最大二叉树.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index af945aca..361a92d1 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -256,6 +256,23 @@ class Solution { ## Python ```python +class Solution: + """递归法 更快""" + def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: + if not nums: + return None + maxvalue = max(nums) + index = nums.index(maxvalue) + + root = TreeNode(maxvalue) + + left = nums[:index] + right = nums[index + 1:] + + root.left = self.constructMaximumBinaryTree(left) + root.right = self.constructMaximumBinaryTree(right) + return root + class Solution: """最大二叉树 递归法"""