We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5205880 commit 43682e2Copy full SHA for 43682e2
2018.11.28-leetcode151/018429.md
@@ -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
31
32
+ else
33
34
+ push(s[i--],stack);
35
36
37
+ if(s[++i]!=' ')
38
39
40
41
+ s[j]=' ';
42
43
44
+ j--;
45
+ s1[j]='\0';
46
+ s = strcpy(s,s1);
47
+ free(s1);
48
0 commit comments