版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm

C#版 - Leetcode 633. 平方数之和 - 题解

Leetcode 633 - Sum of square number

在线提交:
https://leetcode.com/problems/sum-of-square-numbers/

题目描述

给定一个非负整数 c ,你要判断是否存在两个整数 ab,使得 <nobr aria&#45;hidden="true"> a2+b2=c </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mi> a </mi> <mn> 2 </mn> </msup> <mo> + </mo> <msup> <mi> b </mi> <mn> 2 </mn> </msup> <mo> = </mo> <mi> c </mi> </math>

示例1:

输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5

示例2:

输入: 3
输出: False

Input:
5
2
100

Expected answer:
true
true
true


  ●  题目难度: 简单

思路:

做一次循环,用目标和减去循环变量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true,否则返回false。循环变量i满足 <nobr aria&#45;hidden="true"> i22<c2 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mi> i </mi> <mn> 2 </mn> </msup> <mo> ⋅ </mo> <mn> 2 </mn> <mo> < </mo> <msup> <mi> c </mi> <mn> 2 </mn> </msup> </math>.

已AC代码:
最初版本:

public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - 2 * i * i >= 0; i++)
        {
            double diff = c - i*i;
            if ((int)(Math.Ceiling(Math.Sqrt(diff))) == (int)(Math.Floor(Math.Sqrt(diff))))
                return true;
        }

        return false;
    }
}

Rank:

You are here! Your runtime beats <kbd>56.14%</kbd> of csharp submissions.

优化1:

public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - 2 * i * i >= 0; i++)
        {
            int diff = c - i*i;
            if (IsPerfectSquare(diff))
                return true;
        }

        return false;
    }
    private bool IsPerfectSquare(int num)
    {
        double sq1 = Math.Sqrt(num);
        int sq2 = (int)Math.Sqrt(num);
        if (Math.Abs(sq1 - (double)sq2) < 10e-10)
            return true;
        return false;
    }
}

Rank:
You are here! Your runtime beats <kbd>85.96%</kbd> of csharp submissions.

优化2:

public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; i <= c && c - i * i >= 0; i++)
        {
            int diff = c - i*i;
            if (IsPerfectSquare(diff))
                return true;
        }

        return false;
    }
    public bool IsPerfectSquare(int num)
    {
        if ((0x0213 & (1 << (num & 15))) != 0)
        {
            int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5);
            return t * t == num;
        }
        return false;
    }
}

Rank:
You are here! Your runtime beats <kbd>85.96%</kbd> of csharp submissions.

优化3:

public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - i * i >= 0; i++)
        {
            long diff = c - i*i;
            if (IsSquareFast(diff))
                return true;
        }

        return false;
    }

    bool IsSquareFast(long n)
    {
        if ((0x2030213 & (1 << (int)(n & 31))) > 0)
        {
            long t = (long)Math.Round(Math.Sqrt((double)n));
            bool result = t * t == n;
            return result;
        }
        return false;
    }
}

Rank:
You are here! Your runtime beats <kbd>85.96%</kbd> of csharp submissions.

另外,stackoverflow上还推荐了一种写法:

public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - 2 * i * i >= 0; i++)
        {
            double diff = c - i*i;
            if (Math.Abs(Math.Sqrt(diff) % 1) < 0.000001)
                return true;
        }

        return false;
    }
}

事实上,速度并不快:
Rank:
You are here!
Your runtime beats <kbd>29.82%</kbd> of csharp submissions.

Reference:

Fast way to test whether a number is a square
https://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/