O(1)find solution....tricky with c++


  • -1

    class Solution {
    public:
    map<int,int> hash;
    Solution(){
    for(int i=1;i<1000;i++){
    hash.insert(pair<int,int>(i*i,i));
    }
    }
    bool isPerfectSquare(int num) {
    if(hash.find(num)==hash.end())return false;
    return true;
    }
    };


  • 0
    X

    well, it will cost O(1000) to initiate the a Solution obj. And it can't work for 1001*1001.


  • 0

    yep,but it cost O(1000) 1 time, for multi tests, and cost O(1) to check~~ whatever haha :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.