【Leetcode】1523. Count Odd Numbers in an Interval Range
題目地址:
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/
給定一個整數閉區間 [ a , b ] [a,b] [a,b],問其中有多少個奇數。
程式碼如下:
public class Solution {
public int countOdds(int low, int high) {
high -= high % 2 == 0 ? 1 : 0;
low += low % 2 == 0 ? 1 : 0;
return (high - low >> 1) + 1;
}
}
時空複雜度 O ( 1 ) O(1) O(1)。
相關推薦
【Leetcode】1523. Count Odd Numbers in an Interval Range
技術標籤:# 二分、位運算與數學leetcode演算法 題目地址: https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/
1523. Count Odd Numbers in an Interval Range
Given two non-negative integerslowandhigh. Return thecount of odd numbers betweenlowandhigh(inclusive).
Count Odd Numbers in an Interval Range(C++在區間範圍內統計奇數數目)
技術標籤:C++LeetCodeleetcode演算法c++ 解題思路: (1)計算間距,同時考慮邊界
【LeetCode】315. Count of Smaller Numbers After Self 計算右側小於當前元素的個數(Medium)(JAVA)
技術標籤:Leetcode演算法leetcode資料結構java排序 【LeetCode】315. Count of Smaller Numbers After Self 計算右側小於當前元素的個數(Medium)(JAVA)
【LeetCode】357. Count Numbers with Unique Digits 計算各個位數不同的數字個數(Medium)(JAVA)
技術標籤:Leetcodeleetcodejava演算法字串動態規劃 【LeetCode】357. Count Numbers with Unique Digits 計算各個位數不同的數字個數(Medium)(JAVA)
【刷題-LeetCode】215. Kth Largest Element in an Array
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
【leetcode】1540. Can Convert String in K Moves
題目如下: Given two stringssandt, your goal is to convertsintotinkmoves or less. During theith(1 <= i <= k)move you can:
【leetcode】1560. Most Visited Sector in a Circular Track
題目如下: Given an integernand an integer arrayrounds.Wehave a circular track which consists ofnsectors labeled from1ton. A marathon will be held on this track, the marathon consists ofmrounds. Thei
【LeetCode】329. Longest Increasing Path in a Matrix 矩陣中的最長遞增路徑(Hard)(JAVA)
技術標籤:Leetcodeleetcode演算法java面試資料結構 【LeetCode】329. Longest Increasing Path in a Matrix 矩陣中的最長遞增路徑(Hard)(JAVA)
【LeetCode】327. Count of Range Sum 區間和的個數(Hard)(JAVA)
技術標籤:Leetcode演算法java資料結構leetcode快速排序 【LeetCode】327. Count of Range Sum 區間和的個數(Hard)(JAVA)
【LeetCode】387. First Unique Character in a String 字串中的第一個唯一字元(Easy)(JAVA)每日一題
技術標籤:LeetCode 每日一題字串leetcodejava資料結構演算法 【LeetCode】387. First Unique Character in a String 字串中的第一個唯一字元(Easy)(JAVA)
【刷題-LeetCode】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *version2* return 1; if *version1* < *version2* return -1;otherwise return 0.
【LeetCode】【Math】X of a Kind in a Deck of Cards
題目: 在一副紙牌中,每張紙牌上都有一個整數。 當且僅當您可以選擇X> = 2並可以將整個卡片組分成1組或多組卡片時,才返回true,其中:
【刷題-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: 10 Output: 4
【leetcode】1524. Number of Sub-arrays With Odd Sum
題目如下: Given an array of integersarr. Returnthe number of sub-arrayswithoddsum. As the answer may grow large, the answermust becomputed modulo10^9 + 7.
【leetcode】1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers
題目如下: Given two arrays of integersnums1andnums2, return the number of triplets formed (type 1 and type 2) under the following rules:
【Leetcode】1685. Sum of Absolute Differences in a Sorted Array
技術標籤:# 棧、佇列、串及其他資料結構leetcode演算法 題目地址: https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/
【LeetCode】328. Odd Even Linked List 奇偶連結串列(Medium)(JAVA)
技術標籤:Leetcode演算法連結串列leetcodejava資料結構 【LeetCode】328. Odd Even Linked List 奇偶連結串列(Medium)(JAVA)
【Leetcode】762. Prime Number of Set Bits in Binary Representation
技術標籤:# 二分、位運算與數學java演算法leetcode 題目地址: https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
【LeetCode】386. Lexicographical Numbers 字典序排數(Medium)(JAVA)
技術標籤:Leetcode演算法leetcodejava資料結構面試 【LeetCode】386. Lexicographical Numbers 字典序排數(Medium)(JAVA)