Leetcode每日一题 —— 3723. 数位平方和的最大值

impl Solution {
    pub fn max_sum_of_squares(num: i32, sum: i32) -> String {
        if num * 9 < sum {
            return "".to_string();
        }
        let mut ans = String::from("");
        for i in 1..=num {
            if sum - 9 * i > 0{
                ans.push('9');
            } else {
                let remaining = sum - 9 * (i - 1);
                ans.push((b'0' + remaining as u8) as char);
                ans.push_str(&("0").repeat((num - i) as usize));
                return ans;
            }
        }
        ans
    }
}

ovO

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论