Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 5, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/b1d112693307760408262994cf9d9a4d to your computer and use it in GitHub Desktop.
Save jianminchen/b1d112693307760408262994cf9d9a4d to your computer and use it in GitHub Desktop.
being interviewer - sentence reverse
#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