Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

先说明用循环:

如下

class Solution {
public:
bool isPowerOfFour(int num) {
if(num<)return false;
char position=-;
int i=;
while(i<=)
{
if(num&(<<(i)))
{
if(-!=position&&position!=i)
return false;
position=i;
}
++i;
}
if(position%)return false;
else return true;
}
};

然后我发现题目中提示,你可以不用循环或者递归吗?我就去搜寻了一下这个题目发现http://www.cnblogs.com/grandyang/p/5403783.html

发现第三种方法很巧妙

LeetCode 342. Power of Four的更多相关文章

  1. [LeetCode] 342. Power of Four 4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  2. LeetCode 342. Power of Four (4的次方)

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  3. Leetcode 342 Power of Four 数论

    题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...

  4. Python [Leetcode 342]Power of Four

    题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...

  5. [LeetCode] 342. Power of Four(位操作)

    传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...

  6. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  7. 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  8. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  9. 342. Power of Four(One-line)

    342. Power of Four     Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...

随机推荐

  1. hdu 4639 Hehe

    http://acm.hdu.edu.cn/showproblem.php?pid=4639 每一段 "hehe..... " 相互独立  将每一段 "hehe..... ...

  2. java内存泄露的理解与解决(转)

    Java内存管理机制 在C++语言中,如果需要动态分配一块内存,程序员需要负责这块内存的整个生命周期.从申请分配.到使用.再到最后的释放.这样的过程非常灵活,但是却十分繁琐,程序员很容易由于疏忽而忘记 ...

  3. Hadoop 2.2.0学习笔记20131210

    伪分布式单节点安装执行pi失败: [root@server- ~]# ./bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples ...

  4. enmo_day_01

    OCP : 三门,sql, 管理一,管理二 VUE 考试中心 OCM : 2天,5年经验 11g grid 12c cloud 个人技术博客 itpub, csdn, 51blog 自己买主机,域名, ...

  5. Android 计算器UI-TableLayout

    表格布局(TableLayout) <?xml version="1.0" encoding="utf-8"?> <TableLayout x ...

  6. Android四大组件之Activity(活动)及其布局的创建与加载布局

    Android四大组件之Activity(活动)及其布局的创建与加载布局 什么是Activity ? 活动(Activity)是包含用户界面的组件,主要用于和用户进行交互的,一个应用程序中可以包含零个 ...

  7. ViewHolder简洁写法

    ViewHolder holder = null;         if(convertView == null){                 convertView = mInflater.i ...

  8. CSS简单布局总结

    display  block       块级元素,占据一行 none       隐藏 inline      允许同一行显示,但不再有宽和高 inline-block   允许在一行的块级元素,可 ...

  9. 数据结构《16》----自动补齐实现《一》----Trie 树

    1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...

  10. C#中的var类型

    var关键字是C#3.0开始新增的特性,称为推断类型(其实也就是弱化类型的定义) . VAR可代替任何类型,编译器会根据上下文来判断你到底是想用什么类型,类似 OBJECT,但是效率比OBJECT高点 ...