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