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 에서 가장 큰 숫자를 찾아 반환하면 되는데 이 숫자를 찾는데 흥미로운 일이 생겼다.

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[] 로 받아들여서 위와 같이 구했는데

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/11c339fc-6275-4ae3-a3d9-743e51c4753b/Screen_Shot_2021-01-07_at_23.51.42.png

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% 음.. 뭐... 그래 좋긴한데 더 빠른 방법이 없나??