String 初学者诚恳地询问有关弦乐的建议

String 初学者诚恳地询问有关弦乐的建议,string,return,String,Return,本课程所描述的是关于“反转字符串”,这是正确的,可以从Leetcode网站上使用。今天,我想通过自己输入一个值来演示“反转字符串”(如下面的int main()部分),但我想了很长时间后仍然无法执行它。初学者诚恳征求意见,也许你也可以附上你的文章,让我可以学习,谢谢 #include <iostream> #include <string> using namespace std; class Solution {

本课程所描述的是关于“反转字符串”,这是正确的,可以从Leetcode网站上使用。今天,我想通过自己输入一个值来演示“反转字符串”(如下面的int main()部分),但我想了很长时间后仍然无法执行它。初学者诚恳征求意见,也许你也可以附上你的文章,让我可以学习,谢谢

   #include <iostream>
    #include <string>
    using namespace std;
    
    class Solution
    {
    public:
        string reverseWords(string s)
        {
            if (s.size() == 0)
            { 
                return s;
            }
            int front = 0, back = 0; 
            for (int i = 0; i < s.size() - 1; i++)
            {
                if (s[i] != ' ')
                {
                    back++;
                }
                else
                {
                    reverse(s.begin() + front, s.begin() + back); 
                    front = back + 1;                             
                    back = front;                                
                }
            } 
            back++;
            reverse(s.begin() + front, s.begin() + back); 
            return s;
        }
    };
    
    int main()
    {
        Solution word01;
        string s1= "Hello caterpillar";
        word01 s1;
        cout << s1.reverseWords();
    }
#包括
#包括
使用名称空间std;
类解决方案
{
公众:
字符串反转符(字符串s)
{
如果(s.size()==0)
{ 
返回s;
}
int前=0,后=0;
对于(int i=0;i
  • 您的代码非常好,但是我们只想反转单词而不是字符,因为我们可以使用while循环

  • 类似地,使用两个指针,这将很好地传递:

  • //下面的代码块可能会大大缩短执行时间;
    //可以移除;
    静态常数自动优化{
    std::ios::与stdio同步(false);
    标准::cin.tie(空);
    标准::cout.tie(空);
    返回0;
    }();
    //大多数标题已经包含在内;
    //可以移除;
    #包括
    #包括
    #包括,您可以通过各种方法找到大量解释良好的公认解决方案,包括低复杂度算法和渐近/分析
    
    
    // The following block might trivially improve the exec time;
    // Can be removed;
    static const auto __optimize__ = []() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(NULL);
        std::cout.tie(NULL);
        return 0;
    }();
    
    // Most of headers are already included;
    // Can be removed;
    #include <cstdint>
    #include <string>
    #include <algorithm>
    
    static const struct Solution {
        using ValueType = std::uint_fast16_t;
        std::string reverseWords(std::string s) {
            std::reverse(std::begin(s), std::end(s));
            ValueType len = std::size(s);
            ValueType index = 0;
    
            for (auto left = 0; left < len; ++left) {
                if (s[left] != ' ') {
                    if (index) {
                        s[index++] = ' ';
                    }
    
                    ValueType right = left;
    
                    while (right < len && s[right] != ' ') {
                        s[index++] = s[right++];
                    }
    
                    std::reverse(std::begin(s) + index - (right - left), std::begin(s) + index);
                    left = right;
                }
            }
    
            s.erase(std::begin(s) + index, std::end(s));
            return s;
        }
    };
    
    class Solution {
      public:
      string reverseWords(string s) {
        // reverse the whole string
        reverse(s.begin(), s.end());
    
        int n = s.size();
        int idx = 0;
        for (int start = 0; start < n; ++start) {
          if (s[start] != ' ') {
            // go to the beginning of the word
            if (idx != 0) s[idx++] = ' ';
    
            // go to the end of the word
            int end = start;
            while (end < n && s[end] != ' ') s[idx++] = s[end++];
    
            // reverse the word
            reverse(s.begin() + idx - (end - start), s.begin() + idx);
    
            // move to the next word
            start = end;
          }
        }
        s.erase(s.begin() + idx, s.end());
        return s;
      }
    };