美文网首页Leetcode
Leetcode.344.Reverse String

Leetcode.344.Reverse String

作者: Jimmy木 | 来源:发表于2019-12-23 09:43 被阅读0次

    题目

    给定一个字符数组,对数组进行倒序。只是用O(1)的额外内存空间。

    Input: ["h","e","l","l","o"]
    Output: ["o","l","l","e","h"]
    Input: ["H","a","n","n","a","h"]
    Output: ["h","a","n","n","a","H"]
    

    思路

    直接倒序。

    void reverseString(vector<char>& s) {
        for (int i = 0; i < s.size() / 2; i++) {
            char temp = s[s.size()-1-i];
            s[s.size()-1-i] = s[i];
            s[i] = temp;
        }
    }
    

    总结

    这个题是乱入的吧。

    相关文章

      网友评论

        本文标题:Leetcode.344.Reverse String

        本文链接:https://www.haomeiwen.com/subject/npxznctx.html