Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
class Solution {
func reverseString(_ s: inout [Character]) {
if s.isEmpty {
return
}
var i = 0, j = s.count - 1
while i < j {
s.swapAt(i, j)
i += 1
j -= 1
}
}
}
public class Solution {
public String reverseString(String s) {
if (s == null || s.length() == 0) {
return "";
}
int i = 0, j = s.length() - 1;
char[] chars = s.toCharArray();
while (i < j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
return new String(chars);
}
}
Hope this helps,
Michael