MrainW's Home

All things come to those who wait!

0%

LeetCode 344. Reverse String

Question

Write a function that reverses a string. The input string is given as an array of characters s.

https://leetcode.com/problems/reverse-string/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public void reverseString(char[] s) {
int i = 0, j = s.length - 1;
while (i < j){
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
return;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Welcome to my other publishing channels