관리 메뉴

솜씨좋은장씨

[leetCode] 1523. Count Odd Numbers in an Interval Range (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 1523. Count Odd Numbers in an Interval Range (Python)

솜씨좋은장씨 2020. 12. 28. 00:13
728x90
반응형

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

 

Example 1:

Input: low = 3, high = 7
Output: 3
Explanation: The odd numbers between 3 and 7 are [3,5,7].

Example 2:

Input: low = 8, high = 10
Output: 1
Explanation: The odd numbers between 8 and 10 are [9].

 

 

Constraints:

  • 0 <= low <= high <= 10^9

Solution

class Solution:
    def countOdds(self, low: int, high: int) -> int:
        answer = 0
        
        if high-low == 1:
            answer = 1
        elif low % 2 == 1 and high % 2 == 1:
            answer = (high - low + 1) // 2 + 1
        elif low % 2 == 0 and high % 2 == 0:
            answer = (high - low + 1) // 2
        elif (low % 2 == 0 and high % 2 == 1) or (low % 2 == 1 and high % 2 == 0):
            answer = (high - low + 1) // 2
            
        return answer

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments