[Programmers] #120854 - 배열 원소의 길이 [Java][C++][Python]
문자열 배열의 각 원소 길이를 순서대로 담은 배열을 구하는 워밍업 문제.
[Programmers] #120854 - 배열 원소의 길이 [Java][C++][Python]
1. 아이디어
각 문자열의 길이를 구해 입력 순서대로 배열에 담아 반환한다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = strlist의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
class Solution {
public int[] solution(String[] strlist) {
int[] arr = new int[strlist.length];
for (int i = 0; i < strlist.length; i++) {
arr[i] = strlist[i].length();
}
return arr;
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<string> strlist) {
vector<int> v;
for (string& s : strlist) {
v.push_back(s.length());
}
return v;
}
1
2
def solution(strlist):
return [len(s) for s in strlist]
This post is licensed under CC BY 4.0 by the author.