2615. 等值距离和
This commit is contained in:
parent
6074be16ce
commit
4ee2acd245
44
src/main.rs
44
src/main.rs
|
|
@ -1,31 +1,25 @@
|
||||||
use crate::arr::make_string_arr;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn diff_count_less_two(a: &str, b: &str) -> bool {
|
pub fn distance(nums: Vec<i32>) -> Vec<i64> {
|
||||||
let a = a.as_bytes();
|
let mut ans = vec![0_i64; nums.len()];
|
||||||
let b = b.as_bytes();
|
let mut map: HashMap<i32, Vec<usize>> = HashMap::new();
|
||||||
let mut cnt = 0;
|
for (i, num) in nums.iter().enumerate() {
|
||||||
for i in 0..a.len() {
|
map.entry(*num).or_insert(vec![]).push(i);
|
||||||
if a[i] != b[i] {
|
|
||||||
cnt += 1;
|
|
||||||
if cnt > 2 {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
for (_, v) in map.into_iter() {
|
||||||
|
let mut local_len = 0;
|
||||||
|
for i in 1..v.len() {
|
||||||
|
local_len += v[i] - v[0];
|
||||||
}
|
}
|
||||||
}
|
ans[v[0]] = local_len as i64;
|
||||||
true
|
for i in 1..v.len() {
|
||||||
}
|
local_len =
|
||||||
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
|
local_len - (v[i] - v[i - 1]) * (v.len() - 1 - i) + (v[i] - v[i - 1]) * (i - 1);
|
||||||
let mut ans = vec![];
|
ans[v[i]] = local_len as i64;
|
||||||
for query in queries {
|
|
||||||
for dict in dictionary.iter() {
|
|
||||||
if Self::diff_count_less_two(&query, dict) {
|
|
||||||
ans.push(query.clone());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ans
|
ans
|
||||||
|
|
@ -33,11 +27,5 @@ impl Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!("{:?}", Solution::distance(vec![1, 3, 1, 1, 2]));
|
||||||
"{:?}",
|
|
||||||
Solution::two_edit_words(
|
|
||||||
make_string_arr(r#"["word","note","ants","wood"]"#),
|
|
||||||
make_string_arr(r#"["wood","joke","moat"]"#)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue