cleanUrl: /posts/convert-string-number-to-char-array-leetcode-1689
문제를 푸는것 보다 문자열 → char[] → int 로 변경하는 코드를 보고 싶으신 분들은 여기를 눌러 바로 이동하세요
Leetcode 의 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers 문제를 풀면서 이건 좀 기록해둘 필요가 있다 생각했다.
Partitioning Into Minimum Number Of Deci-Binary Numbers - LeetCode
A decimal number is called deci-binary if each of its digits is either 0
or 1
without any leading zeros. For example, 101
and 1100
are deci-binary, while 112
and 3001
are not.
Given a string n
that represents a positive decimal integer, return the minimum number of positive deci-binary numbers needed so that they sum up to n
.
123454321
과 같이 숫자 문자열이 발견되면 여기서 가장 큰 숫자 만큼 deci-binary 가 발생된다.
n="8"
→ 1 이 8개 필요하다.n="38"
→ (33): 11, 11, 11 + (1): 1 + 1 + 1 + 1+ 1 → 11은 3개 + 1은 5개 → 8n="438"
→ 111, 111, 111, 101, 1, 1, 1, 1 → 8개결국 문자열 n 에서 가장 큰 숫자를 찾아 반환하면 되는데 이 숫자를 찾는데 흥미로운 일이 생겼다.
int max = 0;
for (int i = 0; i < n.length(); i++) {
int num = Character.getNumericValue(n.charAt(i));
max = Math.max(max, num);
}
n 을 char[] 로 받아들여서 위와 같이 구했는데
int max = 0;
for (int i = 0; i < n.length(); i++) {
int num = Character.getNumericValue(
n.charAt(i));
max = Math.max(max, num);
}
return max;
runtime 21ms, 13.93% 음.. 뭐... 그래 좋긴한데 더 빠른 방법이 없나??