Skip to content

Commit 55977d3

Browse files
Merge branch 'master' into master
2 parents 009662f + a5ed404 commit 55977d3

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

problems/0020.有效的括号.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ var isValid = function(s) {
283283
};
284284
```
285285

286+
286287
Swift
287288
```swift
288289
func isValid(_ s: String) -> Bool {
@@ -307,5 +308,46 @@ func isValid(_ s: String) -> Bool {
307308
}
308309
```
309310

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+
310352
-----------------------
311353
<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>

0 commit comments

Comments
 (0)