当前位置: 首页 > news >正文

LeetCode1523. 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
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

二、题解

首先考虑两侧均是偶数的情况:奇数数量为(high - low) / 2
若左侧是奇数,则数量加一,并右移,使其变为偶数。
若右侧是奇数,则数量减一,并左移,使其变为偶数。
最终,如果左侧或右侧是奇数,可以转换为左右侧均为偶数的情况计算。

class Solution {
public:int countOdds(int low, int high) {int res = 0;if(low % 2 != 0){res++;low++;}if(high % 2 != 0){res++;high--;}res += (high - low) / 2;return res;}
};
http://www.lryc.cn/news/273704.html

相关文章:

  • E中国铜金属行业需求前景及未来发展机遇分析报告2024-2030年
  • python SVM 保存和加载模型参数
  • JAVA进化史: JDK12特性及说明
  • Databend 的算力可扩展性
  • 「解析」Windows 如何优雅使用 Terminal
  • Linux第18步_安装“Ubuntu系统下的C语言编译器GCC”
  • 【Linux】Linux 基础命令 crontab命令
  • 14:00面试,14:08就出来了,问的问题过于变态了。。。
  • Ubuntu envs setting
  • Windows 下用 C++ 调用 Python
  • 九州金榜|家庭教育一招孩子不在任性
  • 爬虫案列 --抖音视频批量爬取
  • 【React系列】React中的CSS
  • 基于Kettle开发的web版数据集成开源工具(data-integration)-应用篇
  • 51单片机三种编译模式的相互关系
  • java 千帆大模型 流式返回
  • 全新互联网洗衣洗鞋小程序平台新模式
  • js 对于一些脚本中对于url的一些参数获取
  • IEDA中tomcat日志乱码解决
  • 计算机网络实验(六):三层交换机实现VLAN间路由
  • Flutter中showModalBottomSheet的属性介绍和使用
  • 机器学习 -- k近邻算法
  • 安全测试之SSRF请求伪造
  • php composer安装
  • 【MyBatis】MyBatis基础操作
  • Automatic merge failed; fix conflicts and then commit the result.如何处理
  • 一文读懂 $mash 通证 “Fair Launch” 规则(幸运池玩法解读篇)
  • Qt3D QGeometryRenderer几何体渲染类使用说明
  • pandasDataFrame读和写csv文件
  • 力扣122. 买卖股票的最佳时机 II