leetcode (Sum of Square Numbers)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85453881

Title:Sum of Square Numbers    633

Difficulty:Easy

原题leetcode地址:   https://leetcode.com/problems/perfect-number/

1.  假设问题

时间复杂度:O(n),一次一层for循环,循环最长为根号n。

空间复杂度:O(1),没有申请额外空间。

    /**
     * 先假设a是,再判断b是不是
     * @param c
     * @return
     */
    public static boolean judgeSquareSum(int c) {

        if (c < 0) {
            return false;
        }

        for (int a = (int) Math.sqrt(c); a >= 0; a--) {
            if (a * a == c) {
                return true;
            }
            int db = c - a * a;
            int b = (int) Math.sqrt(db);
            if (b * b == db) {
                return true;
            }
        }

        return false;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85453881