summaryrefslogtreecommitdiff
path: root/0014
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-09-06 23:17:22 +0800
committerMistivia <i@mistivia.com>2025-09-06 23:17:41 +0800
commitad95cba8220e2a7c86362caeb76e1a4333e9c2b8 (patch)
tree30b31d94e2ceb46d4e946bfa1eb88b956508f760 /0014
parent5dd8dcdc2ccfa89d25a3cb342a2f89c644236971 (diff)
refactor
Diffstat (limited to '0014')
-rw-r--r--0014/Cargo.lock7
-rw-r--r--0014/Cargo.toml6
-rw-r--r--0014/src/main.rs30
3 files changed, 43 insertions, 0 deletions
diff --git a/0014/Cargo.lock b/0014/Cargo.lock
new file mode 100644
index 0000000..83c9b24
--- /dev/null
+++ b/0014/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "p14"
+version = "0.1.0"
diff --git a/0014/Cargo.toml b/0014/Cargo.toml
new file mode 100644
index 0000000..23404ea
--- /dev/null
+++ b/0014/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "p14"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/0014/src/main.rs b/0014/src/main.rs
new file mode 100644
index 0000000..e85ef8f
--- /dev/null
+++ b/0014/src/main.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