commit 9989dcdfc9e49db9afa0fb26b499a1c1226876fd parent 6e713b3a1a7966646c5d6b7bf335b538ef3512f3 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Thu, 4 May 2023 09:49:13 -0500 Completed power of two problem using dart Diffstat:
| A | power-of-two/power-of-two.dart | | | 17 | +++++++++++++++++ |
1 file changed, 17 insertions(+), 0 deletions(-)
diff --git a/power-of-two/power-of-two.dart b/power-of-two/power-of-two.dart @@ -0,0 +1,17 @@ +//If a number is a power of two return true else return false. +//This code has a time complexity of log(n) where n is the input value. +//Time: 318ms Beats: 38.82% +//Memory: 144.6MB Beats: 41.18% + +class Solution { + bool isPowerOfTwo(int n) { + int guess = 1; + while(guess < n + 1){ + if(guess == n){ + return true; + } + guess = guess * 2; + } + return false; + } +}