Endless statistical algorithm digit even number of digital

To give you an array of integers nums, you return to where the median of an even number of numbers.

Example 1:

Input: nums = [12,345,2,6,7896]
Output: 2

Explanation:

12 is a 2-digit number (the number of bits is an even number)
345 3 digits (digits is odd)
2 is a 1-bit digital (odd digits)
6 is an odd number of digits)
7896 is 4 digits (bits the number is an even number)
so only 12, and 7896 is the number of bits is an even number

Example 2:

Input: nums = [555,901,482,1771]
Output: 1

Explanation:

Only 1771 was a median of numbers even.

Problem solving:

Using the string:

public int findNumbers(int[] nums) {
		int count = 0;
		for (int num : nums)
			count += String.valueOf(num).length() % 2 == 0 ? 1 : 0;
		return count;
}

Mathematics / 10

public int findNumbers(int[] nums) {
		int count = 0;
		for (int num : nums) {
			int n = 0;
			while (num > 0) {
				num /= 10;
				n++;
			}
			count += n % 2 == 0 ? 1 : 0;
		}
		return count;
}
Published 125 original articles · won praise 236 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_33709508/article/details/103798314