1295. Statistical even number of digits to digital

1295. Statistical even number of digits to digital

Subject description:

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 2 digits (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 4 digits (number of bits is an even number)
so that only the 7896 is 12 bits and the even-numbered digital
example 2:

Input: nums = [555,901,482,1771]
Output: 1
Explanation:
Only median number 1771 is an even number.

prompt:

1 <= nums.length <= 500
1 <= nums[i] <= 10^5

Problem solution 1:

Title numbers are given size range 1 to 100,000, where the number of bits is an even number are:
* When one hundred thousand is not 0, a count is incremented; (100000)
· When ten thousand and one hundred thousand to 0, one thousand is not 0 when the count plus one; (001000)
when the current face are all 0 · ten bits are not 0, a count is incremented. (000010)

public class Solution2 {
    public static int findNumbers(int[] nums) {
//统计位数为偶数的数字个数
         int count = 0;
//该循环用于判断数字位数是否为偶数        
         for(int i = 0;i<nums.length;i++){        
            int n = nums[i];
            int shiwan = n/100000;
            int wan = n/10000;
            int qian = n/1000;
            int bai = n/100;
            int shi = n/10;
            
            if(shiwan!=0){
                count++;
            }else if(wan!=0){
            }else if(qian!=0){
                count++;
            }else if(bai!=0){
            }else if(shi!=0){
                count++;
            }       
        }
 //返回个数
        return count;
    }
Submit Result 1:

Here Insert Picture Description

Problem solution 2:

Converts the number into a string type, length is determined so that the code is simpler

class Solution {
    public int findNumbers(int[] nums) {
//统计位数为偶数的数字个数    
    int count = 0;
	for (int num : nums)
//判断转换成的字符串长度是否为偶数,是计数就加1
		count += String.valueOf(num).length() % 2 == 0 ? 1 : 0;
	return count;
    }
}
2 to submit the results:

Here Insert Picture Description

Published 21 original articles · won praise 19 · views 595

Guess you like

Origin blog.csdn.net/weixin_44458246/article/details/104187170