zoukankan      html  css  js  c++  java
  • python1

    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?

    python解决方案:

    class Solution(object):
        def isPowerOfFour(self, num):
        
        """
        :type num: int
        :rtype: bool
        """
        while num > 0 and num & 3 == 0:
            num >>= 2
        return num == 1

    题解:  

    如果是4的倍数,表示成二进制的话

      4  100

      16   10000

      64   1000000

      就是这种形式

     所以代码的思路就是

     整型的num

     每回判断它的后两位是不是0 (也即和3相与结果是不是0)

     然后num=num/4  (即右移两位)

      判断最后剩下的是不是一。

  • 相关阅读:
    memset使用技巧
    04.碰撞反应
    03.键盘状态跟踪与精灵删除
    02.基本动作
    01.基本图形
    00.入门
    03.交互--鼠标,键盘
    02.action--新增精灵知识点
    01.helloworld--标签
    05.声音
  • 原文地址:https://www.cnblogs.com/wangccc/p/5405748.html
Copyright © 2011-2022 走看看