LeetCode1523. Count Odd Numbers in an Interval Range

2024-01-01 18:20

本文主要是介绍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;}
};

这篇关于LeetCode1523. Count Odd Numbers in an Interval Range的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/560119

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetcode#38. Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 12. 113. 214. 12115. 111221 1 is read off as “one 1” or 11. 11 is read off

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

56. Merge Interval

题目: 解答: 常规的合并,根据前后interval是否有交集判定。 代码: /*** Definition for an interval.* struct Interval {* int start;* int end;* Interval() : start(0), end(0) {}* Interval(int s, int e) : start

MySql 1264 - Out of range value for column 异常

前段时间操作数据库,本是一个很简单的修改语句,却报了  1264 - Out of range value for column字段类型官网  当时一看懵逼了,网上很多都说是配置的问题,需要修改my.ini文件,这个方式我没有试过,我想肯定还有其它方法,经过慢慢排 查发现表里的字段为 decimal(10,3) ,这说明小数点前只有7位,保留了3位小数点,而值在小数点前却有8位,这就导致了错误

ON_COMMAND_RANGE 和 ON_UPDATE_COMMAND_UI_RANGE

 ON_COMMAND_RANGE 和 ON_UPDATE_COMMAND_UI_RANGE 可以影射ID连续的Toolbar/Menu ID。 ON_COMMAND_RANGE影射的消息响应函数需要一个参数UINT表明是哪一个消息, afx_msg void OnZoom(UINT nID); 而ON_UPDATE_COMMAND_UI_RANGE的消息响应函数则无此ID,与ON

on command range

 ON_COMMAND_RANGEON_COMMAND_RANGE( id1, id2, memberFxn )参数: id1一个连续范围的命令ID的起始值。id2一个连续范围的命令ID的结束值。memberFxn该命令被映射到的消息处理函数的名字。 说明:使用这个宏把一个连续范围的命令ID映射到单个命令处理函数。ID的范围从id1开始,到id2结束。用ON_COMMAND_RAN

ON_COMMAND_RANGE的用法

 今天主要介绍一下ON_COMMAND_RANGE的用法 第一次用这个方法还是刚毕业那会,那时写过一个控制程序,界面上有很多电器的控制按钮,这些按钮的响应函数基本一致,只是相应的ID值不一样,要是一一写响应函数那不累死人,于是就东找西找,找到ON_COMMAND_RANGE。 最近一个偶然机会也要用到它,三下五除二,CODE写完了, 1.在要添加的工程上添加函数afx_msg vo