附leetcode链接:https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
1295. Find Numbers with Even Number of Digits
Given an array nums of integers, return how many of them contain an even number of digits.
public int numbersWithEvenNumDig(int[] nums) {
int num = 0;
for(int i = 0;i<nums.length;i++) {
int n = 0;
while(nums[i]>0){
nums[i] /= 10;
n++;
}
if(n%2==0)
num++;
}
return num;
}
小结:对于数组,明确循环次数,用for;对于数字,有多少位是未知的,因此用while;判断数字的位数,常对10求商、求余;