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,使得 a 2 + b 2 = c a^2 + b^2 = c a2+b2=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学习视频!

大白技术控 CSDN认证博客专家 CSDN博客专家 .NET开发者 计算机硕士
本人个人博客: 极客玩家大白,欢迎关注本人公众号:大白技术控。
本博主大白BravoYeung是985高校计算机硕士,目前任职于一家高科技外企。平时的我是知乎干货答主(知乎上有近4万关注者)和CSDN博客专家。
我的公众号【大白技术控】(ID:DaBaiTech)专注于分享IT前沿技术、干货知识、热点资讯等,同时分享硬核的自媒体赚钱方法、推广技巧和运营实战,最近在发力短视频(微信视频号为主)。有兴趣交流成长的小伙伴可以来撩(微信:iMath7)~
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值