1331. 数组序号转换
This commit is contained in:
parent
b88bfb137b
commit
8cc7ce864c
33
src/main.rs
33
src/main.rs
|
|
@ -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 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;
|
||||||
}
|
}
|
||||||
let mut ans = 0;
|
mp.insert(sorted_arr[i], i as i32 - sub);
|
||||||
for t in arr {
|
|
||||||
if t == 3 {
|
|
||||||
ans += 1;
|
|
||||||
}
|
}
|
||||||
|
println!("{:?}", mp);
|
||||||
|
for x in arr.iter_mut() {
|
||||||
|
*x = *mp.get(x).unwrap() + 1;
|
||||||
}
|
}
|
||||||
ans
|
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]));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue