LeetCode 633. Sum of Square Numbers平方数之和 (C++)
题目:
Given a non-negative integer c
, your task is to decide whether there're two integers a
and b
such that a2 + b2 = c.
Example 1:
- Input: 5
- Output: True
- Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
- Input: 3
- Output: False
分析:
给定一个非负整数c ,你要判断是否存在两个整数a和b,使得 a^2 + b^2 = c。
我们可以给定一个a,来判断c-a*a是不是等于(sqrt(c-a*a))^2,因为一个数如果可以开方,且这个数开方后再平方的话和原来相等就是完全平方数,也就是我们所求的b。
程序:
- class Solution {
- public:
- bool judgeSquareSum(int c) {
- for(double a = ; a*a <= c; ++a){
- int b = sqrt(c - a*a);
- if(b*b == (c - a*a))
- return true;
- }
- return false;
- }
- };
LeetCode 633. Sum of Square Numbers平方数之和 (C++)的更多相关文章
- [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 ...
- Leetcode633.Sum of Square Numbers平方数之和
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...
- #Leetcode# 633. Sum of Square Numbers
https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...
- 【Leetcode_easy】633. Sum of Square Numbers
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【leetcode】633. Sum of Square Numbers(two-sum 变形)
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】
Given a non-negative integer c, your task is to decide whether there're two integers a and bsuch tha ...
随机推荐
- js常见执行方法window.onload = function (){},$(document).ready()
1. window.onload = function(){}; 当页面DOM对象加载完毕,web浏览器能够运行JS时,此方法即被触发. 2. $(document).ready();当web页面以及 ...
- 【Python求助】在eclipse和pycharm中,通过adb install安装中文名字APK时老是报错,如何解决
# -*- coding: utf-8 -*- import os import sys import subprocess import time from uiautomator import d ...
- 元素视差方向移动jQuery插件-类似github 404页面效果
原文地址:http://www.xuanfengge.com/shake.html 前言: 视差滚动,大家也许并不陌生.但是对于视差方向移动,你是否有见过效果呢?看官请进来瞧瞧~ demo : 轩枫阁 ...
- Android调用相机拍照并返回路径和调用系统图库选择图片
调用系统图库: Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); ...
- JS编写简单的弹窗插件(含有demo和源码)
最近项目做完了 事情不是很多,今天正好也在调休,所以趁着这个时间研究了一下简易的JS弹窗功能,当然网上这块插件非常多,本人也没有仔细看网上的插件源码 只是凭着日常使用过的弹窗插件有这么多功能 来实现自 ...
- Java IO详解(三)------字节输入输出流
File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...
- 【小程序】访问 https配置的数据接口
小程序对于网络请求的URL的特殊要求:1)不能出现端口号; 2)不能用localhost; 3) 必须用https (一)搭建本地https服务器(windows) 搭建出来的服务 ...
- Delphi 10.3 Rio + iOS 12.1 SDK 编译错误 "libcharset.1.dylib"
环境版本: Delphi 10.3 Rio iOS 12.1 SDK Xcode 10.1 (10B61) 错误讯息:[DCC Error] E2597 ld: file not found: /us ...
- 20155204 王昊《网络对抗技术》EXP1 PC平台逆向破解
20155204 王昊<网络对抗技术>EXP1 PC平台逆向破解 (一)实验内容 一.掌握NOP.JNE.JE.JMP.CMP汇编指令的机器码 NOP:NOP指令即"空指令&qu ...
- frameset的各个frame之间互相访问的方法
工作中很少使用到frameset,对其了解也是十分有限,这里在网上找了点资料,摘抄了部分内容. (1)获得html页面上的frame window.frames可以获得本页面上所有frame集合,用法 ...