C#LeetCode刷题之#342-4的幂(Power of Four)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4058 访问。
给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。
输入: 16
输出: true
输入: 5
输出: false
进阶:你能不使用循环或者递归来完成本题吗?
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Given num = 16, return true.
Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
Credits:Special thanks to @yukuairoy for adding this problem and creating all test cases.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4058 访问。
public class Program {public static void Main(string[] args) {var n = 16;var res = IsPowerOfFour(n);Console.WriteLine(res);n = 257;res = IsPowerOfFour2(n);Console.WriteLine(res);Console.ReadKey();}public static bool IsPowerOfFour(int num) {if(num <= 0) return false;while(num % 4 == 0) {num >>= 2;}return num == 1;}public static bool IsPowerOfFour2(int num) {while(num % 4 == 0 && (num >>= 2) > 1) { }return num == 1;}}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4058 访问。
True
False
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#342-4的幂(Power of Four)相关推荐
- leetcode刷题笔记342 4的幂
题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例: 当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不 ...
- LeetCode刷题实战354:俄罗斯套娃信封问题
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战355:设计推特
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...
- LeetCode刷题笔记汇总
LeetCode刷题笔记汇总 第一次刷LeetCode写的一些笔记. 1.两数之和 3.无重复字符的最长子串 15.三数之和 18.四数之和 19.删除链表的倒数第 N 个结点 20.有效的括号 21 ...
- 个人LeetCode刷题记录(带题目链接及解答)持续更新
Leetcode 刷题 注:~[完成]代表还有一些方法没看,最后再看 一.一些需要重刷的典型题: 1.快速排序,归并排序,堆排序(递归的思想) 2.链表中的回文链表,其中的快慢指针,多看,多练 3.链 ...
- Leetcode-How-What 力扣Leetcode刷题指南
Leetcode-How-What 力扣Leetcode刷题指南 About the way how to use Leetcode wisely for preparing the intervie ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
最新文章
- linux下防火墙的管理工具firewall-cmd
- 详解Linux多线程编程
- 基于计算机的控制系统,基于计算机的机械控制系统实现方式研究.pdf
- Linux(12)-命令行的使用,shell脚本
- 图文存储常识:单机、集中、分布式、云、云原生存储
- Python案例:格式化输出斐波拉契数列
- WebLogic Clustering Overview Slides
- 5种速成数据分析方法
- 小点向前走,我们在后头 -- 转自点妈文集
- 自制solidworks图框步骤_solidworks工程图模板制作教程(上) - CAD自学网
- Apple Pay接入详细教程
- matlab二进制香农,香农编码及MATLAB实现.ppt
- npm 安装yarn
- Apple_Mail 登录163邮箱账号
- 学习CNN的比较好的网站
- 快速剪辑视频,每个视频按秒数快速分割,并保留原声
- Vue + Element UI 表格分页记忆选中
- 滴滴副总裁叶杰平离职,他是出行巨头的AI掌门人
- 360n4s高通版卡刷包
- 剪贴板占用计算机的内存,王朝知道我提了几个问题后,电脑就说内存不足,然后死机,怎么办?...