leetcode1945&1946
两道题都是字符串的修改。
leetcode上对字符串的考察非常多。
1945
impl Solution {
pub fn get_lucky(s: String, k: i32) -> i32 {
let mut t = String::new();
let mut i = k - 1;
for c in s.chars() {
let num = c as i32 - 'a' as i32 + 1;
t.push_str(&num.to_string());
}
while i > 0 {
let mut sum = 0;
for c in t.chars() {
sum += c as i32 - '0' as i32;
}
t = format!("{}", sum);
i = i - 1;
}
let mut res = 0;
for c in t.chars() {
res += c as i32 - '0' as i32;
}
res
}
}
1946
impl Solution {
pub fn maximum_number(num: String, change: Vec<i32>) -> String {
let mut res = String::new();
let mut begin = 0;
for c in num.chars() {
let x = c as i32 - '0' as i32;
let y = change[x as usize];
match begin {
0 => {
if y > x {
begin = 1;
res.push((change[x as usize] as u8 + '0' as u8) as char);
} else {
res.push((x as u8 + '0' as u8) as char);
}
}
1 => {
if change[x as usize] >= x {
res.push((change[x as usize] as u8 + '0' as u8) as char)
} else {
begin = 2;
res.push((x as u8 + '0' as u8) as char);
}
}
_ => {
res.push((x as u8 + '0' as u8) as char);
}
}
}
res
}
}
命令行工具
std::env::args().nth(1).unwrap()
可以获取从命令行来的参数,参数从1开始std::fs::write(path, contents)
可以写文件for item in [a, b, c, d]
items.join("+")
用+号把所有项连接起来。sys.get....