Skip to content

Commit a6a0bc5

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 331d3a4 + bb4abed commit a6a0bc5

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

problems/0020.有效的括号.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,70 @@ var isValid = function(s) {
284284
```
285285

286286

287+
Swift
288+
```swift
289+
func isValid(_ s: String) -> Bool {
290+
var stack = [String.Element]()
291+
for ch in s {
292+
if ch == "(" {
293+
stack.append(")")
294+
} else if ch == "{" {
295+
stack.append("}")
296+
} else if ch == "[" {
297+
stack.append("]")
298+
} else {
299+
let top = stack.last
300+
if ch == top {
301+
stack.removeLast()
302+
} else {
303+
return false
304+
}
305+
}
306+
}
307+
return stack.isEmpty
308+
}
309+
```
310+
311+
C:
312+
```C
313+
//辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False
314+
int notMatch(char par, char* stack, int stackTop) {
315+
switch(par) {
316+
case ']':
317+
return stack[stackTop - 1] != '[';
318+
case ')':
319+
return stack[stackTop - 1] != '(';
320+
case '}':
321+
return stack[stackTop - 1] != '{';
322+
}
323+
return 0;
324+
}
325+
326+
bool isValid(char * s){
327+
int strLen = strlen(s);
328+
//开辟栈空间
329+
char stack[5000];
330+
int stackTop = 0;
331+
332+
//遍历字符串
333+
int i;
334+
for(i = 0; i < strLen; i++) {
335+
//取出当前下标所对应字符
336+
char tempChar = s[i];
337+
//若当前字符为左括号,则入栈
338+
if(tempChar == '(' || tempChar == '[' || tempChar == '{')
339+
stack[stackTop++] = tempChar;
340+
//若当前字符为右括号,且栈中无元素或右括号与栈顶元素不符,返回False
341+
else if(stackTop == 0 || notMatch(tempChar, stack, stackTop))
342+
return 0;
343+
//当前字符与栈顶元素为一对括号,将栈顶元素出栈
344+
else
345+
stackTop--;
346+
}
347+
//若栈中有元素,返回False。若没有元素(stackTop为0),返回True
348+
return !stackTop;
349+
}
350+
```
351+
287352
-----------------------
288353
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0131.分割回文串.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,101 @@ var partition = function(s) {
450450
};
451451
```
452452

453+
##C
454+
```c
455+
char** path;
456+
int pathTop;
457+
char*** ans;
458+
int ansTop = 0;
459+
int* ansSize;
460+
461+
//将path中的字符串全部复制到ans中
462+
void copy() {
463+
//创建一个临时tempPath保存path中的字符串
464+
char** tempPath = (char**)malloc(sizeof(char*) * pathTop);
465+
int i;
466+
for(i = 0; i < pathTop; i++) {
467+
tempPath[i] = path[i];
468+
}
469+
//保存tempPath
470+
ans[ansTop] = tempPath;
471+
//将当前path的长度(pathTop)保存在ansSize中
472+
ansSize[ansTop++] = pathTop;
473+
}
474+
475+
//判断字符串是否为回文字符串
476+
bool isPalindrome(char* str, int startIndex, int endIndex) {
477+
//双指针法:当endIndex(右指针)的值比startIndex(左指针)大时进行遍历
478+
while(endIndex >= startIndex) {
479+
//若左指针和右指针指向元素不一样,返回False
480+
if(str[endIndex--] != str[startIndex++])
481+
return 0;
482+
}
483+
return 1;
484+
}
485+
486+
//切割从startIndex到endIndex子字符串
487+
char* cutString(char* str, int startIndex, int endIndex) {
488+
//开辟字符串的空间
489+
char* tempString = (char*)malloc(sizeof(char) * (endIndex - startIndex + 2));
490+
int i;
491+
int index = 0;
492+
//复制子字符串
493+
for(i = startIndex; i <= endIndex; i++)
494+
tempString[index++] = str[i];
495+
//用'\0'作为字符串结尾
496+
tempString[index] = '\0';
497+
return tempString;
498+
}
499+
500+
void backTracking(char* str, int strLen, int startIndex) {
501+
if(startIndex >= strLen) {
502+
//将path拷贝到ans中
503+
copy();
504+
return ;
505+
}
506+
507+
int i;
508+
for(i = startIndex; i < strLen; i++) {
509+
//若从subString到i的子串是回文字符串,将其放入path中
510+
if(isPalindrome(str, startIndex, i)) {
511+
path[pathTop++] = cutString(str, startIndex, i);
512+
}
513+
//若从startIndex到i的子串不为回文字符串,跳过这一层
514+
else {
515+
continue;
516+
}
517+
//递归判断下一层
518+
backTracking(str, strLen, i + 1);
519+
//回溯,将path中最后一位元素弹出
520+
pathTop--;
521+
}
522+
}
523+
524+
char*** partition(char* s, int* returnSize, int** returnColumnSizes){
525+
int strLen = strlen(s);
526+
//因为path中的字符串最多为strLen个(即单个字符的回文字符串),所以开辟strLen个char*空间
527+
path = (char**)malloc(sizeof(char*) * strLen);
528+
//存放path中的数组结果
529+
ans = (char***)malloc(sizeof(char**) * 40000);
530+
//存放ans数组中每一个char**数组的长度
531+
ansSize = (int*)malloc(sizeof(int) * 40000);
532+
ansTop = pathTop = 0;
533+
534+
//回溯函数
535+
backTracking(s, strLen, 0);
536+
537+
//将ansTop设置为ans数组的长度
538+
*returnSize = ansTop;
539+
//设置ans数组中每一个数组的长度
540+
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
541+
int i;
542+
for(i = 0; i < ansTop; ++i) {
543+
(*returnColumnSizes)[i] = ansSize[i];
544+
}
545+
return ans;
546+
}
547+
```
453548
454549
-----------------------
455550
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0654.最大二叉树.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ class Solution {
256256
## Python
257257

258258
```python
259+
class Solution:
260+
"""递归法 更快"""
261+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
262+
if not nums:
263+
return None
264+
maxvalue = max(nums)
265+
index = nums.index(maxvalue)
266+
267+
root = TreeNode(maxvalue)
268+
269+
left = nums[:index]
270+
right = nums[index + 1:]
271+
272+
root.left = self.constructMaximumBinaryTree(left)
273+
root.right = self.constructMaximumBinaryTree(right)
274+
return root
275+
259276
class Solution:
260277
"""最大二叉树 递归法"""
261278

problems/1047.删除字符串中的所有相邻重复项.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ char * removeDuplicates(char * s){
319319
}
320320
```
321321

322+
Swift:
323+
```swift
324+
func removeDuplicates(_ s: String) -> String {
325+
let array = Array(s)
326+
var stack = [Character]()
327+
for c in array {
328+
let last: Character? = stack.last
329+
if stack.isEmpty || last != c {
330+
stack.append(c)
331+
} else {
332+
stack.removeLast()
333+
}
334+
}
335+
return String(stack)
336+
}
337+
```
322338

323339
-----------------------
324340
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)