diff --git a/leetcode/cpp/string/151.cpp b/leetcode/cpp/string/151.cpp new file mode 100644 index 00000000..d633b4c8 --- /dev/null +++ b/leetcode/cpp/string/151.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string reverseWords(string s) { + string result; + int i = 0; + int n = s.length(); + + while(i= n){ + break; + } + int j = i+1; + while(j < n && s[j] != ' ')j++; + string sub = s.substr(i, j-i); + if(result.length()==0){ + result = sub; + } + else{ + result = sub + " " + resul; + } + i = j+1; + } + return result; + } +};