Post

[Programmers] #181941 - 문자 리스트를 문자열로 변환하기 [Java][C++][Python]

[Programmers] #181941 - 문자 리스트를 문자열로 변환하기 [Java][C++][Python]

문제 링크


1. 아이디어

배열 arr에 담긴 문자들을 순서대로 이어 붙인 문자열을 반환하면 되는 간단한 문제다.


2. 복잡도

접근시간공간
풀이$O(N)$$O(N)$

($N$ = arr의 길이)


3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
class Solution {
    public String solution(String[] arr) {
        return String.join("", arr);
    }
}

String의 join으로 한번에 이어붙여서 반환했다.

1
2
3
4
5
6
7
8
#include <bits/stdc++.h>
using namespace std;

string solution(vector<string> arr) {
    string ans;
    for (string& s : arr) ans += s;
    return ans;
}

C++는 std::string이 가변이므로 누적해서 더해준 후 반환했다.

1
2
def solution(arr):
    return "".join(arr)

join으로 한번에 이어붙여서 반환했다.


This post is licensed under CC BY 4.0 by the author.