693. Binary Number with Alternating Bits - LeetCode

Question

693.?Binary Number with Alternating Bits

Solution

思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等,返回true,如果有相等的就返回false

Java实现:

public boolean hasAlternatingBits(int n) {
    char last = '2'; // 非0非1即可
    for (char c : Integer.toBinaryString(n).toCharArray()) { // int转二进制字符串
        if (c == last) return false;
        last = c;
    }
    return true;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9241159.html

时间: 2024-10-08 15:38:30

693. Binary Number with Alternating Bits - LeetCode的相关文章

Leetcode - 693. 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. 解题思路: 题设要求判断形如101010的数字,那么如何在复杂度最小的情况下给出算法呢 首先看一下用python解决本题有哪些基本工具. 说到二进制,首先想到的是位运算符号,python的位运算符号有& ^ ~ | >> &

693. 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: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex

191. Number of 1 Bits Leetcode Python

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

[LeetCode] Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-1)]的用处 题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For exam

[LeetCode][JavaScript]Number of 1 Bits

https://leetcode.com/problems/number-of-1-bits/ Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representat

[LeetCode] Number of 1 Bits 位1的个数

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

LeetCode OJ:Number of 1 Bits(比特1的位数)

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

[LeetCode]Number of 1 Bits

Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the fun