模拟竞赛,总时间 47:57,3WA1,4WA1,排名 304/6811,第一名总时间 09:05。
tips:
- 二分边界/答案边界需要多考虑一下,懒得考虑就拿特别大的值,last_success 设置对就行。
- 调用 unwrap 要当心,option 真的能调用 unwrap 吗?
用处不大的 tips:
3. 定长定格式字符串处理,可以自己快速写处理函数。
4. win/lose 这道题,可以不用注册 win,把 lose 值注册一下就可以,+=0.
2224 转化时间需要的最少操作数
impl Solution {
pub fn convert_time(current: String, correct: String) -> i32 {
let (h0, m0) = current.split_once(":").unwrap();
let (h0, m0) = (h0.parse::<i32>().unwrap(), m0.parse::<i32>().unwrap());
let (h1, m1) = correct.split_once(":").unwrap();
let (h1, m1) = (h1.parse::<i32>().unwrap(), m1.parse::<i32>().unwrap());
let mut need = (h1-h0)*60 + (m1-m0);
let mut ans = 0;
for unit in [60, 15, 5, 1] {
ans += need / unit;
need %= unit;
}
ans
}
}
2225 找出输掉零场或一场比赛的玩家
impl Solution {
pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
use std::collections::BTreeMap;
let mut win_cnt = BTreeMap::new();
let mut lose_cnt = BTreeMap::new();
for mat in matches {
let (win, lose) = (mat[0], mat[1]);
win_cnt.entry(win).and_modify(|e| *e += 1).or_insert(1);
lose_cnt.entry(lose).and_modify(|e| *e += 1).or_insert(1);
}
let mut ans = vec![vec![], vec![]];
for (win, _) in win_cnt {
if lose_cnt.get(&win).is_none() {
ans[0].push(win);
}
}
for (lose, cnt) in lose_cnt {
if cnt == 1 {
ans[1].push(lose);
}
}
ans
}
}
2226 每个小孩最多能分到多少糖果
impl Solution {
pub fn maximum_candies(candies: Vec<i32>, k: i64) -> i32 {
let mut left = 1;
let mut right = *candies.iter().max().unwrap();
let mut last_success = 0;
while left <=right {
let candidate = (left + right)/2;
let mut cnt = 0;
for candy in &candies {
cnt += (candy/candidate) as i64;
}
if cnt >= k {
last_success = candidate;
left = candidate + 1;
} else {
right = candidate - 1;
}
}
last_success
}
}
2227 加密解密字符串
struct Encrypter {
keys: Vec<char>,
values: Vec<String>,
dictionary: Vec<String>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Encrypter {
fn new(keys: Vec<char>, values: Vec<String>, dictionary: Vec<String>) -> Self {
Encrypter { keys, values, dictionary }
}
fn encrypt(&self, word1: String) -> String {
let mut result = String::new();
for c in word1.chars() {
// println!("{} {}", c, c as i32);
let index = self.keys.iter().position(|&x| x == c);
if index.is_none() {
return "".to_string();
}
result.push_str(&self.values[index.unwrap()]);
}
result
}
fn decrypt(&self, word2: String) -> i32 {
let mut res = 0;
for dic in &self.dictionary {
if self.encrypt(dic.clone()) == word2 {
res += 1;
}
}
res
}
}