1. 程式人生 > 其它 >#力扣 LeetCode1295. 統計位數為偶數的數字 @FDDLC

#力扣 LeetCode1295. 統計位數為偶數的數字 @FDDLC

技術標籤:演算法&資料結構

題目描述:

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

Java程式碼:

class Solution {
    public boolean judge(int n){ //1 <= nums[i] <= 10^5
        if(n<10)return false;
        else if(n<100)return true;
        else if(n<1000)return false;
        else if(n<10000)return true;
        else if(n<100000)return false;
        else return true;
    }
    public int findNumbers(int[] a) {
        int ans=0;
        for(int e:a)if(judge(e))ans++;
        return ans;
    }
}

優化:

class Solution {
    public int findNumbers(int[] a) {
        int ans=0;
        for(int e:a)if(e>=10&&e<=99||e>=1000&&e<=9999||e==100000)ans++;
        return ans;
    }
}