367. Valid Perfect Square
Topic Binary Search
Area Algorithms
Summary
just compare the num with all the perfect squares from 1 to max.
Problem
Difficulty: Easy
Tags: Math, Binary Search
Intuition
this one seemed easy. i can just compare the number with all the perfect squres and finds it. it will only take sqrt(n) time.
Approach
just compare the num with all the perfect squares from 1 to max.
Solution
bool isPerfectSquare(int num) {
int i, k;
i = 1;
k = 1;
while(true){
if(num == k){
return true;
}
i++;
if(i > 46340){
return false;
}
k = i*i;
if(k > num){
return false;
}
}
}
Complexity
-
Time:
-
Space:
Thoughts
this one was easy.