[LeetCode]633. Sum of Square Numbers

xiaoxiao2021-02-28  96

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

给一个数字c,判断是否满足a2 + b2 = c

双指针,end最大为c开根号,beg最小为0.判断当前beg和end是否满足,然后相应移动beg或者end

public class Solution { public boolean judgeSquareSum(int c) { int beg = 0; int end = (int) Math.sqrt(c); while (beg <= end) { int target = beg * beg + end * end; if (target > c) { end--; } else if (target < c) { beg++; } else { return true; } } return false; } }

转载请注明原文地址: https://www.6miu.com/read-58071.html

最新回复(0)