342. Power of Four(One-line)

342. Power of Four

Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy

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?

One_line Code:

return(num&num-1) ==0 &&(num&1431655765);

时间: 2024-10-14 21:51:18

342. Power of Four(One-line)的相关文章

安装使用Entity Framework Power Tool Bate4 (Code First)从已建好的数据自动生成项目中的对应Model(新手贴,望各位大侠给予指点)

从开始学习使用MVC以后,同时也开始接触EF,很多原理都不是太懂,只知道安装了EF以后,点击哪里可以生成数据库对应的Model,不用再自己手写Model.这里记录的就是如何从已建立好的数据库生成项目代码中的Model.一是记录这种操作方式,二是方便那些和我一样的菜鸟同学快速生成Model.(其中的问题希望园里的大侠们给予指点!) 第一步:安装Entity Framework Power Tools Beta 4,打开VS2013——工具——扩展与更新 第二步:选择左边菜单的联机——在右上的搜索栏

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:Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目标签:Bit Manipulation 这道题目让我们判断一个数字是不是4的

342. Power of Four(LeetCode)

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? 1 class Solution { 2 public: 3 bool isPo

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

传送门 Description 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? 思路 题意:不使用循环和递归,求一个数是否是4的

LeetCode算法题-Power Of Two(Java实现)

这是悦乐书的第194次更新,第200篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第56题(顺位题号是231).给定一个整数,写一个函数来确定它是否是2的幂.例如: 输入:1 输出:true 说明:2^0 = 1 输入:16 输出:true 说明:2^4 = 16 输入:218 输出:false 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 此解法是做乘法,新建一个变量,

HDU 4461:The Power of Xiangqi(水题)

http://acm.hdu.edu.cn/showproblem.php?pid=4461 题意:每个棋子有一个权值,给出红方的棋子情况,黑方的棋子情况,问谁能赢. 思路:注意“ if a player has no Ma or no Pao, or has neither, his total offense power will be decreased by one”这句话即可. 1 #include <cstdio> 2 #include <cstring> 3 #inc

Power of Four(Difficulty: Easy)

题目: 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. 实现: 1 class Solution { 2 public: 3 bool isPowerOfFour(int num) { 4 return (num > 0) &&

leetcode_231题——Power of Two (位运算)

Power of Two Total Accepted: 11027 Total Submissions: 36750My Submissions Question Solution Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating a

UVA - 11149 Power of Matrix(矩阵倍增)

题意:已知N*N的矩阵A,输出矩阵A + A2 + A3 + . . . + Ak,每个元素只输出最后一个数字. 分析: A + A2 + A3 + . . . + An可整理为下式, 从而可以用log2(n)的复杂度算出结果. 注意:输入时把矩阵A的每个元素对10取余,因为若不处理,会导致k为1的时候结果出错. #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #in