zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  • 相关阅读:
    char类型细节
    Hibernate面试题
    线程
    IO流
    集合
    链表相关的一点东西
    正则表达式学习
    python中的变量域问题
    python的输出和输入形式
    python mutable 和 immutable
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4278659.html
Copyright © 2011-2022 走看看