[Programmers] #12925 - 문자열을 정수로 바꾸기 [Java][C++][Python]
[Programmers] #12925 - 문자열을 정수로 바꾸기 [Java][C++][Python]
1. 아이디어
맨 앞에 부호가 올 수 있는 숫자 문자열을 정수로 바꾸는 문제다. 정수 변환 함수가 맨 앞의 +와 -를 부호로 받아 주므로 부호를 따로 떼어 처리하지 않고 문자열 전체를 그대로 변환하면 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public int solution(String s) {
return Integer.parseInt(s);
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
int solution(string s) {
return stoi(s);
}
1
2
def solution(s):
return int(s)
This post is licensed under CC BY 4.0 by the author.