统计位数为偶数的数字

此博客链接:https://www.cnblogs.com/ping2yingshi/p/13021775.html

统计 位数为偶数的数字(22min)

题目链接:https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits/

给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。

示例 1:

输入:nums = [12,345,2,6,7896]
输出:2
解释:
12 是 2 位数字(位数为偶数) 
345 是 3 位数字(位数为奇数)  
2 是 1 位数字(位数为奇数) 
6 是 1 位数字 位数为奇数) 
7896 是 4 位数字(位数为偶数)  
因此只有 12 和 7896 是位数为偶数的数字
示例 2:

输入:nums = [555,901,482,1771]
输出:1
解释:
只有 1771 是位数为偶数的数字。

题解:

       思路:

                1.取出数组中每个数字。

                 2.把每个数字转换成字符串求长度。

                3.判断长度师傅为偶数。

  代码如下:

class Solution {
    public int findNumbers(int[] nums) {
         int len=nums.length;
         int temp=0;
         int count=0;
         for(int i=0;i<len;i++){
            temp=String.valueOf(nums[i]).length();
           if(temp%2==0)
            {
                count++;
            }
         }
         return count;
    }
}
原文地址:https://www.cnblogs.com/ping2yingshi/p/13021775.html