简体   繁体   English

反向 C++ 向量<char>

[英]Reverse C++ vector<char>

I am not that great in C++.我在 C++ 方面不是很好。 Question is a simple reverse string.问题是一个简单的反向字符串。 This is a Leetcode question and I am trying to solve it recursively.这是一个 Leetcode 问题,我试图递归地解决它。

void reverse_str(vector<char>& s, int len)
{
   if (len <= 1) return;
        
   swap(s[0], s[len-1]);
   reverse_str(s.front(), len-2);  // Compilation error when I call s.front()
}

void reverseString(vector<char>& s)
{
    reverse_str(s, s.size());
}

I am trying to recursively call reverse_str with the reference to the 2nd element in the vector.我正在尝试使用对向量中第二个元素的引用递归调用 reverse_str。 How do I do that?我怎么做?

Thank You in advance.先感谢您。

You can do that in this way你可以这样做

void reverse_str(vector<char>& s, int len = 0) // default params with zero
{ 
    int n = s.size(); 
    if (len == n / 2) 
        return; 
// swap last with first upto n/2
    swap(s[len ], s[n - len - 1]); 
    reverse_str(s, len + 1); 
} 
  
void reverseString(vector<char>& s)
{
    reverse_str(s);
}

Maybe we'd move std::swap to the helper function and recurse the helper:也许我们会将std::swap移动到辅助函数并递归辅助函数:

// 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 <vector>
#include <algorithm>


static const struct Solution {
    using ValueType = std::int_fast16_t;
    static constexpr void reverseString(
        vector<char>& s
    ) {
        helper(s, 0, std::size(s) - 1);
    }

    static constexpr void helper(
        std::vector<char>& s,
        ValueType left,
        ValueType right
    ) {
        if (left >= right) {
            return;
        }

        std::swap(s[left], s[right]);
        ++left;
        --right;
        helper(s, left, right);
    }
};

We can also use unsigned int :我们也可以使用unsigned int

// 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 <vector>
#include <algorithm>


static const struct Solution {
    using ValueType = std::uint_fast16_t;
    static constexpr void reverseString(
        vector<char>& s
    ) {
        if (s.empty()) {
            return;
        }

        helper(s, 0, std::size(s) - 1);
    }

    static constexpr void helper(
        std::vector<char>& s,
        ValueType left,
        ValueType right
    ) {
        if (left >= right) {
            return;
        }

        std::swap(s[left], s[right]);
        ++left;
        --right;
        helper(s, left, right);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM