Re: [閒聊] 每日leetcode

作者: yam276 ('_')   2024-06-17 13:52:02
※ 引述《sustainer123 (caster )》之銘言:
: https://leetcode.com/problems/sum-of-square-numbers
: 633. Sum of Square Numbers
: 給定一非負整數C 請回傳是否存在a,b兩數使得a**2 + b**2 == c
: 思路:
: two pointer
一樣雙指標
要轉成i64不然會不夠用
Code:
impl Solution {
pub fn judge_square_sum(c: i32) -> bool {
let c_i64 = c as i64;
let c_sqrt = (c as f64).sqrt() as i64;
let mut a: i64 = 0;
let mut b: i64 = c_sqrt;
while a <= b {
let square = a * a + b * b;
if square == c_i64 {
return true;
} else if square < c_i64 {
a += 1;
} else if square > c_i64 {
b -= 1;
}
}
false
}
}
作者: DJYOSHITAKA (Evans)   2024-06-17 13:56:00
拜託你別捲了
作者: digua (地瓜)   2024-06-17 13:59:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com