C# LeetCode刷題 - Leetcode 633. 平方數之和 - 題解

版權聲明: 本文爲博主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,使得 a2+b2=ca^2 + b^2 = c

示例1:

輸入: 5
輸出: True
解釋: 1 * 1 + 2 * 2 = 5

示例2:

輸入: 3
輸出: False

Input:
5
2
100

Expected answer:
true
true
true


  ●  題目難度: 簡單
- 通過次數:1.1K - 提交次數:4.8K - 貢獻者: [Stomach_ache](https://leetcode.com/stomach_ache/)

思路:

做一次循環,用目標和減去循環變量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true,否則返回false。循環變量i滿足 $i^2 \cdot 2 < c^2 $.

已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 56.14% 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 85.96% 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 85.96% 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 85.96% 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 29.82% 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/


dotNET匠人 公衆號名片

文末彩蛋

微信後臺回覆“asp”,給你:一份全網最強的ASP.NET學習路線圖。


回覆“cs”,給你:一整套 C# 和 WPF 學習資源!


回覆“core”,給你:2019年dotConf大會上發佈的.NET core 3.0學習視頻!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章