C#版 - Leetcode 633. 平方数之和 - 题解
版权声明: 本文为博主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:
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2:
输入: 3
输出: False
Input:
5
2
100
Expected answer:
true
true
true
| ● 题目难度: | 简单 |
- 通过次数:1.1K
- 提交次数:4.8K
贡献者: Stomach_ache
思路:
做一次循环,用目标和减去循环变量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true,否则返回false。循环变量i满足 i2⋅2<c2" role="presentation">i2⋅2<c2i2⋅2<c2.
已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:
You are here! Your runtime beats 56.14% of csharp submissions.
优化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:
You are here! Your runtime beats 85.96% of csharp submissions.
优化2:
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)
{
if ((0x0213 & (1 << (num & 15))) != 0)
{
int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5);
return t * t == num;
}
return false;
}
}
Rank:
You are here! Your runtime beats 85.96% of csharp submissions.
优化3:
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:
You are here! Your runtime beats 85.96% of csharp submissions.
另外,stackoverflow上还推荐了一种写法:
public class Solution
{
public bool JudgeSquareSum(int c)
{
for (int i = 0; c - 2 * i * i >= 0; i++)
{
double diff = c - i*i;
if (Math.Abs(Math.Sqrt(diff) % 1) < 0.000001)
return true;
}
return false;
}
}
事实上,速度并不快:
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. 平方数之和 - 题解的更多相关文章
- Java实现 LeetCode 633 平方数之和(暴力大法)
633. 平方数之和 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 ...
- LeetCode 633. 平方数之和
题目: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2 ...
- C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和
上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...
- leetcode.双指针.633平方数之和-Java
1. 具体题目 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a^2 + b^2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 注 ...
- 力扣(LeetCode)平方数之和 个人题解
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...
- [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 ...
- [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 ...
- C#版 - Leetcode 306. 累加数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【JavaScript】Leetcode每日一题-平方数之和
[JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...
随机推荐
- python kafka权限校验client.id
kafka集群有权限校验,在连接时需要加入client.id.但pykafka不能配置该选项.搜索了一下,需要使用confluent-kafka 链接: https://blog.csdn.net/l ...
- JavaWeb从开发环境搭建,到第一个servlet程序(图文)
## 开学到今天,已经是第三周了~ 然而这门课的教材还没发~ 滋滋滋 表示很“蓝瘦”~~~ Java Web开发环境搭建 1. 下载安装Tomcat 官网地址:http://tomcat. ...
- 在dcef3当中执行js代码并获得返回值
1.如何在dcef3当中执行js代码 procedure TForm1.btnWriteZMClick(Sender: TObject);var js: string;begin js := 'd ...
- 用万能马甲免费看VIP电影
什么是:万能马甲? 它浏览器的一个扩展程序插件,安装后能看一些热门网址的VIP特权,免费观看付费电影等. 使用过程: 1. 访问网址:http://www.wndd123.com/ 点击免费看电影时提 ...
- 怎么修改kodexplorer网盘下的版权
前言: 要说kodexplorer,可是个好东西,在线web管理服务器文件,着实是网站管理员的好助手.内置的adminer管理数据库,用起来也是很顺手. 这么好的工具,还是免费的.但就是页面底部有ko ...
- Openstack的视频学习
0.安装环境准备 部署架构: 网络模式(红色Net0为管理网络,Net1接外网,Net2是接虚拟机网络流量的): 虚拟化平台为VirtualBox,虚拟网络Host-Only网络的配置: Net0:管 ...
- 一步一步 copy163: 网易严选 ---- vue-cli
面试点 组件间通信 生命周期函数 路由 - 参数 - 重定向 vuex 参考 网易严选商城小程序全栈开发,域名备案中近期上线(mpvue+koa2+mysql) 小程序服务端源码地址 小程序源码地址 ...
- java课程之团队开发冲刺阶段1.2
一.总结昨天进度 1.三个任务都已经实现 2.使用时间:四个小时左右 二.遇到的困难 1.对Android原生的侧拉任务栏不了解,导致使用的时候出现了一部分问题 三.今天任务规划 1.对之前的程序重新 ...
- 清理 zabbix 历史数据, 缩减 mysql 空间
zabbix 由于历史数据过大, 因此导致磁盘空间暴涨, 下面是结局方法步骤 1. 停止 ZABBIX SERER 操作 [root@gd02-qa-plxt2-nodomain-web-95 ~] ...
- CF F. Shovels Shop(前缀和预处理+贪心+dp)
F. Shovels Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...