前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和

C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和

作者头像
Enjoy233
发布2019-10-27 00:19:08
4640
发布2019-10-27 00:19:08
举报

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/yanglr2010/article/details/102693759

前文传送门:

上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~

今天要给大家分析的面试题是 LeetCode 上第 633 号问题,

Leetcode 633 - 平方数之和

https://leetcode.com/problems/sum-of-square-numbers/

题目描述

给定一个非负整数c ,你要判断是否存在两个整数a和 b,使得

640?wx_fmt=png
640?wx_fmt=png

示例1:

代码语言:javascript
复制
输入: 5	
输出: True	
解释: 1* 1+ 2* 2= 5

示例2:

代码语言:javascript
复制
输入: 3	
输出: False

Input:

代码语言:javascript
复制
5	
2	
100

Expected answer:

代码语言:javascript
复制
true	
true	
true

  • 题目难度: 简单
  • 贡献者:Stomach_ache

相关话题

  • 数学 https://leetcode-cn.com/tag/math

相似题目

  • 有效的完全平方数 https://leetcode-cn.com/problems/valid-perfect-square

解题思路:

方法1: 遍历

做一次循环,用目标和减去循环变量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true;否则返回false。

假定

640?wx_fmt=png
640?wx_fmt=png

,根据数据的对称性,循环变量 i 只需取到 

640?wx_fmt=png
640?wx_fmt=png

 即可覆盖所有情形.

时间复杂度: O(n)

方法2: 双指针法

左指针 l=0,右指针 r = √C,夹逼条件是 ll + rr = C

感谢 博客园园友 msp的昌伟哥哥 的补充和指正~

时间复杂度: log(n)

方法1 已AC代码:

最初版本:

代码语言:javascript
复制
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:

执行用时: 56ms, 在所有 csharp 提交中击败了 68.18%的用户.

优化1:

代码语言:javascript
复制
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:

执行用时: 52ms, 在所有 csharp 提交中击败了 90.91% 的用户.

优化2(根据文末参考资料[1]中MPUCoder 的回答改写,16进制下mod16减少比较次数):

代码语言:javascript
复制
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)	
    {	
        //TRUE only if n mod 16 is 0,1,4,or 9	
        if ((0x0213 & (1 << (num & 15))) != 0)  	
        {	
            int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5);	
            return t * t == num;	
        }	
        return false;	
    }	
}

Rank:

执行用时: 44ms, 在所有 csharp 提交中击败了 100.00% 的用户.

640?wx_fmt=png
640?wx_fmt=png

优化3(根据文末参考资料[1]中 Simon 的回答改写):

代码语言:javascript
复制
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:

执行用时: 48ms, 在所有 csharp 提交中击败了 100.00%的用户.

方法2 已AC代码:

代码语言:javascript
复制
    public class Solution	
    {	
        public bool JudgeSquareSum(int c)	
        {	
            var r = (int)Math.Sqrt(c);	
            var l = 0;	
            while (l <= r)	
            {	
                var sum = l * l + r * r;	
                if (sum == c)	
                    return true;	
                if (sum < c)	
                    l++;	
                else	
                    r--;	
            }	
            return false;	
        }	
        // 以下为测试	
        public static void Main(string[] args)	
        {	
            var sol = new Solution();	
            var res = sol.JudgeSquareSum(25);	
            Console.WriteLine(res);	
        }	
    }

Rank: 

执行用时: 40ms, 在所有 csharp 提交中击败了 100.00% 的用户.

相比较而已,双指针法确实更快一些~

相应代码已经上传到github:

https://github.com/yanglr/Leetcode-CSharp/tree/master/leetcode633

参考资料:

[1] 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/

[2] Shortest way to check perfect Square? - C#

https://stackoverflow.com/questions/4885925/shortest-way-to-check-perfect-square/4886006#4886006

End

作者简介:Bravo Yeung,计算机硕士,知乎干货答主(获81K 赞同, 37K 感谢, 234K 收藏)。曾在国内 Top3互联网视频直播公司工作过,后加入一家外企做软件开发至今。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档