Re: [閒聊] 每日leetcode已回收
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/06/17 09:37)推噓1(1推 0噓 6→)留言7則, 3人參與討論串372/1548 (看更多)
https://leetcode.com/problems/sum-of-square-numbers
633. Sum of Square Numbers
給定一非負整數C 請回傳是否存在a,b兩數使得a**2 + b**2 == c
Example 1:
Input: c = 5
Output: true
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: c = 3
Output: false
Constraints:
0 <= c <= 231 - 1
思路:
two pointer
Python Code:
class Solution:
def judgeSquareSum(self, c: int) -> bool:
left = 0
right = math.isqrt(c)
while right >= left:
ans = right ** 2 + left ** 2
if ans == c:
return True
elif ans < c:
left += 1
else:
right -= 1
return False
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.112.186 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1718588279.A.7C6.html
推
06/17 09:38,
1年前
, 1F
06/17 09:38, 1F
→
06/17 09:39,
1年前
, 2F
06/17 09:39, 2F
→
06/17 09:39,
1年前
, 3F
06/17 09:39, 3F
→
06/17 09:40,
1年前
, 4F
06/17 09:40, 4F
→
06/17 09:40,
1年前
, 5F
06/17 09:40, 5F
→
06/17 09:40,
1年前
, 6F
06/17 09:40, 6F
→
06/17 09:41,
1年前
, 7F
06/17 09:41, 7F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 372 之 1548 篇):