Functional programming:
using static System.Math;
public class Solution {
public int FindNumbers(int[] nums) =>
nums.Count(num => CountDigit(num) % 2 == 0);
public int CountDigit(int n) => (int)Max(Log10(n), 0) + 1;
}
Imperative programming:
using static System.Math;
public class Solution {
public int FindNumbers(int[] nums) {
int evenCount = 0;
foreach (var num in nums) {
if (CountDigit(num) % 2 == 0) {
++evenCount;
}
}
return evenCount;
int CountDigit(int num) {
int digit = 0;
for (; num != 0; num /= 10) {
++digit;
}
return Max(digit, 1);
}
}
}
Source:
https://leetcode.com/problems/find-numbers-with-even-number-of-digits/submissions/
No comments:
Post a Comment