문자열의 단어 순서를 뒤집기(reverse) 2
문자열의 단어 순서를 뒤집기(reverse) 2
- Origin.] LeetCode – Reverse Words in a String II (Java)
https://www.programcreek.com/2014/05/leetcode-reverse-words-in-a-string-ii-java/
- 코드
- Origin.] LeetCode – Reverse Words in a String II (Java)
https://www.programcreek.com/2014/05/leetcode-reverse-words-in-a-string-ii-java/
- 코드
public static String reverseWords(char[] s) {
int i=0;
String ss;
for(int j=0; j<s.length; j++){
if(s[j]==' '){
reverse(s, i, j-1);
i=j+1;
}
}
ss = reverse(s, i, s.length-1);
ss = reverse(s, 0, s.length-1);
return ss;
}
public static String reverse(char[] s, int i, int j){
while(i<j){
char temp = s[i];
s[i]=s[j];
s[j]=temp;
i++;
j--;
}
return new String(s);
}