[Programmers] #181848 - 문자열을 정수로 변환하기 [Java][C++][Python]
[Programmers] #181848 - 문자열을 정수로 변환하기 [Java][C++][Python]
1. 아이디어
숫자로만 이루어진 문자열 n_str을 정수로 변환하는 문제로 내장 라이브러리를 활용하면 간단하게 해결할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(1)$ |
($N$ = n_str의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public int solution(String n_str) {
return Integer.parseInt(n_str);
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
int solution(string n_str) {
return stoi(n_str);
}
1
2
def solution(n_str):
return int(n_str)
This post is licensed under CC BY 4.0 by the author.