Hyoseo Lee
Home About Projects Blog Thinking
leetcode

367. Valid Perfect Square

Topic Binary Search
Area Algorithms
Summary
just compare the num with all the perfect squares from 1 to max.

Problem

View on LeetCode →

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

Thoughts

this one was easy.