File tree Expand file tree Collapse file tree 4 files changed +143
-0
lines changed
solutions/151.Reverse_Words_in_a_String Expand file tree Collapse file tree 4 files changed +143
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Author: illuz <iilluzen[at]gmail.com>
3+ * Blog: http://blog.csdn.net/hcbbt
4+ * File: AC_reverse_n.c
5+ * Create Date: 2015-03-18 22:53:16
6+ * Descripton: O(1) space.
7+ */
8+
9+ #include <stdio.h>
10+ #include <string.h>
11+
12+ void reverse_sub (char * s , int len ) {
13+ char tmp ;
14+ int i ;
15+ for (i = 0 ; i < len / 2 ; ++ i ) {
16+ tmp = s [i ];
17+ s [i ] = s [len - i - 1 ];
18+ s [len - i - 1 ] = tmp ;
19+ }
20+ }
21+
22+ void reverseWords (char * s ) {
23+ int i = 0 ;
24+ int cur = 0 ; // current pos of dealed string
25+ int cnt = 0 ; // current words' length
26+ int word_cnt = 0 ;
27+
28+ while (1 ) {
29+ // skip spaces
30+ while (s [i ] == ' ' )
31+ ++ i ;
32+
33+ if (s [i ] == '\0' )
34+ break ;
35+
36+ if (word_cnt ++ )
37+ s [cur ++ ] = ' ' ;
38+ // count the length and move
39+ cnt = 0 ;
40+ while (s [i + cnt ] != '\0' && s [i + cnt ] != ' ' ) {
41+ if (cur != i )
42+ s [cur + cnt ] = s [i + cnt ];
43+ cnt ++ ;
44+ }
45+
46+ reverse_sub (s + cur , cnt );
47+
48+ // update the pos and add space
49+ i += cnt ;
50+ cur += cnt ;
51+ }
52+ s [cur ] = '\0' ;
53+ reverse_sub (s , cur );
54+ }
55+
56+ int main () {
57+ char str [100 ];
58+ while (gets (str )) {
59+ printf ("'%s'\n" , str );
60+ reverseWords (str );
61+ printf ("'%s'\n\n" , str );
62+ }
63+ return 0 ;
64+ }
65+
Original file line number Diff line number Diff line change 1+ /*
2+ * Author: illuz <iilluzen[at]gmail.com>
3+ * File: AC_split_n.java
4+ * Create Date: 2015-03-18 22:30:59
5+ * Descripton:
6+ */
7+
8+ import java .util .*;
9+
10+ public class Solution {
11+ public String reverseWords (String s ) {
12+ String [] words = s .trim ().split (" +" );
13+ int len = words .length ;
14+ if (len == 0 ) {
15+ return "" ;
16+ }
17+ StringBuilder sb = new StringBuilder (words [len - 1 ]);
18+ for (int i = len - 2 ; i >= 0 ; --i ) {
19+ sb .append (" " + words [i ]);
20+ }
21+ return sb .toString ();
22+ }
23+
24+ // debug
25+ public static void main (String [] args ) {
26+ Scanner cin = new Scanner (System .in );
27+ Solution s = new Solution ();
28+ int [] input = {1 , 2 , 3 , 4 };
29+ System .out .println ("no case" );
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+ # -*- coding: utf-8 -*-
3+ # Author: illuz <iilluzen[at]gmail.com>
4+ # File: AC_split_n.py
5+ # Create Date: 2015-03-18 21:49:19
6+ # Usage: AC_split_n.py
7+ # Descripton:
8+
9+
10+ class Solution :
11+ # @param s, a string
12+ # @return a string
13+ def reverseWords (self , s ):
14+ return ' ' .join (list (reversed (s .split ())))
Original file line number Diff line number Diff line change 1+ /*
2+ * Author: illuz <iilluzen[at]gmail.com>
3+ * File: AC_stringstream_n.cpp
4+ * Create Date: 2015-03-18 16:26:06
5+ * Descripton:
6+ */
7+
8+ #include < bits/stdc++.h>
9+
10+ using namespace std ;
11+ const int N = 0 ;
12+
13+ class Solution {
14+ public:
15+ void reverseWords (string &s) {
16+ stringstream is (s);
17+ is >> s;
18+ string tmp;
19+ while (is >> tmp)
20+ s = tmp + ' ' + s;
21+ if (s[0 ] == ' ' ) // if input is all space then s will be the same
22+ s = " " ;
23+ }
24+ };
25+
26+ int main () {
27+ Solution s;
28+ string str = " " ;
29+ s.reverseWords (str);
30+ cout << str << endl;
31+ return 0 ;
32+ }
33+
You can’t perform that action at this time.
0 commit comments