Difficulty: Easy

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/sum-of-square-numbers/submissions/

Given a non-negative integer c, your task is to decide whether there're two integers aand b such that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3
Output: False

Intuition

Using two pointers: One starts from the beginning, the other starts from the end.

It's very similar to Two Sum II - Input array is sorted

Solution

    public boolean judgeSquareSum(int c) {
int i=0, j=(int)Math.sqrt(c);
while(i<=j){
int ans=i*i+j*j;
if(ans<c){
i++;
}else if(ans>c){
j--;
}else{
return true;
}
}
return false;
}

  

Complexity

Time complexity : O(n)

Space complexity :  O(1)

What I've learned

1.The use of two pointers.

 More:【目录】LeetCode Java实现

【LeetCode】633. Sum of Square Numbers的更多相关文章

  1. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  2. 【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. ...

  3. 【Leetcode_easy】633. Sum of Square Numbers

    problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...

  4. 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...

  5. 【leetcode】985. Sum of Even Numbers After Queries

    题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = quer ...

  6. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  7. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  8. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  9. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

随机推荐

  1. NSQ端口关系以及注意事项

    0.相关参考文章: 官网:https://nsq.io/ <golang实战-nsq集群入门与坑> <nsq系统架构> <NSQ消息队列> 1.启动命令 ①nsql ...

  2. 6.redis 的持久化有哪几种方式?不同的持久化机制都有什么优缺点?持久化机制具体底层是如何实现的?

    作者:中华石杉 面试题 redis 的持久化有哪几种方式?不同的持久化机制都有什么优缺点?持久化机制具体底层是如何实现的? 面试官心理分析 redis 如果仅仅只是将数据缓存在内存里面,如果 redi ...

  3. 使用Python3导出MySQL查询数据

    整理个Python3导出MySQL查询数据d的脚本. Python依赖包: pymysql xlwt Python脚本: #!/usr/bin/env python # -*- coding: utf ...

  4. 【Spring Cloud】Spring Cloud之自定义@SpringCloudProfile注解实现@Profile注解的功能

    一.为什么会想到定义@SpringCloudProfile这样的注解 首页提一下@Profile注解:它主要用与Spring Boot多环境配置中,指定某个类只在指定环境中生效,比如swagger的配 ...

  5. 基于Redisson+SpringBoot的Redission分布式锁

    原文:https://blog.csdn.net/sunct/article/details/80178197 定义分布式锁接口 package com.redis.lock.redisson_spr ...

  6. UiPath: Send SMTP Mail Message 发送带附件的邮件

    Tips:关于Hotmail的server和port的获取方式,请参考以下链接 https://support.office.com/en-us/article/Server-settings-you ...

  7. ubuntu配置定时任务crontab何保存退出

    crontab -e配置完成后,如何把保存并退出? 1.Ctrl+o 写入 2.出现“FIile name to Write...”,输入Enter 3.Ctrl+x 保存输出 提示“crontab: ...

  8. 从websocket协议出发,了解应用层协议,传输层协议,网络的7层协议

    其他关联连接 :TCP的三次握手(建立连接)和四次挥手(关闭连接) 1.websocket是全双工,不同于传统半双工通信 传统的Web应用中,浏览器与服务器交互都是半双工通信(但并不完全是半双工通信, ...

  9. echars配置案例-reactnative

    option = { backgroundColor:'#fff', grid: { left: '3%', right: '4%', top:, bottom: '6%', containLabel ...

  10. JDOJ 2175: 忠诚2

    JDOJ 2175: 忠诚2 题目传送门 Description 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让 ...