3043. 最长公共前缀的长度
This commit is contained in:
parent
44ce8b9fd4
commit
25645920ff
40
src/main.rs
40
src/main.rs
|
|
@ -1,30 +1,38 @@
|
||||||
mod arr;
|
mod arr;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn get_common(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
|
pub fn longest_common_prefix(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
|
||||||
let (mut a, mut b) = (0,0);
|
let mut map = HashSet::new();
|
||||||
loop {
|
for &x in &arr1 {
|
||||||
if nums1[a] == nums2[b] {
|
let mut x = x;
|
||||||
return nums1[a];
|
while x > 0 {
|
||||||
|
map.insert(x);
|
||||||
|
x /= 10;
|
||||||
}
|
}
|
||||||
if nums1[a] < nums2[b] {
|
}
|
||||||
a += 1;
|
let get_length = |x : i32| -> i32 {
|
||||||
if a >= nums1.len() {
|
x.to_string().len() as i32
|
||||||
|
};
|
||||||
|
let mut max_length = 0;
|
||||||
|
for &x in &arr2 {
|
||||||
|
let mut x = x;
|
||||||
|
while x > 0 {
|
||||||
|
if map.contains(&x) {
|
||||||
|
max_length = max_length.max(get_length(x));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
x /= 10;
|
||||||
b += 1;
|
|
||||||
if b >= nums2.len() {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
max_length
|
||||||
-1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{:?}", Solution::get_common(vec![1,2,3,6], vec![2,3,4,5]));
|
println!(
|
||||||
|
"{:?}",
|
||||||
|
Solution::longest_common_prefix(vec![1, 10, 100], vec![1000])
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue