58. 最后一个单词的长度 Length of Last Word
给你一个字符串 s
,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
示例 1:
输入:s = "Hello World"
输出:5
解释:最后一个单词是“World”,长度为5。
示例 2:
输入:s = " fly me to the moon "
输出:4
解释:最后一个单词是“moon”,长度为4。
示例 3:
输入:s = "luffy is still joyboy"
输出:6
解释:最后一个单词是长度为6的“joyboy”。
提示:
1 <= s.length <= 10^4
s
仅有英文字母和空格' '
组成s
中至少存在一个单词
代码1:
将字符串按照空格分割成多个子字符串,然后取最末的字符串长度即可。
fn length_of_last_word(s: String) -> i32 { let strs: Vec<&str> = s.trim().split(' ').collect(); for i in (0..strs.len()).rev() { if !strs[i].is_empty() { return strs[i].len() as i32; } } return 0; } fn main() { let s = "Hello World".to_string(); println!("{:?}", length_of_last_word(s)); let s = " fly me to the moon ".to_string(); println!("{:?}", length_of_last_word(s)); let s = "luffy is still joyboy".to_string(); println!("{:?}", length_of_last_word(s)); }
代码2:
反向遍历:从字符串末尾开始向前遍历,找到第一个不为空格的字符,然后再向前遍历,直到遇到空格或到达字符串开头为止,这段子字符串的长度即为最后一个单词的长度。
fn length_of_last_word(s: String) -> i32 { let bytes = s.into_bytes(); let n = bytes.len(); let mut i = n as isize - 1; while i >= 0 && bytes[i as usize] == b' ' { i -= 1; } if i < 0 { return 0; } let mut j = i; while j >= 0 && bytes[j as usize] != b' ' { j -= 1; } return (i - j) as i32; } fn main() { let s = "Hello World".to_string(); println!("{:?}", length_of_last_word(s)); let s = " fly me to the moon ".to_string(); println!("{:?}", length_of_last_word(s)); let s = "luffy is still joyboy".to_string(); println!("{:?}", length_of_last_word(s)); }
输出:
5
4
6
59. 螺旋矩阵 II Spiral Matrix II
给你一个正整数 n
,生成一个包含 1
到 n2
所有元素,且元素按顺时针顺序螺旋排列的 n x n
正方形矩阵 matrix
。
示例 1:
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1
输出:[[1]]
提示:
1 <= n <= 20
代码1:
按层模拟 按照从外到内的顺序,依次填入矩阵中的每一个元素。
fn generate_matrix(n: i32) -> Vec<Vec<i32>> { let mut matrix = vec![vec![0; n as usize]; n as usize]; let (mut num, mut left, mut right, mut top, mut bottom) = (1, 0, n - 1, 0, n - 1); while left <= right && top <= bottom { for i in left..=right { matrix[top as usize][i as usize] = num; num += 1; } for i in top + 1..=bottom { matrix[i as usize][right as usize] = num; num += 1; } if left < right && top < bottom { for i in (left..right).rev() { matrix[bottom as usize][i as usize] = num; num += 1; } for i in (top + 1..bottom).rev() { matrix[i as usize][left as usize] = num; num += 1; } } left += 1; right -= 1; top += 1; bottom -= 1; } matrix } fn main() { let n = 3; println!("{:?}", generate_matrix(n)); println!("{:?}", generate_matrix(1)); }
代码2:
模拟转圈 用四个变量分别记录当前填数位置所在的行列范围,然后按照“向右、向下、向左、向上”的顺序不断填入数值,每填入一个数就更新当前位置和范围。
fn generate_matrix(n: i32) -> Vec<Vec<i32>> { let mut matrix = vec![vec![0; n as usize]; n as usize]; let mut num = 1; let mut row = 0; let mut col = 0; let mut direction = 0; let directions = vec![vec![0, 1], vec![1, 0], vec![0, -1], vec![-1, 0]]; while num <= n * n { matrix[row as usize][col as usize] = num; num += 1; let next_row = row + directions[direction][0]; let next_col = col + directions[direction][1]; if next_row < 0 || next_row >= n || next_col < 0 || next_col >= n || matrix[next_row as usize][next_col as usize] != 0 { direction = (direction + 1) % 4; } row += directions[direction][0]; col += directions[direction][1]; } matrix } fn main() { let n = 3; println!("{:?}", generate_matrix(n)); println!("{:?}", generate_matrix(1)); }
输出:
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
[[1]]
60. 排列序列 Permutation Sequence
给出集合 [1,2,3,...,n]
,其所有元素共有 n!
种排列。
按大小顺序列出所有排列情况,并一一标记,当 n = 3
时, 所有排列如下:
"123" "132" "213" "231" "312" "321"
给定 n
和 k
,返回第 k
个排列。
示例 1:
输入:n = 3, k = 3
输出:"213"
示例 2:
输入:n = 4, k = 9
输出:"2314"
示例 3:
输入:n = 3, k = 1
输出:"123"
提示:
1 <= n <= 9
1 <= k <= n!
代码: 回溯法
use std::convert::TryInto; fn factorial(n: usize) -> usize { (1..=n).product() } fn get_permutation(n: i32, k: i32) -> String { let mut nums: Vec<i32> = (1..=n).collect(); let mut res = String::new(); nums.sort(); let mut k = k - 1; for i in (1..=n).rev() { let f = factorial((i-1).try_into().unwrap()) as i32; let index = (k / f) as usize; res.push_str(&nums[index].to_string()); nums.remove(index); k %= f; } res } fn main() { let n = 3; let k = 3; let s = get_permutation(n, k); println!("{}", s); let n = 4; let k = 9; let s = get_permutation(n, k); println!("{}", s); let n = 3; let k = 1; let s = get_permutation(n, k); println!("{}", s); }
输出:
213
2314
123
🌟 每日一练刷题专栏🌟
✨持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Rust每日一练 专栏 (2023.5.16~)更新中... |
|
Golang每日一练 专栏 (2023.3.11~)更新中... |
|
Python每日一练 专栏 (2023.2.18~2023.5.18)暂停更 |
|
C/C++每日一练 专栏 (2023.2.18~2023.5.18)暂停更 |
|
Java每日一练 专栏 (2023.3.11~2023.5.18)暂停更 |