Power of Two - Leetcode 231 - Python

preview_player
Показать описание


0:00 - Read the problem
0:10 - Drawing Explanation
2:00 - Coding Explanation
4:21 - Drawing Explanation
7:33 - Coding Explanation
8:50 - Drawing Explanation
11:41 - Coding Explanation

leetcode 231

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

it's worth studying bit manipulation. Not really that tricky (I'm dumb and can understand it), and feels good when you solve problems like this.

edmonddantes
Автор

another similar solution:
return n > 0 and n == (n & -n)

李炎-dq
Автор

I personally prefer the subtractive method because not every processor has divide, and divide is slower anyway even when available.

anon_y_mousse
Автор

I'm new to code katas and python in general: Why not just: return n > 0 and math.log2(n).is_integer()? Using built-in libraries is forbidden during code interviews? I know it's probably not the fastest and clearest solution but I think it is still easier to get then bitwise operations. Thank you!

wp
Автор

What about a binary search solution
For example
2 ^ x = 16
We will calculate all the possible values to x is between 0 & 16 and by using binary search. we will get the mid check if 2 ^ mid is greater or smaller or equal

Goe-kw
Автор

You can achieve the answer by left shift as well ->
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
for i in range(32):
ans = 1 << i
if n == ans :
return True
return False

dingus
Автор

Hi all,
I came up with the following solution when I was trying for a brute force approach. Apologies if this sounds silly but Is this good or bad brute force approach to come up with, while interviewing :

class Solution:
def isPowerOfTwo(self, n: int) -> bool:
# base case:
if n==1:
return True
# check if n is odd or less than 1 or check if n has more than one bits set to 1
if ((n%2)!=0) or n<=0 or (n & (n-1)!=0):
return False

return True

jshaikh
Автор

similar answer:
if n<=0:
return False
b=bin(n)
if b[2:].count("1")==1:
return True
return False

MarriRahul
Автор

A one liner problem!! I am actually unable to solve hard questions with acceptance rete less than 60%. Do you have any tips on how I can improve?

satyamjha
Автор

Funny how the fastest solution for me was a loop.

ecchioni
Автор

I couldn't get it the constant time explanation. How it is constant time even if it is integer max value? If so let's say when we do linear search in list which has size of int max value, can we say it is constant time?

MehmetDemir-xiyy
Автор

I couldn't help laughing when you said "I've been sloppy today"😆
I love you because you always tell the truth❤

shakhzod_shermatov
Автор

This is a bit more intuitive I think.


class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n <= 0:
return False

num = math.log2(n)

if num == int(num):
return True

return False

shenawy
Автор

Here after getting runtime error with loops haha

ETR
Автор

should i quit programming I think my solution is shit:
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
l=[]

for i in range(0, 100):
l.append(2**i)

for i in range(0, len(l)):
if n == l[i]:
return True
else:
return False

kartikeysaini
Автор

My solution is 99th percentile and uses sets

StephenRoseDuo
Автор

Mathematically, even -1 is an integer.

Invincible-vpzt
Автор

return n > 0 && (__builtin_popcount(n) == 1);

lakshaybansal