diff options
| author | Mistivia <i@mistivia.com> | 2025-09-01 21:27:04 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-09-01 21:27:04 +0800 |
| commit | 6e4397623758ad5417a6b30858cefc223d51d282 (patch) | |
| tree | 4ac04fa1dcd24659bd2619fa64b896e2f1962b30 /rust/src/bin/p0014.rs | |
| parent | fff78815bae72051159a4a4ae5da34bea027317c (diff) | |
move
Diffstat (limited to 'rust/src/bin/p0014.rs')
| -rw-r--r-- | rust/src/bin/p0014.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/rust/src/bin/p0014.rs b/rust/src/bin/p0014.rs new file mode 100644 index 0000000..e85ef8f --- /dev/null +++ b/rust/src/bin/p0014.rs @@ -0,0 +1,30 @@ +impl Solution { + pub fn longest_common_prefix(strs: Vec<String>) -> String { + if strs.is_empty() { return "".to_string(); } + let mut l: i32 = -1; + let mut brk = false; + for i in 0..strs[0].len() { + let c = strs[0].as_bytes()[i]; + for j in 1..strs.len() { + if strs[j].len() <= i || strs[j].as_bytes()[i] != c { + brk = true; + break; + } + } + if brk { break; } + l = l + 1; + } + if l < 0 { + return "".to_string(); + } + strs[0][0..(l+1) as usize].to_string() + } +} + +struct Solution {} +fn main() { + println!("{}", Solution::longest_common_prefix(vec![ + "fliower".to_string(), + "fliow".to_string(), + "fliight".to_string()])); +}
\ No newline at end of file |
