Love丶FFC's Blog

LintCode_easy:具有交替位的二进制数

2019-09-04 18:29:17
阅读:8269   •   评论:156
标签:,

描述

给一个正整数,检查它的二进制表示是否具有交替位:即,两个相邻的位总是具有不同的值。

样例

样例 1:

输入: 5
输出: True
解释:
5 的二进制表示为: 101

样例 2:

输入: 7
输出: False
解释:
7 的二进制表示为: 111.

样例 3:

输入: 11
输出: False
解释:
11 的二进制表示为: 1011.

样例 4:

输入: 10
输出: True
解释:
10 的二进制表示: 1010.

编程语言:Java

解题思想:

1.逐位比对即可。

时间复杂度:O(n)

IDE代码如下:

  1. package LintCode_Easy;
  2.  
  3. public class _987 {
  4.  
  5. public static void main(String[] args) {
  6. int n=5;
  7. String binary = Integer.toBinaryString(n);
  8. boolean judge = true;
  9. for(int i=0;i<binary.length()-1;i++)
  10. {
  11. if (binary.charAt(i)==binary.charAt(i+1))
  12. {
  13. judge = false;
  14. break;
  15. }
  16. }
  17. System.out.println(judge);
  18. }
  19.  
  20. }
  21.  

LintCode编辑器代码如下:

  1. public class Solution {
  2. public boolean hasAlternatingBits(int n) {
  3. String binary = Integer.toBinaryString(n);
  4. boolean judge = true;
  5. for(int i=0;i<binary.length()-1;i++)
  6. {
  7. if (binary.charAt(i)==binary.charAt(i+1))
  8. {
  9. judge = false;
  10. break;
  11. }
  12. }
  13. return judge;
  14. }
  15. }

评论板

共有 156 条评论

  1. Jorcift

    If the infant is able to suckle, encourage the mother to breastfeed more often levitra se vende sin receta

  2. intuido

    Not surprisingly, this very low soy food intake was not associated with urinary 2 16 values buy generic propecia ENBREL is a medicine that affects your immune

  3. intuido

    can viagra be taken daily My husband took furosemide for many years

  4. rYubBnHOD

    The raw CT slices were reconstructed using VGStudio 2 real cialis no generic Working at common primary schools, a specially trained team used theatre scenes to portray children experiencing epileptic seizures before an audience of schoolchildren, parents and teachers

  5. JackLit

    [url=http://acticin.skin/]acticin over the counter[/url]

  6. AlanLit

    [url=http://tenormina.online/]atenolol generic[/url]

  7. SueLit

    [url=http://accurane.online/]roaccutane without a prescription[/url]

  8. ZakLit

    [url=https://erectafil.foundation/]buy erectafil 20[/url]

  9. DarrylBox

    [url=https://phenergan.trade/]phenergan 25 mg otc[/url]

  10. CurtisHed

    [url=http://neurontin.foundation/]gabapentin 300mg[/url]

  11. AlanLit

    [url=http://tamoxifen.foundation/]buy nolvadex without prescription[/url]

  12. JosephKip

    [url=https://trimox.party/]amoxicillin 500mg no rx[/url]

  13. JackLit

    [url=http://propecia.africa/]finasteride tablets[/url]

  14. AlanLit

    [url=http://erectafil.gives/]erectafil 40[/url]

  15. JosephKip

    [url=https://stromectol.party/]price of ivermectin[/url]

  16. JoeLit

    [url=https://pharmacyonline.science/]canadian pharmacy mall[/url]

  17. SamLit

    [url=http://triamterene.charity/]triamterene/hctz 37.5[/url]

  18. Davidevelf

    [url=http://metformin.party/]metformin 300 mg daily[/url]

  19. DarrylBox

    [url=http://tretinon.online/]tretinoin 50 mg[/url]

  20. SamuelEcors

    [url=http://duloxetine.foundation/]cymbalta price 60 mg[/url]

  21. Davidevelf

    [url=https://azithromycin.solutions/]can i purchase azithromycin over the counter[/url]

  22. EyeLit

    [url=http://metformin.party/]metformin online purchase[/url]

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:502569