• leetcode Reverse Words in a String


    将句子的词反转,例如:

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

    思路:就是从后面往前,找到非空格的长度,然后取到另一个串中。遍历一次就可以了。如下:

    class Solution {
    public:
        void reverseWords(string &s) {
            if (s.size() < 1) return ;
            int ind = s.size() - 1, wordLen = 0, tmpind;
            string ans = "";
            while(ind >=0)
            {
                wordLen = 0;
                while(ind >= 0 && s[ind] == ' ') ind--;
                tmpind = ind;
                while(ind >= 0 && s[ind] != ' ') ind--;
                wordLen = tmpind - ind;
                if (wordLen > 0)
                {
                    if (ans.size() == 0)
                        ans = s.substr(ind + 1, wordLen);
                    else
                        ans = ans + ' ' + s.substr(ind + 1, wordLen);
                }
                ind--;
            }
            s = ans;
        }
    };

    这次又出现 unlock the question,然后评分

  • 相关阅读:
    day3
    day2
    day1-存储
    day5-iptables
    MySQL之补充
    11.18
    11.17
    junit基础学习之-测试controller层(2)
    junit基础学习之-简介(1)
    外键和级联
  • 原文地址:https://www.cnblogs.com/higerzhang/p/4171010.html
Copyright © 2020-2023  润新知