알고리즘
[leetcode] Find Numbers with Even Number of Digits
개발정리
2021. 11. 6. 14:24
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
[Success]
정수 배열이 주어졌을 때, 자릿수가 짝수인 값은 몇개인지 반환하는 문제
[풀이]
Math.log10 라는 함수를 이용하여 정수 값의 자릿수를 알아 낸 후 자릿수가 짝수인지 확인하여 짝수라면 카운트 값을 증가시킨 후 반환한다.
class Solution {
public static int findNumbers(int[] nums) {
if (nums == null) return -1;
int evenCount = 0;
for(int num : nums) {
int length = (int)Math.log10(num)+1;
if (length % 2 == 0) {
evenCount++;
}
}
return evenCount;
}
}