More:【目錄】LeetCode Java實現html
https://leetcode.com/problems/sum-of-square-numbers/submissions/java
Given a non-negative integer c
, your task is to decide whether there're two integers a
and b
such that a2 + b2 = c.ide
Example 1:post
Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5
Example 2:ui
Input: 3 Output: False
Using two pointers: One starts from the beginning, the other starts from the end.code
It's very similar to Two Sum II - Input array is sortedhtm
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; }
Time complexity : O(n)blog
Space complexity : O(1)ip
1.The use of two pointers.ci
More:【目錄】LeetCode Java實現