042_翻转单词顺序

/*
*
Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std; 
class Solution {
public:
    void reverseWords(string &s) {
        
        int length = s.length();
        int start = 0;
        int end = 0;
	
        reverse(s, 0, length - 1);
        
        while(end <= length - 1){
            end = s.find(" ",start);
            if(end != s.npos){
              reverse(s, start, end - 1);
              start = end + 1;
            } else {
              end = length - 1; 
              reverse(s, start, end);
              break;
            }
            
        }
        

        
    }
   

    void reverse(string &s, int start, int end){
        int i = 0;
        char temp;
        while(start < end){
            temp = s[start];
            s[start] = s[end];
            s[end] = temp;
            start++;
            end--;
        }
    }
    
    
};

int main(){
	Solution solution;
	string s = "Sky is blue";
	solution.reverseWords(s);
	cout<<s<<endl;
	return 1;
}



















原文地址:https://www.cnblogs.com/blfshiye/p/4278659.html