From ec52d094831b9b9e936cf4c17b83abc3476799dc Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Fri, 5 Aug 2022 17:11:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=20Rust=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0738.单调递增的数字 Rust版本 --- problems/0738.单调递增的数字.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 6b7381f3..b452d02d 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -278,5 +278,26 @@ object Solution { } ``` +### Rust + +```Rust +impl Solution { + pub fn monotone_increasing_digits(n: i32) -> i32 { + let mut str_num = n.to_string().chars().map(|x| x.to_digit(10).unwrap() as i32).collect::>(); + let mut flag = str_num.len(); + for i in (1..str_num.len()).rev() { + if str_num[i - 1] > str_num[i] { + flag = i; + str_num[i - 1] -= 1; + } + } + for i in flag..str_num.len() { + str_num[i] = 9; + } + str_num.iter().fold(0, |acc, x| acc * 10 + x) + } +} +``` + -----------------------