Skip to content

Latest commit

 

History

History
82 lines (65 loc) · 1.42 KB

reverse-string.md

File metadata and controls

82 lines (65 loc) · 1.42 KB
title Reverse String

描述

Write a function to reverse a string. The input string is given as an array of characters.

You must do this by modifying the input array in-place with O(1) extra memory.

分析

无。

代码

import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";

<Tabs defaultValue="python" values={[ { label: 'Python', value: 'python', }, { label: 'Java', value: 'java', }, { label: 'C++', value: 'cpp', }, ] }>

class Solution {
    public void reverseString(char[] s) {
        int left = 0, right = s.length - 1;
        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }
}
// Reverse String
// 时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
    void reverseString(vector<char>& s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            swap(s[left], s[right]);
            left++;
            right--;
        }
    }
};
def reverse_string(s):
    left, right = 0, len(s) - 1
    while left < right:
        s[left], s[right] = s[right], s[left]
        left += 1
        right -= 1

相关题目