https://school.programmers.co.kr/learn/courses/30/lessons/181914
자바
class Solution {
public int solution(String number) {
int sum = 0;
for (char ch: number.toCharArray()) {
sum += Character.getNumericValue(ch);
}
return sum % 9;
}
}
자바에서는 Character 패키지의 스태틱 메서드인 getNumericValue()를 사용해서 풀이하는 방법을 사용했습니다.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.
Parameters:codePoint - the character (Unicode code point) to be converted.
Returns:the numeric value of the character, as a nonnegative int value; -2 if the character has a numeric value but the value can not be represented as a nonnegative int value; -1 if the character has no numeric value.Since:1.5
자바스크립트
'Portpolio > codingtest' 카테고리의 다른 글
프로그래머스 삼각형의 완성조건(1) 풀이 (1) | 2025.02.03 |
---|---|
프로그래머스 마지막 두 원소 구하기 (0) | 2025.02.03 |
프로그래머스 옷가게 할인 받기 풀이 (0) | 2025.02.03 |
프로그래머스 세균 증식 풀이 (0) | 2025.02.02 |
프로그래머스 몫 구하기 (0) | 2025.02.02 |
댓글