1331. 数组序号转换
This commit is contained in:
parent
b88bfb137b
commit
8cc7ce864c
35
src/main.rs
35
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<i32>) -> Vec<i32> {
|
||||
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]));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue