【Leetcode_easy】693. Binary Number with Alternating Bits
problem
693. Binary Number with Alternating Bits
solution1:
class Solution {
public:
bool hasAlternatingBits(int n) {
int bit = -;
while(n>)
{
/*
errr...
if(n&1 && bit==1) return false;
else if(n&1) bit = 1;
if(n&1==0 && bit==0) return false;
else if(n&1==0) bit = 0;
*/
if(n&==)
{
if(bit==) return false;
bit = ;
}
else
{
if(bit==) return false;
bit = ;
}
n >>= ;//err.
}
return true;
}
};
solution2:
通过异或操作判断最低位是否在0/1之间转换进行。
class Solution {
public:
bool hasAlternatingBits(int n) {
int d = (n&);
while((n&)==d)
{
d ^= ;//
n >>= ;
}
return n==;
}
};
参考
1. Leetcode_easy_693. Binary Number with Alternating Bits;
2. Grandyang;
完
【Leetcode_easy】693. Binary Number with Alternating Bits的更多相关文章
- 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...
- 693. Binary Number with Alternating Bits - LeetCode
Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation
problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...
- [LeetCode&Python] Problem 693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- LeetCode 693 Binary Number with Alternating Bits 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- 693. Binary Number with Alternating Bits
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [LeetCode] 693. Binary Number with Alternating Bits_Easy
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
随机推荐
- git生成ssh公私钥
ssh-keygen -t rsa -C "youremail@example.com" 生成好的密钥文件在%userprofile%/.ssh/目录,.pub文件为公钥,然后添加 ...
- Java注解合并,注解继承
莆田SEO:spring中有时候一个类上面标记很多注解. 实际上Java注解可以进行继承(也就是把多个注解合并成1个) 比如说SpringMVC的注解 @RestController @Request ...
- Web前端 --- 前端基础简介
目录 web端 HTTP协议 web端 1.前端,后端 什么是前端 任何与用户直接打交道的操作界面,都可以称之为前端, eg:电脑界面 手机界面 什么是后端 真正的幕后操作者 2.前端学习的历程 HT ...
- Spring核心概念和案例
一.Spring概念 1.Spring框架概述 轻量级的Java EE开源框架,它是由Rod Johnson为了解决企业应用程序开发的复杂性而创建, Spring框架提供了一个开发平台,用于整合其他技 ...
- Appium自动化测试教程-自学网-monkey日志管理
日志管理作用 Monkey日志管理是Monkey测试中非常重要的一个环节,通过日志管理分析,可以获取当前测试对象在测试过程中是否会发生异常,以及发生的概率,同时还可以获取对应的错误信息,帮助开发定位和 ...
- HR#7 题解
T1 签到题 #include<bits/stdc++.h> #define R register int using namespace std; inline int g() { R ...
- 四十二.部署MongoDB服务 、 MongoDB基本使用
1. 部署MongoDB服务 192.168.4.50 创建服务工作目录 ]# mkdir /usr/local/mongodb ]# cd /usr/local/mongodb/ ]# mkdir ...
- scheduled定时任务+实例请求数据库
1.scheduled定时任务类:ScheduledDemo.java package com.nantian.scheduled; import java.util.Date; import org ...
- Centos7的rabbitmq镜像集群
1.下载RabbitMQ vim /etc/hosts10.10.21.197 rabbit110.10.21.198 rabbit2 #分别命名hostname rabbit1hostname ra ...
- python快捷键的使用【摘抄】
接触python有些快捷键还不熟悉,搜索到下面这个文章很好的转发和摘抄了,感谢作者的用心分析 摘抄来源:https://www.cnblogs.com/haiyan123/p/7170593.html ...