WildPointer

Welcome to my world!

LeetCode 344: Reverse String


Difficulty: Easy

Tags: Two Pointers, String


问题描述

反转字符串。


解决方案

直接反转。


▷ Time Complexity: O(n)

▷ Space Complexity: O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
string reverseString(string s) {
int length = s.size();
int iter = length/2;

for(int i=0; i<iter; i++){
char t = s[i];
s[i] = s[length-1-i];
s[length-1-i] = t;
}
return s;
}
};

回到 Conquer Leetcode

0%