版权声明: 本文为博主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. jquery.string.js

    /** * jquery.string - Prototype string functions for jQuery * version: 1.1.0 * (c) 2008-2011 David E ...

  2. python代码规范与标准库参考

    python代码规范与标准库参考 python代码规范参考文献: http://www.runoob.com/w3cnote/google-python-styleguide.html https:/ ...

  3. python中for嵌套打印图形

    # 打印出九九乘法表 1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 1 ...

  4. Scrapy框架之CrawlSpider

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  5. 练习2-1 Programming in C is fun!

    练习2-1 Programming in C is fun! 一 问题描述 本题要求编写程序,输出一个短句“Programming in C is fun!”. 输入格式: 本题目没有输入. 输出格式 ...

  6. linux视频录制,推流处理

    1.linux视频合成(视频后缀要一致) ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -lavfi hstack=inputs=3 4.mp4 (input=3表示希望合并的视 ...

  7. Hibernate HQL ③

    迫切左外连接: - LEFT JOIN FETCH 关键字表示迫切左外连接检索策略 - list()方法返回的集合中存放实体对象的引用,每个 Department 对象关联的 Employee 集合都 ...

  8. springboot增删改查

    改https://blog.csdn.net/weixin_42338186/article/details/81561592 添加https://blog.csdn.net/weixin_42338 ...

  9. HBase MVCC 机制介绍

    关键词:MVCC HBase 一致性 本文最好结合源码进行阅读 什么是MVCC ? MVCC(MultiVersionConsistencyControl , 多版本控制协议),是一种通过数据的多版本 ...

  10. 如何设置body高度为浏览器高度

    html{height:100%} body{min-height:100%} 有时我们的页面上内容不多,但设计师要求背景色必须铺满全屏,这时候只需在样式表中加上这行,body就以浏览器的高度显示,超 ...