Leetcode 1523. Count Odd Numbers in an Interval Range

Problem

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

Algorithm

Count the number of odd integers between low and high. Find the nearest low_odd and high_odd, then calculate (high_odd - low_odd) // 2 + 1.

Code

python3 复制代码
class Solution:
    def countOdds(self, low: int, high: int) -> int:
        if low % 2 == 0:
            low += 1
        if high % 2 == 0:
            high -= 1
        
        return (high - low) // 2 + 1
相关推荐
你们补药再卷啦2 小时前
人工智能算法概览
人工智能·算法
cnxy1882 小时前
围棋对弈Python程序开发完整指南:步骤3 - 气(Liberties)的计算算法设计
python·算法·深度优先
AndrewHZ2 小时前
【图像处理基石】什么是光栅化?
图像处理·人工智能·算法·计算机视觉·3d·图形渲染·光栅化
小白菜又菜2 小时前
Leetcode 944. Delete Columns to Make Sorted
算法·leetcode
我找到地球的支点啦3 小时前
Matlab系列(006) 一利用matlab保存txt文件和读取txt文件
开发语言·算法·matlab
Dev7z3 小时前
基于Matlab实现GRACE卫星重力数据的全球水储量变化估算与分析
人工智能·算法·matlab
爱喝热水的呀哈喽4 小时前
11题目汇总
算法
三斗米4 小时前
Transformer入门:一文读懂《Attention Is All You Need》
算法·架构
Swift社区4 小时前
LeetCode 458 - 可怜的小猪
算法·leetcode·职场和发展