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 | 29 | 30 | 31 |
Tags
- base7
- 코딩게임
- leetcode
- DDD #도메인 #도메인 주도 설계 #도메인 주도 설계 핵심
- codinGame
- Find Pivot Index
- leetcode #20. Valid Parentheses #알고리즘 #leetcode Valid Parentheses
- #20. Valid Parentheses java
- DDD #도메인 #도메인 주도 설계 #도메인 주도 섥계 핵심
- leetcode #2206. Divide Array Into Equal Pairs
- 도메인 #도메인 주도 설계 #도메인 주도 설계 핵심 #DDD
- codingame #코딩게임 #codingame fall challenge2023 #코딩게임 2023 가을 챌린지
- aws #cloudwatch #log insight
- #move zeroes
- 867. Transpose Matrix #Transpose Matrix
- 도메인 주도 설계 핵심 #DDD #도메인 주도 설계 #도메인
- 도메인 주도 설계 핵심
- ddd
- Fall Challenge 2023
- leetcode #알고리즘 #릿코드
- 반 버논
- LeetCode #
Archives
- Today
- Total
서하아빠의 개발 블로그
693. Binary Number with Alternating Bits 본문
693. Binary Number with Alternating Bits
Easy
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example 1:
Input: n = 5
Output: true
Explanation: The binary representation of 5 is: 101
Example 2:
Input: n = 7
Output: false
Explanation: The binary representation of 7 is: 111.
Example 3:
Input: n = 11
Output: false
Explanation: The binary representation of 11 is: 1011.
Example 4:
Input: n = 10
Output: true
Explanation: The binary representation of 10 is: 1010.
Example 5:
Input: n = 3
Output: false
Constraints:
- 1 <= n <= 231 - 1
[문제풀이]
1) 주어진 값을 2진수로 변환한다.
2) 이때 주어진 값(n)을 1보다 작을 때까지 계속 2로 나누어서,
이전 나머지 값과 현재 나머지 값이 같으면 => false
이전 나머지 값과 현재 나머지 값이 다르면 => true
[소스코드]
class Solution {
public boolean hasAlternatingBits(int n) {
if( n <=1 ) return true;
int prev = n % 2;
n /= 2;
while( n >= 1 ) {
int cur = n % 2;
if( prev == cur) {
return false;
}
n /= 2;
prev = cur;
}
return true;
}
}
'알고리즘 > LeetCode' 카테고리의 다른 글
217. Contains Duplicate (0) | 2022.05.20 |
---|---|
25. Reverse Nodes in k-Group (0) | 2022.05.20 |
61. Rotate List (0) | 2022.05.19 |
200. Number of Islands (0) | 2021.06.14 |
283. Move Zeroes (0) | 2021.04.07 |