From 018d6b39dc586fc39420d350bfb8628b8ce50e5e Mon Sep 17 00:00:00 2001 From: xiaolatiao <1628652790@qq.com> Date: Tue, 25 Jul 2023 21:01:58 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2496 Signed-off-by: xiaolatiao <1628652790@qq.com> --- .../README.md | 54 +++++++++++++++++++ .../README_EN.md | 54 +++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md b/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md index 8368c81478aee..b3a557c5338cb 100644 --- a/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md +++ b/solution/2400-2499/2496.Maximum Value of a String in an Array/README.md @@ -210,6 +210,60 @@ impl Solution { } ``` +```rust +impl Solution { + pub fn maximum_value(strs: Vec) -> i32 { + let parse = |s: String| -> i32 { + let mut x = 0; + + for c in s.chars() { + if c >= 'a' && c <= 'z' { + x = s.len(); + break + } + + x = x * 10 + (c as u8 - b'0') as usize + } + + x as i32 + }; + + let mut ans = 0; + for s in strs { + let v = parse(s); + if v > ans { + ans = v; + } + } + + ans + } +} +``` + +```rust +use std::cmp::max; + +impl Solution { + pub fn maximum_value(strs: Vec) -> i32 { + let mut ans = 0; + + for s in strs { + match s.parse::() { + Ok(v) => { + ans = max(ans, v); + }, + Err(_) => { + ans = max(ans, s.len() as i32); + } + } + } + + ans + } +} +``` + ### **C** ```c diff --git a/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md b/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md index 795c9b4a3db91..f09b14f26dc3b 100644 --- a/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md +++ b/solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md @@ -193,6 +193,60 @@ impl Solution { } ``` +```rust +impl Solution { + pub fn maximum_value(strs: Vec) -> i32 { + let parse = |s: String| -> i32 { + let mut x = 0; + + for c in s.chars() { + if c >= 'a' && c <= 'z' { + x = s.len(); + break + } + + x = x * 10 + (c as u8 - b'0') as usize + } + + x as i32 + }; + + let mut ans = 0; + for s in strs { + let v = parse(s); + if v > ans { + ans = v; + } + } + + ans + } +} +``` + +```rust +use std::cmp::max; + +impl Solution { + pub fn maximum_value(strs: Vec) -> i32 { + let mut ans = 0; + + for s in strs { + match s.parse::() { + Ok(v) => { + ans = max(ans, v); + }, + Err(_) => { + ans = max(ans, s.len() as i32); + } + } + } + + ans + } +} +``` + ### **C** ```c