作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/power-of-two/

Total Accepted: 71172 Total Submissions: 194399 Difficulty: Easy

题目描述

Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1
Output: true
Explanation: 20 = 1

Example 2:

Input: 16
Output: true
Explanation: 24 = 16

Example 3:

Input: 218
Output: false

题目大意

判断一个整数是不是2的幂。

解题方法

和刚才那个是否为3的倍数好像。嗯。刚才有个字符串的方法没讲。这里试了一下。

二进制

这个方法应该是通用的方法,转换为特定的进制的字符串,然后判断是否为1开头的字符。

The code above converts number into base base and returns the result as a String. For example, Integer.toString(5, 2) == “101” and Integer.toString(5, 3) == “12”.

boolean matches = myString.matches("123");

public class Solution {
public boolean isPowerOfThree(int n) {
return Integer.toString(n, 2).matches("^10*$");
}
}

AC:20ms

如果用Python写的话,可以直接数一下二进制中1的个数是不是正好是1个.

class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
return n > 0 and bin(n).count("1") == 1

位运算

还记得那个 n&(n-1) 的方法来数一个数里边有多少个1吗?没错,只要只有一个1就好。

public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0) return false;
return (n & (n-1)) == 0;
}
}

AC:2ms

python解法如下:

class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0: return False
return n & (n - 1) == 0

判断是不是最大2的幂的因数

嗯。参考了3的幂的另一种解法,用满足条件的最大的int来看这个n能否整除。本来想手算2的最大的幂的int是多少呢,后来一想可以用位移运算。太聪明了。恩。

public class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0) return false;
return (1<<31) % n == 0;
}
}

AC:2ms

判断因子是不是只有2

不停地循环。判断因子是不是只有2,最后的话结果应该是1.

class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0: return False
while n % 2 == 0:
n /= 2
return n == 1

日期

2016/5/1 17:10:28
2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】231. Power of Two 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  2. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  3. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  4. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  5. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  6. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  8. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  10. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. MAFFT 进行多序列比对

    简介 最经典和广为熟知的多序列比对软件是 clustalw . 但是现有的多序列比对软件较多,有文献报道:比对速度(Muscle>MAFFT>ClustalW>T-Coffee),比 ...

  2. android 点击图片从Fragment跳转到activity

    android 点击图片从Fragment跳转到activity 在Fragment里编写 public View onCreateView(@NonNull LayoutInflater infla ...

  3. java四则运算规则

    java四则运算规则 1.基本规则 运算符:进行特定操作的符号.例如:+ 表达式:用运算符连起来的式子叫做表达式.例如:20 + 5.又例如:a + b 四则运算: 加:+ 减:- 乘:* 除:/ 取 ...

  4. 学习java 7.26

    学习内容: 进度条是图形界面中广浅个较大的文件时,操作系统会显示一个进度条,用于标识复制操作完成的比例:当启动Eclipse等程序时,因为需要加载较多的资源,故而启动速度较慢,程序也会在启动过程中显示 ...

  5. 学习java 7.10

    学习内容: List 集合:有序集合,用户可以精确控制列表中每个元素的插入位置 List 集合特点:有序:存储和取出的元素顺序一致 可重复:存储的元素可以重复 增强for循环:简化数组和 Collec ...

  6. day12 keepalived高可用

    day12 keepalived高可用 一.高可用介绍 1.什么是高可用 部署在整个集群中的一个高可用软件,作用是创建一个VIP(虚拟IP),在整个集群中有且只有一个机器上生成VIP,当这台机器出现问 ...

  7. collection映射

    讲了manyToOne和oneToMany,下面来看看get方法.在之前已经说过,如果是映射单对象,直接使用association来映射.而如果关系 是一个集合,则需要使用collection来描述. ...

  8. Type difference of character literals in C and C++

    Every literal (constant) in C/C++ will have a type information associated with it. In both C and C++ ...

  9. 运维笔记之yum,rpm,挂载,磁盘管理和raid详解

    yum 与 rpm centos6,7 主要有rpm和yum这两种包管理软件,两种包的管理各有用处,其中最主要区别是:  yum使用简单但需要联网,yum会去网上的yum包源去获取所需要的软件包.而r ...

  10. Controller返回类的自动识别,WEB-INF,jsp位置

    Controller: @Controller@RequestMapping("/params")public class ParamsController { @RequestM ...