Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
youngyangyang04
2021-12-01 12:37:37 +08:00
4 changed files with 193 additions and 0 deletions

View File

@ -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;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -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;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -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:
"""最大二叉树 递归法"""

View File

@ -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)
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>