C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3885 访问。
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
输入: 3
输出: False
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Input: 3
Output: False
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3885 访问。
public class Program {
public static void Main(string[] args) {
var num = 3;
var res = CheckSumOfSquareNumbers(num);
Console.WriteLine(res);
num = 5;
res = CheckSumOfSquareNumbers2(num);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool CheckSumOfSquareNumbers(int c) {
//算是暴力解法
//不过没必要全部计算,否则肯定TLE
var sqrt = (int)Math.Sqrt(c);
for(var i = 0; i <= sqrt; i++) {
//计算值 c - i * i
double k = Math.Sqrt(c - i * i);
//判定是否相待,这是常用的判定开根号与目标值是否相等的写法
if((int)k == k) return true;
}
return false;
}
private static bool CheckSumOfSquareNumbers2(int c) {
//双指针法
var sqrt = (int)Math.Sqrt(c);
//存放计算 平方的和 的值
var sum = 0;
//前后指针
var i = 0;
var j = sqrt;
//指针碰撞时为止
while(i <= sqrt) {
sum = i * i + j * j;
if(sum < c) {
i++;
} else if(sum > c) {
j--;
} else {
return true;
}
}
return false;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3885 访问。
False
True
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)的更多相关文章
- [Swift]LeetCode633. 平方数之和 | 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刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- #leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- #leetcode刷题之路1-两数之和
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- #leetcode刷题之路18-四数之和
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
- (1)leetcode刷题Python笔记——两数之和
题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和
上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...
- C#版 - Leetcode 633. 平方数之和 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- SSM框架前后端信息交互
一.从前端向后端传送数据 常见的3种方式 1.form表单的action:此方法可以提交form表单内的输入数据,也可同时提交某些隐藏但设置有默认值的<input>,如修改问题时,我们除了 ...
- TCP 和 UDP,哪个更胜一筹
作为 TCP/IP 中两个最具有代表性的传输层协议,TCP 和 UDP 经常被拿出来相互比较.这些协议具体有什么区别,又是什么作用呢? 在 IT 圈混迹多年的小伙伴们,对 TCP 和 UDP 肯定再熟 ...
- Vue脚手架的搭建和路由配置
- dva + umi 学习笔记
首先,这里的 dva + umi 不是说学习dva和umi,而是基于 dva + umi 的react前端开发脚手架,这是我的目标. 一开始,我在dva文档的例子里看到了 dva + umi 的例子. ...
- deepin换源
python的pip源 pip install pip -U pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/sim ...
- 最近建了一个.net源码共享群,群共享有大量网友分享的.net(C#)商业源码
.net源码共享群 324087998. 本群创建于2013/6/21: 群里都是.net(C#)程序开发人员,群共享有大量网友分享的.net(C#)商业源码.比如:DTCMS旗舰版,hishop微分 ...
- DQL_MySQL
4.DQL(查询数据){SUPER 重点} 4.1DQL (Data Query Language : 数据查询语言) -所有的查询操作: Select 数据库中最核心的语言 create data ...
- php提取xml配置参数
demo1.php <?php class AddressManager{ private $addresses = array("ip地址1","ip地址2&qu ...
- feign.FeignException: status 404 reading xxService#xxmethod
做乐优商城授权中心出错 public interface UserApi { @GetMapping("query") public User queryUser( @Reques ...
- Python字符串内建函数_上
Python字符串内建函数: 注:汉字属于字符(既是大写又是小写).数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字. 1.capitalize( ): 将字符串第一个字母大 ...