티스토리 뷰
1. 문제
입력 받은 문장의 각 단어들을 뒤집어주는 문제
입)
I am happy today
We want to win the first prize
출)
I ma yppah yadot
eW tnaw ot niw eht tsrif ezirp
2. 풀이
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (t-- > 0) {
String str = bf.readLine() + "\n";
Stack<Character> s = new Stack<>();
for (char ch : str.toCharArray()) {
if (ch == '\n' || ch == ' ') {
while (!s.isEmpty()) {
bw.write(s.pop());
}
bw.write(ch);
} else {
s.push(ch);
}
}
}
bw.flush();
}
}
문장을 입력받고, 문장의 끝을 표시하기 위해 '\n' 문자를 추가함.
스택에 문자를 push하다가, '\n' 문자나 ' '(공백) 문자를 만나면 모두 pop해준다.
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 블로킹
- 코딩테스트 고득점 Kit
- Synchronous
- 해시
- 스택/큐
- 동기
- a
- 논블로킹
- 비동기
- 핸들러 인터셉터
- Filter
- Handler Interceptor
- 프로그래머스
- http://www.nextree.co.kr/p6960/
- 프로그래머스 Level 2
- blocking
- 프로그래머스 Level 3
- 필터
- 프로그래머스 Level 1
- 인터셉터
- Asynchronous
- non-blocking
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함