Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9fd2403

Browse filesBrowse files
authored
Create 231_power-of-two.cpp
Add solution to LeetCode problem no. 231 "Power Of Two".
1 parent 7e478ce commit 9fd2403
Copy full SHA for 9fd2403

File tree

1 file changed

+47
-0
lines changed
Filter options

1 file changed

+47
-0
lines changed

‎231_power-of-two.cpp

Copy file name to clipboard
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public:
3+
bool isPowerOfTwo(int n) {
4+
if (n <= 0) {
5+
return false;
6+
}
7+
for (; n != 1; n >>= 1) {
8+
if (n & 0 != 1) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
14+
};
15+
16+
17+
/*
18+
// CodeHelp Solution:
19+
20+
#include<limits.h>
21+
22+
class Solution {
23+
public:
24+
bool isPowerOfTwo(int n) {
25+
26+
int ans = 1;
27+
28+
for(int i = 0; i <= 30; i++) {
29+
30+
//cout<<" ans "<<ans <<endl;
31+
if(ans == n)
32+
{
33+
return true;
34+
}
35+
if(ans < INT_MAX/2)
36+
ans = ans * 2;
37+
38+
}
39+
return false;
40+
}
41+
};
42+
*/
43+
44+
/*
45+
// Explation of the efficiency from ChatGPT
46+
https://chatgpt.com/share/a3481a36-6a40-48f4-98cf-39e92f34f545
47+
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.