파비의 매일매일 공부기록

2023.02.13 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.13 Today's Challenge

fabichoi 2023. 2. 13. 23:50

https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/

 

Count Odd Numbers in an Interval Range - LeetCode

Can you solve this real interview question? Count Odd Numbers in an Interval Range - 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 Explanati

leetcode.com

어거지로 풀긴 했는데, 좀 더 엘레강스한 소스 구성이었으면 좋았을 것 같다.

class Solution:
    def countOdds(self, low: int, high: int) -> int:
        flag = 0
        if low % 2 == 0:
            low -= 1
        else:
            flag = 1
        if high % 2 == 0:
            high -= 1
        return ((high+1)//2 - (low+1)//2) + flag
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.15 Today's Challenge  (0) 2023.02.15
2023.02.14 Today's Challenge  (0) 2023.02.14
2023.02.12 Today's Challenge  (0) 2023.02.12
2023.02.11 Today's Challenge  (0) 2023.02.11
2023.02.10 Today's Challenge  (0) 2023.02.10
Comments