将 int tmpCount = 0;这一行写到for循环外面就不能AC,why? ,比如这样
class Solution {
public int findNumbers(int[] nums) {
int count = 0,tmpCount = 0;
for(int i = 0;i<nums.length;i++){
while(nums[i]>0){
tmpCount = tmpCount+1;
nums[i] = nums[i]/10;
}
if(tmpCount%2==0){
count = count+1;
}
tmpCount = 0;
}
return count;
}
}
这是能AC的
class Solution {
public int findNumbers(int[] nums) {
int count = 0;
for(int i = 0;i<nums.length;i++){
int tmpCount = 0;
while(nums[i]>0){
tmpCount = tmpCount+1;
nums[i] = nums[i]/10;
}
if(tmpCount%2==0){
count = count+1;
}
}
return count;
}
}
看评论区有这样写的,觉得很牛,像脑筋急转弯一样
class Solution {
public int findNumbers(int[] nums) {
int count = 0;
for(int num:nums){
if((num>=10&&num<100)||(num>=1000&&num<10000)||(num==100000)){
count++;
}
}
return count;
}
}