[Programmers] #181945 - 문자열 돌리기 [Java][C++][Python]
입력 문자열을 시계 방향으로 90도 돌려(세로로) 출력하는 워밍업 문제.
[Programmers] #181945 - 문자열 돌리기 [Java][C++][Python]
1. 아이디어
문자열을 시계방향으로 90도 돌리면 각 문자가 위에서 아래로 한 줄씩 나열된 모양이 된다는 점에 착안해, 입력 문자열의 각 문자를 순서대로 한 줄씩 출력하면 되는 문제다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = 입력 문자열의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (char c : br.readLine().toCharArray()) {
System.out.println(c);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
for (char c : s) cout << c << '\n';
}
1
2
3
4
5
import sys
input = sys.stdin.readline
print(*input(), sep="\n")
Python은 문자열을 *로 언패킹해 각 문자를 개별 인자로 만든 뒤 sep="\n"으로 한 줄씩 출력했다.
This post is licensed under CC BY 4.0 by the author.