LeetCode: Reverse a String in java with O(1) space
public class ReverseString {
public static void reverseString(String str) {
char[] s =str.toCharArray();
int left = 0, right = s.length - 1;
while (left < right) {
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
System.out.println(String.valueOf(s));
}
public static void main(String[] args) {
reverseString("ramu is a good boy");
}
}
Comments
Post a Comment