返回介绍

solution / 1500-1599 / 1523.Count Odd Numbers in an Interval Range / README_EN

发布于 2024-06-17 01:03:18 字数 2648 浏览 0 评论 0 收藏 0

1523. Count Odd Numbers in an Interval Range

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def countOdds(self, low: int, high: int) -> int:
    return ((high + 1) >> 1) - (low >> 1)
class Solution {
  public int countOdds(int low, int high) {
    return ((high + 1) >> 1) - (low >> 1);
  }
}
class Solution {
public:
  int countOdds(int low, int high) {
    return (high + 1 >> 1) - (low >> 1);
  }
};
func countOdds(low int, high int) int {
  return ((high + 1) >> 1) - (low >> 1)
}
function countOdds(low: number, high: number): number {
  return ((high + 1) >> 1) - (low >> 1);
}
impl Solution {
  pub fn count_odds(low: i32, high: i32) -> i32 {
    ((high + 1) >> 1) - (low >> 1)
  }
}
class Solution {
  /**
   * @param Integer $low
   * @param Integer $high
   * @return Integer
   */
  function countOdds($low, $high) {
    return ($high + 1 >> 1) - ($low >> 1);
  }
}
int countOdds(int low, int high) {
  return ((high + 1) >> 1) - (low >> 1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文