Count Odd Numbers in an Interval Range

ㅋㅋ·2023년 2월 13일
0

알고리즘-leetcode

목록 보기
110/135

정수 low와 high가 주어지는데 low 이상, high 이하의 홀수 개수를 구하는 문제

class Solution {
public:
    int countOdds(int low, int high) {
        
        bool lowIsOdd{low % 2 == 0 ? false : true};
        bool highIsOdd{high % 2 == 0 ? false : true};
        
        int min{(high - low) >> 1};

        return min + ((lowIsOdd || highIsOdd) ? 1 : 0);
    }
};

0개의 댓글