Open yokostan opened 5 years ago
class Solution { public void reverseWords(char[] str) { if (str == null || str.length == 0) return ; swap(str, 0, str.length - 1); int start = 0; while (start < str.length) { int end = start; while (end < str.length && str[end] != ' ') { end++; } swap(str, start, end - 1); start = end + 1; } } private void swap(char[] str, int i, int j) { if (i == j) return ; while (i < j && j < str.length) { char tmp = str[i]; str[i] = str[j]; str[j] = tmp; i++; j--; } } }