Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 파이썬
- 리트코드 1557
- 백준 16935
- 자바 리트코드
- 프로그래머스
- 카카오
- 리트코드
- 자바
- 스프링 에러
- java leetcode
- 그래프 자바
- 스택
- dfs
- DP
- BFS
- 프로그래머스 java
- 백준 18222
- 코딩테스트
- 자바 5464
- leetcode 1721
- 코테
- 구현
- Java
- 인텔리제이 에러
- daily challenge
- 백준
- 분할정복
- 리트코드 자바
- java 프로그래머스
- leetcode
Archives
- Today
- Total
레벨업 일지
[Java] leetcode 1523. Count Odd Numbers in an Interval Range 본문
알고리즘/leetcode
[Java] leetcode 1523. Count Odd Numbers in an Interval Range
24시간이모자란 2023. 2. 13. 23:04문제
https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/description/
Count Odd Numbers in an Interval Range - LeetCode
Can you solve this real interview question? 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 Explanati
leetcode.com
풀이
쉬어가는 문제.
로직은 다음과 같다.
- [ 0 .. low ] 0이상 low 이하 의 홀수 개수 를 구한다.
- [ 0 .. high ] 0 이상 high 이하의 홀수 개수를 구한다.
- 두개의 차를 해주고, low 가 홀수이면 1을 더해준다.
코드
class Solution {
public int countOdds(int low, int high) {
return (high+1) /2 - (low+1) /2 +low%2;
}
}
'알고리즘 > leetcode' 카테고리의 다른 글
[Java] leetcode 37. Sudoku Solver (2) | 2023.02.20 |
---|---|
[Java] leetcode 103. Binary Tree Zigzag Level Order Traversal (0) | 2023.02.19 |
[Java] leetcode 1626. Best Team With No Conflicts (0) | 2023.02.12 |
[Java] leetcode 1626. Best Team With No Conflicts (0) | 2023.02.01 |
[Java/Python] leetcode 1137. N-th Tribonacci Number (0) | 2023.01.31 |
Comments