==============================  2的幂次  ================================

 最佳解法

如果一个数是2的次方数的话,根据上面分析,那么它的二进数必然是最高位为1,其它都为0,那么如果此时我们减1的话,则最高位会降一位,其余为0的位现在都为变为1,那么我们把两数相与,就会得到0,用这个性质也能来解题,而且只需一行代码就可以搞定,如下所示:

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. return (n > ) && (!(n & (n - )));
  5. }
  6. };

递归

  1. public boolean isPowerOfTwo(int n) {
  2. if(n==)
  3. return true;
  4. if(n>= && n%==)
  5. return isPowerOfTwo(n/);
  6. return false;
  7. }

位运算

  1. public bool isPowerOfTwo(int n) {
  2. if(n<=)
  3. return false;
  4. return countBit(n)==;
  5. }
  6. public int countBit(int num){
  7. int count=;
  8. while(num!=){
  9. count += (num & );
  10. num >>= ;
  11. }
  12. return count;
  13. }

======================================= 3的幂次 ====================================

  1. //一个基本的事实就是如果n是3的x次方,那么以3为底对数后一定是一个整数,否则不是
  2. //还有枚举法,枚举所有可能的int范围内的3的幂次
  3. class Solution {
  4. public:
  5. bool isPowerOfThree(int n) {
  6. double res = log10(n) / log10(); //有精度问题,不要用以指数2.718为低的log函数
  7. return (res - int(res) == ) ? true : false;
  8. }
  9. };

题目不建议,但是用迭代是可以解的:如果一个数是3的x次方那么,反复除以3,最终一定等于1,return true

  1. class Solution {
  2. public:
  3. bool isPowerOfThree(int n) {
  4. int num=n;
  5. while(num> && num%==)
  6. num/=;
  7. return num==;
  8. }
  9. };

=================================== 4的幂次 ==========================================

自己的笨办法:如果是4的幂次,那么二进制只有一个1,0的个数为2,4,6,8等偶数

  1. class Solution {
  2. public:
  3. bool isPowerOfFour(int num) {
  4. if (num<) return false;
  5. if (num==) return true;
  6. int count_zero = ;
  7. int count_one = ;
  8. while(num!=){
  9. if ((num & )==)
  10. count_zero ++;
  11. else
  12. count_one ++;
  13. num >>= ;
  14. }
  15. if (count_one != || count_zero <)
  16. return false;
  17. if (count_zero%==)
  18. return true;
  19. else
  20. return false;
  21. }
  22. };

不符合要求的递归写法

  1. class Solution {
  2. public:
  3. bool isPowerOfFour(int num) {
  4. while (num && (num % == )) {
  5. num /= ;
  6. }
  7. return num == ;
  8. }
  9. };

用log的换底公式来做

  1. class Solution {
  2. public:
  3. bool isPowerOfFour(int num) {
  4. return num > && int(log10(num) / log10()) - log10(num) / log10() == ;
  5. }
  6. };

首先根据Power of Two中的解法二,我们知道num & (num - 1)可以用来判断一个数是否为2的次方数,更进一步说,就是二进制表示下,只有最高位是1,那么由于是2的次方数,不一定是4的次方数,比如8,所以我们还要其他的限定条件,我们仔细观察可以发现,4的次方数的最高位的1都是计数位,那么我们只需与上一个数(0x55555555) <==> 1010101010101010101010101010101,如果得到的数还是其本身,则可以肯定其为4的次方数:

  1. class Solution {
  2. public:
  3. bool isPowerOfFour(int num) {
  4. return num > && !(num & (num - )) && (num & 0x55555555) == num;
  5. }
  6. };

或者我们在确定其是2的次方数了之后,发现只要是4的次方数,减1之后可以被3整除:

  1. class Solution {
  2. public:
  3. bool isPowerOfFour(int num) {
  4. return num > && !(num & (num - )) && (num - ) % == ;
  5. }
  6. };

【easy】power of 2,3,4的更多相关文章

  1. 142. O(1) Check Power of 2【easy】

    142. O(1) Check Power of 2[easy] Using O(1) time to check whether an integer n is a power of 2. Have ...

  2. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  3. 【CF913G】Power Substring 数论+原根

    [CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...

  4. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  5. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  6. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  7. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  8. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  9. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

随机推荐

  1. 横线和文字一排,文字居中显示vertical-align: middle;

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. C# 递归构造树状数据结构(泛型),如何构造?如何查询?

    十年河东,十年河西,莫欺少年穷. 学无止境,精益求精 难得有清闲的一上午,索性写篇博客. 首先,我们需要准备一张表,如下范例: create table TreeTable ( TreeId ) no ...

  3. .Net Core应用框架Util介绍(四)

    上篇介绍了Util Angular Demo的目录结构和运行机制,本文介绍Util封装Angular的基本手法及背后的动机. Angular应用由Ts和Html两部分构成,本文介绍第一部分. Angu ...

  4. 爬虫之BS&Xpath

    BeautifulSoup 一 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...

  5. iUAP云运维平台v3.0全面支持基于K8s的微服务架构

    什么是微服务架构? 微服务(MicroServices)架构是当前互联网业界的一个技术热点,业内各公司也都纷纷开展微服务化体系建设.微服务架构的本质,是用一些功能比较明确.业务比较精练的服务去解决更大 ...

  6. react学习目录

    前面的话 React是如今热门的两大前端框架之一,它设计思路独特,性能卓越,逻辑简单,受到了大量开发者的喜爱.Vue的基本思路是基于HTML模板的扩展,而React的基本思路是基于JS语言的扩展.由于 ...

  7. 前置通知也能对参数进行加工 通过joiPoint这个方法

  8. python之正则表达式和re模块一

    摘要:正则表达式 re模块 一.正则表达式:只和字符串打交道,是一种用来约束字符串的规则 1.应用场景: 1,判断某一个字符串是否符合规则:注册页-判断手机号.身份证号 是否合法 注册某个账号的时候, ...

  9. Spring Boot程序获取tomcat启动端口

    package com.geostar.geostack.git_branch_manager.config; import org.springframework.beans.factory.ann ...

  10. PHP——模糊匹配文件|目录

    内置函数 glob函数 详解 http://www.w3school.com.cn/php/func_filesystem_glob.asp