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. word2vector(含code)

    Word2Vec其实就是通过学习文本来用词向量的方式表征词的语义信息,即通过一个嵌入空间使得语义上相似的单词在该空间内距离很近. Embedding其实就是一个映射,将单词从原先所属的空间映射到新的多 ...

  2. 十七、Python面向对象之继承

    在面向对象,继承是一个很重要的特性 子类与父类,子类是对父类的一种扩展,在父类的属性和方法上进行一些扩展 示例:没带继承   #定义一个带编号和状态的门的类 class Door(object): d ...

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

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

  4. Two-Stream Adaptive Graph Convolutional Network for Skeleton-Based Action Recognition

    Two-Stream Adaptive Graph Convolutional Network for Skeleton-Based Action Recognition 摘要 基于骨架的动作识别因为 ...

  5. 查看mysql连接数和状态

    查看MySQL连接数 登录到MySQL命令行,使用如下命令可以查看当前处于连接未关闭状态的进程列表: show full processlist; 若不加上full选项,则最多显示100条记录. 若以 ...

  6. Kibana 的 docker 镜像使用

    1.dockhub镜像网址:https://hub.docker.com/_/kibana 2.下载镜像: docker pull kibana:7.4.0 3.创建容器(Kibana 默认的端口为5 ...

  7. Leecode之双指针及二分查找

    题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...

  8. 每天一道Rust-LeetCode(2019-06-02)

    每天一道Rust-LeetCode(2019-06-02) Z 字形变换 坚持每天一道题,刷题学习Rust. LeetCode原题 Z 字形变换 题目描述 将一个给定字符串根据给定的行数,以从上往下. ...

  9. 机器学习-MNIST数据集-神经网络

    #设置随机种子 seed = 7 numpy.random.seed(seed) #加载数据 (X_train,y_train),(X_test,y_test) = mnist.load_data() ...

  10. Linux中关于samba的几个问题

    一.用smbclient命令登录成功但看不了文件 原因:SELinux的阻挡 解决:1.关闭SELinux  :  setenforce 0   (临时生效,重启后失效) 或vi /etc/sysco ...