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 ...
随机推荐
- 一分钟部署nacos
第一步:下载nacos包 https://github.com/alibaba/nacos/releases D:\testNacos\nacos-server-1.3.0\nacos\bin 最后 ...
- css:实现文本两行或多行文本溢出显示省略号
div{ display: -webkit-box; -webkit-box-orient: vertical; word-break: break-all; word-wrap: break-wor ...
- iframe子页面取父页面的变量问题
iframe包含的子页面,想获取父页面的变量,不能直接获取到. 但是子页面可以访问父页面的方法 window.parent.parentFunctionName(); 利用这一点,可以将父页面的变 ...
- 【管理员已阻止你运行此应用】windows defender图标打叉,无法打开mmc.exe解决办法
今天开机遇到一个奇怪的问题,发现windows defender图标上面打了个×: 打开按照系统提示需要restart服务,但是无法重启服务,会出现错误,然后尝试手动重启服务,准备打开管理控制台mmc ...
- 数据库(十三):MySQL内置方法
进击のpython ***** 数据库--MySQL内置方法 目录 数据库--MySQL内置方法 视图 增加 修改 删除 触发器 创建 使用 删除 存储过程 无参 有参 事务 代码实现 视图 视图是一 ...
- seaborn分类数据可视化:散点图|箱型图|小提琴图|lv图|柱状图|折线图
一.散点图stripplot( ) 与swarmplot() 1.分类散点图stripplot( ) 用法stripplot(x=None, y=None, hue=None, data=None, ...
- pandas之数值计算与统计
数值计算与统计 对于DataFrame来说,求和.最大.最小.平均等统计方法,默认是按列进行统计,即axis = 0,如果添加参数axis = 1则会按照行进行统计. 如果存在空值,在统计时默认会忽略 ...
- paramiko上传文件到Linux
一.传输单个文件到Linux服务器 import paramiko transport = paramiko.Transport(('host',22)) transport.connect(user ...
- linux root 与普通用户之间的切换
test@ubuntu:~$ su Password: root@ubuntu:/home/uu# 也可以是从root用户切换到普通用户.如果当前是root用户,那么切换成普通用户uu用以下命令:s ...
- SUM and COUNT -- SQLZOO
SUM and COUNT 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Show the total population of th ...