zoukankan      html  css  js  c++  java
  • leetcoder系列001:c++字符串反转

    问题:

    给定一个输入字符串字符串反向。例如

     s = "the sky is blue",
    返回 "blue is sky the".

    我的答案:

    class Solution {
    public:
        void reverseWords(string &s) {
            if(s.size() <= 0)
                return;
                
            string pattern = " ";
            string::size_type pos;
            vector<string> result;
            s += pattern;
            string::size_type size = s.size();
            for(int i = 0;i < size;i++){
                pos = s.find(pattern, i);
                if(pos < size){
                    string word = s.substr(i, pos - i);
                    if(word.find(pattern) == -1 && word != "")
                        result.push_back(word);
                    i = pos + pattern.size() - 1;
                }
            }
            
            s = "";
            if(result.size() <= 0)
                return;
            for(int i = result.size() - 1; i >= 1 ; i--){
              s += result[i];
              s += " ";
            }
            s += result[0];
        }
    };

     

  • 相关阅读:
    Python保留最后N个元素
    STL算法
    STL迭代器
    STL容器
    C++总结1
    牛客剑指Offer2
    Vue第一天
    UML
    Java继承和组合代码
    Java15后的sealed阻止继承滥用
  • 原文地址:https://www.cnblogs.com/xskCoder/p/3920672.html
Copyright © 2011-2022 走看看