
# 문제 출제 사이트
https://programmers.co.kr/learn/courses/30/lessons/12925
코딩테스트 연습 - 문자열을 정수로 바꾸기
문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요. 제한 조건 s의 길이는 1 이상 5이하입니다. s의 맨앞에는 부호(+, -)가 올 수 있습니다. s는 부호와 숫자로만 이루어져있습니
programmers.co.kr
# 문제
문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요.
# 제한 조건
- s의 길이는 1 이상 5이하입니다.
- s의 맨앞에는 부호(+, -)가 올 수 있습니다.
- s는 부호와 숫자로만 이루어져있습니다.
- s는 "0"으로 시작하지 않습니다.
# 입출력 예
예를들어 str이 "1234"이면 1234를 반환하고, "-1234"이면 -1234를 반환하면 됩니다.
str은 부호(+,-)와 숫자로만 구성되어 있고, 잘못된 값이 입력되는 경우는 없습니다.
# 제출한 소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int solution(String s) { | |
int answer = 0; | |
answer = Integer.parseInt(s); | |
return answer; | |
} | |
} |
- Integer.parseInt 를 활용해서 입력된 String s 의 값을 인트 형식으로 형반환해 리턴하는 함수입니다.
Integer.parseInt란? => https://jamesdreaming.tistory.com/125 - 자바에서 형변환이란? => https://coding-factory.tistory.com/130
반응형