zoukankan      html  css  js  c++  java
  • LeetCode151:Reverse Words in a String

    题目:

    Given an input string, reverse the string word by word.

    For example,
         Given s = "the sky is blue",
         return "blue is sky the".

    Clarification:

    • What constitutes a word?
      A sequence of non-space characters constitutes a word.
    • Could the input string contain leading or trailing spaces?
      Yes. However, your reversed string should not contain leading or trailing spaces.
    • How about multiple spaces between two words?
      Reduce them to a single space in the reversed string.

    实现代码:

    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <string>
    #include <sstream>
    using namespace std;
    
    /*
    
    Given an input string, reverse the string word by word.
    
    For example,
    Given s = "the sky is blue",
    return "blue is sky the".
    
    Clarification:
    What constitutes a word?
    A sequence of non-space characters constitutes a word.
    Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
    How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.
    */
    
    class Solution {
    public:
        void reverseWords(string &s) 
        {
            
            reverse(s.begin(), s.end());
            string::iterator p_iter = s.begin();
            string::iterator q_iter;
            
            //去除最前面和最后面的空白符
            int index = s.find_first_not_of(' ');
            if(index == string::npos)//如果字符串全为空白符 
            {
                s.clear();
                return;
            }
            s.erase(s.begin(), s.begin()+index);
            int index2 = s.find_last_not_of(' ');
            s.erase(s.begin()+index2+1, s.end()); 
                            
            p_iter = s.begin();
            while(p_iter != s.end())
            {            
                q_iter = p_iter;
                while(*p_iter != ' ' && p_iter != s.end()) p_iter++;
                reverse(q_iter, p_iter);
                bool first = true;
                while(*p_iter == ' ' && p_iter != s.end())//去掉单词间多个空白符,只留下一个 
                {
                    if(first)
                    {
                        p_iter++;
                        first = false;
                    }                    
                    else
                        p_iter = s.erase(p_iter);
                }
    
            }
        }
    
        
     
    };
    
    int main(void)
    {
        string str = " this is   zhou ";
        Solution solution;
        solution.reverseWords(str);
        cout<<str<<endl;
        return 0;
    }
  • 相关阅读:
    《Linux性能及调优指南》第二章:监控和基准工具2.1-2.2
    《Linux 性能及调优指南》1.5 网络子系统
    《Linux 性能及调优指南》1.4 硬盘I/O子系统
    《Linux性能及调优指南》1.3 Linux文件系统
    《linux性能及调优指南》 3.5 网络瓶颈
    《linux性能及调优指南》 3.4 硬盘瓶颈
    《linux性能及调优指南》 3.3 内存瓶颈
    你应该使用Python3里的这些新特性
    python异步编程
    异步网络编程aiohttp的使用
  • 原文地址:https://www.cnblogs.com/mickole/p/3669584.html
Copyright © 2011-2022 走看看