版权声明: 本文为博主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 ,你要判断是否存在两个整数 a和 b,使得 a2+b2=c" role="presentation">a2+b2=ca2+b2=c。

示例1:

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

示例2:

  1. 输入: 3
  2. 输出: False

Input:

5

2

100

Expected answer:

true

true

true


  ●  题目难度: 简单

思路:

做一次循环,用目标和减去循环变量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true,否则返回false。循环变量i满足 i2⋅2&lt;c2" role="presentation">i2⋅2<c2i2⋅2<c2.

已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. if ((int)(Math.Ceiling(Math.Sqrt(diff))) == (int)(Math.Floor(Math.Sqrt(diff))))
  9. return true;
  10. }
  11. return false;
  12. }
  13. }

Rank:

You are here! Your runtime beats 56.14% of csharp submissions.



优化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:

You are here! Your runtime beats 85.96% of csharp submissions.

优化2:

  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. if ((0x0213 & (1 << (num & 15))) != 0)
  16. {
  17. int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5);
  18. return t * t == num;
  19. }
  20. return false;
  21. }
  22. }

Rank:

You are here! Your runtime beats 85.96% of csharp submissions.

优化3:

  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:

You are here! Your runtime beats 85.96% of csharp submissions.

另外,stackoverflow上还推荐了一种写法:

  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. if (Math.Abs(Math.Sqrt(diff) % 1) < 0.000001)
  9. return true;
  10. }
  11. return false;
  12. }
  13. }

事实上,速度并不快:

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/

C#版 - Leetcode 633. 平方数之和 - 题解的更多相关文章

  1. Java实现 LeetCode 633 平方数之和(暴力大法)

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

  2. LeetCode 633. 平方数之和

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

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

    上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...

  4. leetcode.双指针.633平方数之和-Java

    1. 具体题目 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a^2 + b^2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 注 ...

  5. 力扣(LeetCode)平方数之和 个人题解

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

  6. [LeetCode] 633. Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  7. [LeetCode] Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  8. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. 【JavaScript】Leetcode每日一题-平方数之和

    [JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...

随机推荐

  1. 动态网页获取ajax,post方法,url里面不直接显示参数

    记录一下,爬去ajax数据时,需要注意一下是post方法还是get方法,get方法就正常做就行了,但是post方法的话,需要这样,如下 a = requests.request('post',url) ...

  2. Android Studio之回退Gradle版本方法

    Android Studio之回退Gradle版本方法 (Minimum supported Gradle version is 4.10.1. Current version is 4.6.)   ...

  3. Spring-Docker简易指南

     使用代码:https://files.cnblogs.com/files/miracle9527/demo4springboot.rar # 约定#为注释行.$为命令行 # 开始操作前将demo4s ...

  4. org.apache.http.client.ClientProtocolException: URI does not specify a valid host name

    问题截图: 原因:http:// 少了两个//

  5. Exp1 PC平台逆向破解 20164302 王一帆

    1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程 ...

  6. 数据分析——pandas

    简介 import pandas as pd # 在数据挖掘前一个数据分析.筛选.清理的多功能工具 ''' pandas 可以读入excel.csv等文件:可以创建Series序列,DataFrame ...

  7. Debug命令详解

    Debug在学习汇编的过程中,担任着一个非常重要的角色,是一个极其重要的调试工具,所以学会它是必须的. 命令格式 功能说明 A [地址] 输入汇编指令 C [范围] 起始地址 对由“范围”指定的区域与 ...

  8. tf.contrib.slim add_arg_scope

    上一篇文章中我们介绍了arg_scope函数,它在每一层嵌套中update当前字典中参数形成新的字典,并入栈.那么这些参数是怎么作用到代码块中的函数的呢?比如说如下情况: with slim.arg_ ...

  9. centos7安装kubeadm

    安装配置docker v1.9.0版本推荐使用docker v1.12, v1.11, v1.13, 17.03也可以使用,再高版本的docker可能无法正常使用. 测试发现17.09无法正常使用,不 ...

  10. 开发中少不了的Fun -- 微信开发IOS端alert/confirm提示信息,去除网址(URL)的方法

    在微信公众号开发的时候在使用[alert/confirm]弹出提示或者警告信息的时候,[alert/confirm]会将该公众号的网址显示出来,这样很不美观.所以很多时候我们会选择去除那个网址提示内容 ...