My Java Solution


  • 1

    Well...I think the time cost limit of OJ is too strict for this problem. If you go from num to num/i, you get TLE.

    public boolean checkPerfectNumber(int num) {
        if(num<=1) return false;
        int sum = 0;
        for(int i = 1;i<=num/i;i++){
            if(num%i==0){
                sum+=i;
                if(i!=sum/i){
                    sum+=num/i;
                }
            }
        }
        return sum==num;
    }

Log in to reply
 

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