From 8cc7ce864c8ab29c8fc0de5de73f4c20a84cb329 Mon Sep 17 00:00:00 2001 From: li-chx Date: Sun, 12 Jul 2026 19:25:21 +0800 Subject: [PATCH] =?UTF-8?q?1331.=20=E6=95=B0=E7=BB=84=E5=BA=8F=E5=8F=B7?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index e257c5f..4c04497 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,32 @@ +use std::collections::HashMap; + mod arr; struct Solution {} impl Solution { - pub fn number_of_special_chars(word: String) -> i32 { - let mut arr = vec![0;26]; - for ch in word.chars() { - if ch as u8 >= 97 { - arr[ch as usize - 97] |= 1; - }else { - arr[ch as usize - 65] |= 2; - } + pub fn array_rank_transform(mut arr: Vec) -> Vec { + if arr.len() == 0 { + return arr; } - let mut ans = 0; - for t in arr { - if t == 3 { - ans += 1; + let mut sorted_arr = arr.clone(); + sorted_arr.sort_unstable(); + let mut mp = HashMap::with_capacity(arr.len()); + let mut sub = 0; + mp.insert(sorted_arr[0], 0); + for i in 1..sorted_arr.len() { + if sorted_arr[i] == sorted_arr[i-1] { + sub+=1; + continue; } + mp.insert(sorted_arr[i], i as i32 - sub); } - ans + println!("{:?}", mp); + for x in arr.iter_mut() { + *x = *mp.get(x).unwrap() + 1; + } + arr } } fn main() { - println!("{:?}", Solution::number_of_special_chars(String::from("aaAbcBC"))); + println!("{:?}", Solution::array_rank_transform(vec![1,5,2,3,4,5])); }