LeetCode 633. Sum of Square Numbers
来源:互联网 发布:剑三喵姐捏脸数据 编辑:程序博客网 时间:2024/06/07 22:40
Description
Given a non-negative integer c
, your task is to decide whether there’re two integers a
and b
such that
Example 1:
Input: 5
Output: True
Explanation:1 * 1 + 2 * 2 = 5Example 2:
Input: 3
Output: False
Analysis
这道题不需要很复杂的数论知识,纯暴力也可以过,但两层循环遍历可能的数字会TLE,要利用a
,b
两数之间的关系,一层循环即可,直接利用sqrt()函数和强制类型转换即可AC。
Code
class Solution {public: bool judgeSquareSum(int c) { for (long a = 0; a * a <= c; a++){ double b = sqrt(c - a * a); if ((int)b == b) return true; } return false; }};
Appendix
- Link: https://leetcode.com/problems/sum-of-square-numbers/
- Run Time: 13ms
阅读全文
0 0
- [leetcode]633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- [leetcode]633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- leetcode[Sum of Square Numbers]
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers。
- 【leetcode】633. Sum of Square Numbers(Python & C++)
- leetcode 633. Sum of Square Numbers 二分查找
- OOP三大特性及几大设计原则
- log4j的使用
- dwr中的部分问题和总结
- UltraEdit常用快捷键
- Spring定时器StopWatch
- LeetCode 633. Sum of Square Numbers
- TypeError: expected dynamic type 'string', but had type 'object'
- mybatis 教程(mybatis in action)
- Java中的session详解
- Hibernate之主键生成策略
- CF div2 C
- 帮你节省时间的100条建议
- Spring学习笔记(9)——注入参数
- ACM中的浮点数精度处理