[코테 풀이] Count Odd Numbers in an Interval Range

시내·2024년 6월 8일

Q_1523) Count Odd Numbers in an Interval Range

출처 : https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/?envType=study-plan-v2&envId=programming-skills

Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).

class Solution {
    public int countOdds(int low, int high) {
       int interval = high - low - 1;
        int res = 0;
        if (interval == -1) {
            if (low % 2 == 1) return 1;
            else return 0;
        } else if (interval == 0) {
            if (low % 2 == 1) res++;
            if (high % 2 == 1) res++;
        } else if (low % 2 != 0 && high % 2 != 0) {
            if (interval % 2 != 0) res = interval / 2 + 2;
        } else if (low % 2 == 0 && high % 2 == 0) {
            res = interval / 2 + 1;
        } else {
            if (low % 2 == 1 || high % 2 == 1) {
                res = interval / 2 + 1;
            }
        }
        return res;
}
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글