Skip to content

Commit 43682e2

Browse files
authored
Create 018429.md
1 parent 5205880 commit 43682e2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

2018.11.28-leetcode151/018429.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
int top=0;
2+
void push(char c,char* stack)
3+
{
4+
stack[top++]=c;
5+
}
6+
char pop(char* stack)
7+
{
8+
return stack[--top];//写入s1
9+
}
10+
void reverseWords(char *s) {
11+
int i=0,j=0,flag=0;
12+
while(s[i]!='\0') i++;
13+
char* stack = (char*)malloc(i);
14+
char* s1 = (char*)malloc(i+1);
15+
i--;
16+
while(i>=0)
17+
{
18+
if(s[i]==' ')
19+
{
20+
while(top!=0)//如果栈不为空
21+
{
22+
flag=1;
23+
s1[j++]=pop(stack);
24+
}
25+
if(flag==1)
26+
{
27+
s1[j++]=' ';
28+
flag=0;//重置flag
29+
}
30+
i--;
31+
}
32+
else
33+
{
34+
push(s[i--],stack);
35+
}
36+
}
37+
if(s[++i]!=' ')
38+
{
39+
while(top!=0)//如果栈不为空
40+
s1[j++]=pop(stack);
41+
s[j]=' ';
42+
}
43+
else
44+
j--;
45+
s1[j]='\0';
46+
s = strcpy(s,s1);
47+
free(s1);
48+
}

0 commit comments

Comments
 (0)