• 문제 링크
Binary Number with Alternating Bits - LeetCode
Can you solve this real interview question? Binary Number with Alternating Bits - 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 Expla
leetcode.com
• 풀이 코드
public class Solution {
public boolean hasAlternatingBits(int n) {
n ^= (n >> 1);
return (n & (n + 1)) == 0;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 697. Degree of an Array - Java (0) | 2026.09.26 |
|---|---|
| [LeetCode] 696. Count Binary Substrings - Java (0) | 2026.09.25 |
| [LeetCode] 682. Baseball Game - Java (0) | 2026.09.23 |
| [LeetCode] 680. Valid Palindrome II - Java (0) | 2026.09.22 |
| [LeetCode] 674. Longest Continuous Increasing Subsequence - Java (0) | 2026.09.21 |
댓글