LeetCode Power of Two

来源:互联网 发布:精通python网络爬虫pdf 编辑:程序博客网 时间:2024/06/03 18:25

原题链接在这里:https://leetcode.com/problems/power-of-two/

思路:n一直除以2,直到1,若中间过程出现非偶数,就返回false。

Note: 1 也是power of two, 2的零次方。 

AC Java:

public class Solution {    public boolean isPowerOfTwo(int n) {        if(n<1)            return false;        while(n!=1.0){            if((n%2) !=0)                return false;            n/=2;        }        return true;    }}


0 0
原创粉丝点击