LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four)

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 的 N 次方。
难点在于后面的附加条件:不能用循环和递归。

首先先给个用递归的解法。

bool isPowerOfFour(int num)
{
    if(num == 1) return true;
    if(num <= 0) return false;
    if(num & 0x03) return false;

    return isPowerOfFour(num / 4);
}

然后再给一个用循环的解法:

bool isPowerOfFour(int num)
{
    if(num < 0) return false;
    do
    {
        if(num == 1) return true;
        if(num & 3) return false;
        num = num >> 2;
    }while (num);
    return false;
}

如果不用循环和递归,也是可以做的。比如穷举所有 4 的 N 次方。虽然这个代码看起来很丑陋,但是确实也满足题目的要求。

bool isPowerOfFour(int num)
{
    switch(num)
    {
    case 0x01:
    case 0x04:
    case 0x10:
    case 0x40:
    case 0x100:
    case 0x400:
    case 0x1000:
    case 0x4000:
    case 0x10000:
    case 0x40000:
    case 0x100000:
    case 0x400000:
    case 0x1000000:
    case 0x4000000:
    case 0x10000000:
    case 0x40000000:
        return true;
    default:
        return false;
    }
}

讲了这么多,该说说正题了。这个题目其实考察的是这么一个小知识点。 一个数 num,如果是 2 的 N 次方,那么有:

num & (num - 1) = 0

一个数 num 如果是 4 的 N 次方必然也是 2 的 N 次方。所以可以先判断 num 是否是 2 的 N 次方。然后再将 2 的 N 次方中那些不是 4 的 N 次方的数去掉。因此就有了下面的代码。

bool isPowerOfFour(int num)
{
    if(num <= 0) return false;
    if(num & (num - 1)) return false; // 先判断是否是 2 的 N 次方
    if(num & 0x55555555) return true; // 再将不是 4 的 N 次方的数字去掉
    return false;
}
时间: 2024-10-15 07:00:56

LeetCode 第 342 题(Power of Four)的相关文章

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) 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? 题目非常eas

LeetCode 第 231 题 (Power of Two)

LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of two. 这个题目有个特别简单的解法.当然.可以独自想出这种方法可不简单.这样的方法可以作为一个知识点记住就好了. class Solution { public: bool isPowerOfTwo(int n) { if(n <= 0) return false; return !(n &

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode第四题,Add Two Numbers

题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6