693.交替位二进制数

题目

693.交替位二进制数

题目大意

给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。

样例

示例 1:

输入:n = 5
输出:true
解释:5 的二进制表示是:101

示例 2:

输入:n = 7
输出:false
解释:7 的二进制表示是:111.

示例 3:

输入:n = 11
输出:false
解释:11 的二进制表示是:1011.

数据规模

提示:

  • 1 < = n < = 2 31 − 1 1 <= n <= 2^{31} - 1 1<=n<=2311

思路

直接考虑将n转换成二进制数,然后遍历所有的二进制元素,如果存在a[i]==a[i+1],说明该二进制不是 0 , 1 0,1 0,1交替,不符合条件;如果都是 a [ i ] ≠ a [ i + 1 ] a[i]\neq a[i+1] a[i]=a[i+1],则符合条件。

  • 时间复杂度: O ( log ⁡ n ) O(\log n) O(logn) n n n的二进制表示最多有 O ( log ⁡ n ) O(\log n) O(logn)位。
  • 空间复杂度: O ( 1 ) O(1) O(1)

代码

// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern 
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals 
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=1e5+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}

class Solution {
public:
    bool hasAlternatingBits(int n) {
		vector<int>a;
		while(n){
			a.push_back(n%2);
			n/=2;
		}
		for(int i=0;i<a.size()-1;i++){
			if(a[i]==a[i+1]){
				return 0;
			}
		}
		return 1;
    }
};

思路2

考虑位运算:对 n n n 的二进制右移一位后,得到的数字再与 n n n按位异或得到 a a a。如果 n n n 0 , 1 0,1 0,1交替的二进制数,那么异或结果一定是全 1 1 1;否则一定存在 0 0 0。然后将 a a a a + 1 a+1 a+1按位与,因为符合要求的 a a a一定是全 1 1 1,那么 a & ( a + 1 ) = 0 a \& (a+1)=0 a&(a+1)=0(当且仅当 a a a的二进制表示全为 1 1 1时, a + 1 a + 1 a+1可以进位,原来的数全部变成 0 0 0,按位与的结果为 0 0 0);否则,不会所有的位都产生进位,两个最高位都为 1 1 1,相与结果不为 0 0 0

// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern 
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals 
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=1e5+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}

class Solution {
public:
    bool hasAlternatingBits(int n) {
		ll a=n^(n>>1);
		if(a&(a+1))return 0;
		return 1;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Phoenix_ZengHao

创作不易,能否打赏一瓶饮料?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值