1331. 数组序号转换

This commit is contained in:
li-chx 2026-07-12 19:25:21 +08:00
parent b88bfb137b
commit 8cc7ce864c
No known key found for this signature in database
GPG Key ID: 9EC03532D8AB7639
1 changed files with 21 additions and 14 deletions

View File

@ -1,25 +1,32 @@
use std::collections::HashMap;
mod arr; mod arr;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
pub fn number_of_special_chars(word: String) -> i32 { pub fn array_rank_transform(mut arr: Vec<i32>) -> Vec<i32> {
let mut arr = vec![0;26]; if arr.len() == 0 {
for ch in word.chars() { return arr;
if ch as u8 >= 97 {
arr[ch as usize - 97] |= 1;
}else {
arr[ch as usize - 65] |= 2;
}
} }
let mut ans = 0; let mut sorted_arr = arr.clone();
for t in arr { sorted_arr.sort_unstable();
if t == 3 { let mut mp = HashMap::with_capacity(arr.len());
ans += 1; 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() { fn main() {
println!("{:?}", Solution::number_of_special_chars(String::from("aaAbcBC"))); println!("{:?}", Solution::array_rank_transform(vec![1,5,2,3,4,5]));
} }