Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

Leetcode: Power of Four

Posted on January 10, 2018September 23, 2019 by braindenny

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • Tag: #math

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?

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## https://code.dennyzhang.com/power-of-four
class Solution(object):
    def isPowerOfFour(self, num):
        return ((num - 1) & num == 0 and (num-1) % 3 == 0)
linkedin
github
slack

Post Views: 6
Posted in MediumTagged #inspiring, #math, #powerofn

Post navigation

Leetcode: Convert BST to Greater Tree
Leetcode: Set Matrix Zeroes

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #misc #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum baseconversion binarysearch concurrency editdistance hashmap knapsack monotone oodesign presum redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • Leetcode: Palindrome Partitioning III
  • Leetcode: Biggest Single Number
  • Leetcode: Max Consecutive Ones II
  • Leetcode: Minimum Cost to Connect Sticks
  • Leetcode: Minimum Swaps to Group All 1’s Together

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.