1. 程式人生 > 其它 >Count Odd Numbers in an Interval Range(C++在區間範圍內統計奇數數目)

Count Odd Numbers in an Interval Range(C++在區間範圍內統計奇數數目)

技術標籤:C++LeetCodeleetcode演算法c++

解題思路:

(1)計算間距,同時考慮邊界

class Solution {
public:
    int countOdds(int low, int high) {
        int count = (high-low)/2;
        if(low%2==0 && high%2==0) return count;
        if(low%2!=0 || high%2!=0) return count+1;
        return 0;
    }
};