diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 57aa5a01..ec49c1c9 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -284,5 +284,70 @@ 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 +} +``` + +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; +} +``` + -----------------------
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; +} +``` -----------------------
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: """最大二叉树 递归法""" 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) +} +``` -----------------------