본문 바로가기
Algorithm/LeetCode

[LeetCode] 1523 | Count Odd Numbers in an Interval Range

by 밤초록 2022. 5. 19.
1523 | Count Odd Numbers in an Interval Range
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/

 

 

작성 코드

 

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

 

 

 

이상 코드

 

 

 

class Solution {
public:
    int countOdds(int low, int high) {
        return (high + 1) / 2 - low / 2;
    }
};
반응형

'Algorithm > LeetCode' 카테고리의 다른 글

[LeetCode] 226 | Invert Binary Tree  (0) 2022.05.30
[LeetCode] 191 | Number of 1 Bits  (0) 2022.05.24
[LeetCode]  (0) 2022.05.09
[LeetCode] 22 | Generate Parentheses  (0) 2022.05.03
[LeetCode] 53 | Maximum Subarray  (0) 2022.05.02

댓글