Created
May 5, 2018 17:55
-
-
Save jianminchen/b1d112693307760408262994cf9d9a4d to your computer and use it in GitHub Desktop.
being interviewer - sentence reverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
vector<char> reverseWords( const vector<char>& arr ) | |
{ | |
vector<char> ans; | |
int i; | |
int length = arr.size(); | |
for( i = length - 1; i >= 0; i-- ) | |
{ | |
bool currentIsSpace = arr[i] == ' '; | |
bool nextOneTrue = (i - 1) >= 0 ; // left to right | |
bool nextIsChar = currentIsSpace && nextOneTrue && arr[i - 1] != ' '; | |
bool multipleSpace = currentIsSpace && nextOneTrue && arr[i - 1] == ' '; | |
if(nextIsChar || i == 0){ // "a" " " " " "b" | |
for( int j = (i == 0? 0: 1); (i + j) < arr.size() && arr[i + j] != ' ' ; j++) // | |
{ | |
ans.push_back(arr[i + j]); | |
} | |
ans.push_back(' '); | |
} | |
else if(multipleSpace) | |
{ | |
ans.push_back(arr[i]); | |
} | |
} | |
return ans; | |
} | |
int main() { | |
return 0; | |
} | |
//"a cb cd" -> | |
// "a", "b", "cd" -> "cd", "b", "a" | |
// "cd b a" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment