点击蓝字“dotNET匠人”关注我哟

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

前文传送门:

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

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

Leetcode 633 - 平方数之和

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

题目描述

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

示例1:

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

示例2:

输入: 3
输出: False

Input:

5
2
100

Expected answer:

true
true
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代码:

最初版本:

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:

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减少比较次数):

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% 的用户.

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

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代码:

    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互联网视频直播公司工作过,后加入一家外企做软件开发至今。

欢迎各位读者加入 .NET技术交流群,在公众号后台回复“加群”或者“学习”即可。

朕已阅 

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

  1. C#刷遍Leetcode面试题系列连载(6):No.372 - 超级次方

    点击蓝字"dotNET匠人"关注我哟 加个"星标★",每日 7:15,好文必达! 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工 ...

  2. c# 两个list比较_C#刷遍Leetcode面试题系列连载(1) 入门与工具简介(VS Code amp; VS)...

    什么要刷LeetCode 大家都知道,很多对算法要求高一点的软件公司,比如美国的FLAGM (Facebook.LinkedIn.Amazon/Apple.Google.Microsoft),或国内大 ...

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

    点击蓝字"dotNET匠人"关注我哟 加个"星标★",每日 7:15,好文必达! 什么要刷LeetCode 大家都知道,很多对算法要求高一点的软件公司,比如美国 ...

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

    前言 前文传送门: 上篇文章中我们主要科普了刷 LeetCode 对大家的作用,今天咱们就正式进行 LeetCode 算法题分析.很多人都知道计算机中有种思想叫 递归,相应地也出现了很多算法.解决递归 ...

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

    点击蓝字"dotNET匠人"关注我哟 加个"星标★",每日 7:15,好文必达! 前言 前文传送门: 上篇文章中我们分析了一个递归描述的字符串问题,今天我们来分 ...

  6. 2022-4-16 Leetcode 633.平方数之和

    第一版,使用双指针,但是造成了溢出. class Solution {public:bool judgeSquareSum(int c) {vector<int> arr(c+1);for ...

  7. leetcode 633. 平方数之和(双指针)

    给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例 1: 输入:c = 5 输出:true 解释:1 * 1 + 2 * 2 = 5 示例 2: 输入 ...

  8. LeetCode 633 平方数之和

    题目描述 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 题解 和两数之和那道题目的求解方式类似,需要注意整数的溢出. 代码 class Solutio ...

  9. LeetCode题库整理【Java】—— 1两数之和

    LeetCode题库整理[Java] 1.两数之和 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nu ...

最新文章

  1. 实现linux作为server时与windows间的数据同步
  2. 分享用mockplus原型图工具制作的
  3. Android-Presentation双屏异显-一看就懂篇
  4. 2018.12.15
  5. 设计模式C++实现(7)——观察者模式
  6. java 用面向接口编程的方式开发打印机_Java“打印机”模型理解面向接口编程。实现接口定义类,接口实现类,核心“业务”类分离...
  7. 《C++高级编程(第3版)》
  8. linux bash技巧_Bash提示技巧和窍门
  9. 源码分析Dubbo前置篇-寻找注册中心、服务提供者、服务消费者功能入口
  10. JavaScript之面向对象与原型笔记整理--------创建对象之原型(2)
  11. 几种.NET平台数据持久化框架介绍
  12. 基于ADS54J40的JESD204B ADC 1GHz采样逻辑开发笔记
  13. python需不需要编译_python需要编译么
  14. 继电保护原理5-变压器保护
  15. 1分钟教会你如何截图文字识别,建议收藏备用
  16. 一个大学教授让人发冷汗的讲演(浙大高分子物理郑强教授)
  17. python kivy教程,Python Kivy 中文教程:安装(Windows)
  18. “accountsservice:依赖: libaccountsservice0(= 0.6.40-2ubuntu11.3)但是0.6.40-2ubuntu11.6已经安装”解决方法
  19. Android极光推送jPush混淆时出错的解决方案 Stack size becomes negative after instruction [247] swap in [cn/jiguang/s
  20. 微积分的历史(五):发展之泰勒公式(上)

热门文章

  1. windows驱动程序编写_如何在Windows中回滚驱动程序
  2. geek_愚蠢的怪胎技巧:在Windows 7中启用秘密的“ How-To Geek”模式
  3. pc微信不支持flash_在出售PC之前,如何取消对Flash内容的授权
  4. 百度php editor图片上传到其他盘,百度编辑器Editor图片独立上传
  5. Animate与transform的使用
  6. python学习笔记 --- 随机数进阶
  7. 一个脚本实现全量增量备份,并推送到远端备份中心服务器
  8. 数据库设计-基础-1-教务科研申报系统设计UML用例图
  9. 如何部署同一个Spring boot web 应用到不同的环境
  10. PHP中常见的五种设计模式