打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

加个“星标”,每日 7:15,好文必达!

前文传送门:

C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介

C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

C#刷遍Leetcode面试题系列连载(3): No.728 - 自除数

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

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

Leetcode 633 - 平方数之和

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

题目描述

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

示例1:

  1. 输入: 5

  2. 输出: True

  3. 解释: 1* 1+ 2* 2= 5

示例2:

  1. 输入: 3

  2. 输出: False

Input:

  1. 5

  2. 2

  3. 100

Expected answer:

  1. true

  2. true

  3. true


  • 题目难度: 简单

  • 贡献者:Stomach_ache

相关话题

  • 数学

    https://leetcode-cn.com/tag/math

相似题目

  • 有效的完全平方数

    https://leetcode-cn.com/problems/valid-perfect-square


解题思路:

方法1: 遍历

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

假定

,根据数据的对称性,循环变量 i 只需取到 
 即可覆盖所有情形.

时间复杂度: O(n)

方法2: 双指针法

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

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

时间复杂度: log(n)

方法1 已AC代码:

最初版本:

  1. public class Solution

  2. {

  3. public bool JudgeSquareSum(int c)

  4. {

  5. for (int i = 0; c - 2 * i * i >= 0; i++)

  6. {

  7. double diff = c - i*i;

  8. // 若向上取整=向下取整,则该数开方后是整数

  9. if ((int)(Math.Ceiling(Math.Sqrt(diff))) == (int)(Math.Floor(Math.Sqrt(diff))))

  10. return true;

  11. }


  12. return false;

  13. }

  14. }

Rank:

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

优化1:

  1. public class Solution

  2. {

  3. public bool JudgeSquareSum(int c)

  4. {

  5. for (int i = 0; c - 2 * i * i >= 0; i++)

  6. {

  7. int diff = c - i*i;

  8. if (IsPerfectSquare(diff))

  9. return true;

  10. }


  11. return false;

  12. }

  13. private bool IsPerfectSquare(int num)

  14. {

  15. double sq1 = Math.Sqrt(num);

  16. int sq2 = (int)Math.Sqrt(num);

  17. if (Math.Abs(sq1 - (double)sq2) < 10e-10)

  18. return true;

  19. return false;

  20. }

  21. }

Rank:

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

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

  1. public class Solution

  2. {

  3. public bool JudgeSquareSum(int c)

  4. {

  5. for (int i = 0; i <= c && c - i * i >= 0; i++)

  6. {

  7. int diff = c - i*i;

  8. if (IsPerfectSquare(diff))

  9. return true;

  10. }


  11. return false;

  12. }

  13. public bool IsPerfectSquare(int num)

  14. {

  15. //TRUE only if n mod 16 is 0,1,4,or 9

  16. if ((0x0213 & (1 << (num & 15))) != 0)

  17. {

  18. int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5);

  19. return t * t == num;

  20. }

  21. return false;

  22. }

  23. }

Rank:

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

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

  1. public class Solution

  2. {

  3. public bool JudgeSquareSum(int c)

  4. {

  5. for (int i = 0; c - i * i >= 0; i++)

  6. {

  7. long diff = c - i*i;

  8. if (IsSquareFast(diff))

  9. return true;

  10. }


  11. return false;

  12. }


  13. bool IsSquareFast(long n)

  14. {

  15. if ((0x2030213 & (1 << (int)(n & 31))) > 0)

  16. {

  17. long t = (long)Math.Round(Math.Sqrt((double)n));

  18. bool result = t * t == n;

  19. return result;

  20. }

  21. return false;

  22. }

  23. }

Rank:

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

方法2 已AC代码:

  1. public class Solution

  2. {

  3. public bool JudgeSquareSum(int c)

  4. {

  5. var r = (int)Math.Sqrt(c);

  6. var l = 0;

  7. while (l <= r)

  8. {

  9. var sum = l * l + r * r;

  10. if (sum == c)

  11. return true;


  12. if (sum < c)

  13. l++;

  14. else

  15. r--;

  16. }


  17. return false;

  18. }


  19. // 以下为测试

  20. public static void Main(string[] args)

  21. {

  22. var sol = new Solution();

  23. var res = sol.JudgeSquareSum(25);

  24. Console.WriteLine(res);

  25. }

  26. }

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互联网视频直播公司工作过,后加入一家外企做软件开发至今。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
LeetCode面试系列 第5天:No.204 - 统计质数
​LeetCode刷题实战403: 青蛙过河
字符串的排列(双指针)
神奇的一行代码,让 Python 轻松跑赢 C
技术图文:集合技术在求解算法题中的应用
素数判定算法 | 董的博客
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服