205.同构字符串(未完成)、345.反转字符串中的元音字母、349.两个数组的交集、500.键盘行、504.七进制数
This commit is contained in:
parent
1cb530d5b1
commit
248ccd5ce4
|
@ -76,6 +76,7 @@ mod q0190;
|
|||
mod q0191;
|
||||
mod q0198;
|
||||
mod q0201;
|
||||
mod q0205;
|
||||
mod q0206;
|
||||
mod q0213;
|
||||
mod q0217;
|
||||
|
@ -102,6 +103,8 @@ mod q0338;
|
|||
mod q0342;
|
||||
mod q0343;
|
||||
mod q0344;
|
||||
mod q0345;
|
||||
mod q0349;
|
||||
mod q0363;
|
||||
mod q0367;
|
||||
mod q0368;
|
||||
|
@ -126,6 +129,8 @@ mod q0461;
|
|||
mod q0477;
|
||||
mod q0486;
|
||||
mod q0495;
|
||||
mod q0500;
|
||||
mod q0504;
|
||||
mod q0509;
|
||||
mod q0547;
|
||||
mod q0554;
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
use crate::Solution;
|
||||
|
||||
impl Solution {
|
||||
/// [205.同构字符串](https://leetcode.cn/problems/isomorphic-strings/)
|
||||
///
|
||||
/// 2022-08-19 14:25:56
|
||||
///
|
||||
/// 给定两个字符串 `s` 和 `t` ,判断它们是否是同构的。
|
||||
///
|
||||
/// 如果 `s` 中的字符可以按某种映射关系替换得到 `t` ,那么这两个字符串是同构的。
|
||||
///
|
||||
/// 每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。
|
||||
///
|
||||
/// + **示例 1:**
|
||||
/// + **输入:** s = `"egg",` t = `"add"`
|
||||
/// + **输出:** true
|
||||
/// + **示例 2:**
|
||||
/// + **输入:** s = `"foo",` t = `"bar"`
|
||||
/// + **输出:** false
|
||||
/// + **示例 3:**
|
||||
/// + **输入:** s = `"paper",` t = `"title"`
|
||||
/// + **输出:** true
|
||||
/// + **提示:**
|
||||
/// * `1 <= s.length <= 5 * 104`
|
||||
/// * `t.length == s.length`
|
||||
/// * `s` 和 `t` 由任意有效的 ASCII 字符组成
|
||||
/// + Related Topics
|
||||
/// * 哈希表
|
||||
/// * 字符串
|
||||
pub fn is_isomorphic(s: String, t: String) -> bool {
|
||||
let mut m1 = std::collections::HashMap::<u8, u8>::new();
|
||||
let mut m2 = std::collections::HashMap::<u8, u8>::new();
|
||||
for (k, v) in s.into_bytes().into_iter().zip(t.into_bytes().into_iter()) {
|
||||
// m2.entry(v).or_insert(k)
|
||||
if v != *m1.entry(k).or_insert(v) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_q0205() {
|
||||
assert_eq!(Solution::is_isomorphic("egg".to_owned(), "add".to_owned()), true);
|
||||
assert_eq!(Solution::is_isomorphic("foo".to_owned(), "bar".to_owned()), false);
|
||||
assert_eq!(Solution::is_isomorphic("paper".to_owned(), "title".to_owned()), true);
|
||||
assert_eq!(Solution::is_isomorphic("badc".to_owned(), "baba".to_owned()), false);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
use crate::Solution;
|
||||
|
||||
impl Solution {
|
||||
/// [345.反转字符串中的元音字母](https://leetcode.cn/problems/reverse-vowels-of-a-string/)
|
||||
///
|
||||
/// 2022-08-19 13:19:18
|
||||
///
|
||||
/// 给你一个字符串 `s` ,仅反转字符串中的所有元音字母,并返回结果字符串。
|
||||
///
|
||||
/// 元音字母包括 `'a'`、`'e'`、`'i'`、`'o'`、`'u'`,且可能以大小写两种形式出现。
|
||||
///
|
||||
/// + **示例 1:**
|
||||
/// + **输入:** s = "hello"
|
||||
/// + **输出:** "holle"
|
||||
/// + **示例 2:**
|
||||
/// + **输入:** s = "leetcode"
|
||||
/// + **输出:** "leotcede"
|
||||
/// + **提示:**
|
||||
/// * `1 <= s.length <= 3 * 105`
|
||||
/// * `s` 由 **可打印的 ASCII** 字符组成
|
||||
/// + Related Topics
|
||||
/// * 双指针
|
||||
/// * 字符串
|
||||
pub fn reverse_vowels(s: String) -> String {
|
||||
let mut b = s.into_bytes();
|
||||
let (mut i, mut j) = (0usize, b.len() - 1);
|
||||
while i < j {
|
||||
match b[i] {
|
||||
b'a' | b'e' | b'i' | b'o' | b'u' | b'A' | b'E' | b'I' | b'O' | b'U' => {
|
||||
while i < j && !matches!(b[j], b'a' | b'e' | b'i' | b'o' | b'u' | b'A' | b'E' | b'I' | b'O' | b'U') {
|
||||
j -= 1;
|
||||
}
|
||||
if i < j {
|
||||
b.swap(i, j);
|
||||
j -= 1;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
_ => i += 1
|
||||
}
|
||||
}
|
||||
String::from_utf8(b).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_q0345() {
|
||||
assert_eq!(Solution::reverse_vowels("hello".to_owned()), "holle".to_owned());
|
||||
assert_eq!(Solution::reverse_vowels("leetcode".to_owned()), "leotcede".to_owned());
|
||||
assert_eq!(Solution::reverse_vowels("Aa".to_owned()), "aA".to_owned());
|
||||
assert_eq!(Solution::reverse_vowels("race car".to_owned()), "race car".to_owned());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
use crate::Solution;
|
||||
|
||||
impl Solution {
|
||||
/// [349.两个数组的交集](https://leetcode.cn/problems/intersection-of-two-arrays/)
|
||||
///
|
||||
/// 2022-08-19 12:55:08
|
||||
///
|
||||
/// 给定两个数组 `nums1` 和 `nums2` ,返回 _它们的交集_ 。输出结果中的每个元素一定是 **唯一** 的。我们可以 **不考虑输出结果的顺序** 。
|
||||
///
|
||||
/// + **示例 1:**
|
||||
/// + **输入:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
|
||||
/// + **输出:** \[2\]
|
||||
/// + **示例 2:**
|
||||
/// + **输入:** nums1 = \[4,9,5\], nums2 = \[9,4,9,8,4\]
|
||||
/// + **输出:** \[9,4\]
|
||||
/// + **解释:** \[4,9\] 也是可通过的
|
||||
/// + **提示:**
|
||||
/// * `1 <= nums1.length, nums2.length <= 1000`
|
||||
/// * `0 <= nums1[i], nums2[i] <= 1000`
|
||||
/// + Related Topics
|
||||
/// * 数组
|
||||
/// * 哈希表
|
||||
/// * 双指针
|
||||
/// * 二分查找
|
||||
/// * 排序
|
||||
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
|
||||
let mut s1 = nums1.into_iter().collect::<std::collections::hash_set::HashSet<i32>>();
|
||||
let mut s2 = nums2.into_iter().collect::<std::collections::hash_set::HashSet<i32>>();
|
||||
if s1.len() > s2.len() {
|
||||
std::mem::swap(&mut s1, &mut s2);
|
||||
}
|
||||
s1.into_iter().filter(|v| s2.contains(v)).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_q0349() {
|
||||
assert_eq!(Solution::intersection(vec![1, 2, 2, 1], vec![2, 2]), vec![2]);
|
||||
assert_eq!(Solution::intersection(vec![4, 9, 5], vec![9, 4, 9, 8, 4]), vec![4, 9]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
use crate::Solution;
|
||||
|
||||
impl Solution {
|
||||
/// [500.键盘行](https://leetcode.cn/problems/keyboard-row/)
|
||||
///
|
||||
/// 2022-08-19 13:50:31
|
||||
///
|
||||
/// 给你一个字符串数组 `words` ,只返回可以使用在 **美式键盘** 同一行的字母打印出来的单词。键盘如下图所示。
|
||||
///
|
||||
/// + **美式键盘** 中:
|
||||
///
|
||||
/// * 第一行由字符 `"qwertyuiop"` 组成。
|
||||
/// * 第二行由字符 `"asdfghjkl"` 组成。
|
||||
/// * 第三行由字符 `"zxcvbnm"` 组成。
|
||||
///
|
||||
/// ![American keyboard](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/keyboard.png)
|
||||
///
|
||||
/// + **示例 1:**
|
||||
/// + **输入:** words = \["Hello","Alaska","Dad","Peace"\]
|
||||
/// + **输出:** \["Alaska","Dad"\]
|
||||
/// + **示例 2:**
|
||||
/// + **输入:** words = \["omk"\]
|
||||
/// + **输出:** \[\]
|
||||
/// + **示例 3:**
|
||||
/// + **输入:** words = \["adsdf","sfd"\]
|
||||
/// + **输出:** \["adsdf","sfd"\]
|
||||
/// + **提示:**
|
||||
/// * `1 <= words.length <= 20`
|
||||
/// * `1 <= words[i].length <= 100`
|
||||
/// * `words[i]` 由英文字母(小写和大写字母)组成
|
||||
/// + Related Topics
|
||||
/// * 数组
|
||||
/// * 哈希表
|
||||
/// * 字符串
|
||||
///
|
||||
/// ```
|
||||
/// fn build_lookup() {
|
||||
/// let row1 = "qwertyuiop".to_owned();
|
||||
/// let row2 = "asdfghjkl".to_owned();
|
||||
/// let mut lookup = vec![];
|
||||
/// for c in 'a'..='z' {
|
||||
/// if row1.find(c).is_some() {
|
||||
/// lookup.push(1);
|
||||
/// } else if row2.find(c).is_some() {
|
||||
/// lookup.push(2);
|
||||
/// } else {
|
||||
/// lookup.push(3);
|
||||
/// }
|
||||
/// }
|
||||
/// println!("{:?}", lookup);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn find_words(words: Vec<String>) -> Vec<String> {
|
||||
const ROW_LOOKUP: [i32; 26] = [2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, 3, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3];
|
||||
let mut result = vec![];
|
||||
for word in words {
|
||||
let bytes = word.as_bytes();
|
||||
let mut flag = true;
|
||||
let row = ROW_LOOKUP[(bytes[0].to_ascii_lowercase() - b'a') as usize];
|
||||
for i in 1..bytes.len() {
|
||||
if row != ROW_LOOKUP[(bytes[i].to_ascii_lowercase() - b'a') as usize] {
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
result.push(word);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_q0500() {
|
||||
assert_eq!(Solution::find_words(vec!["Hello".to_owned(), "Alaska".to_owned(), "Dad".to_owned(), "Peace".to_owned()]), vec!["Alaska".to_owned(), "Dad".to_owned()]);
|
||||
assert_eq!(Solution::find_words(vec!["omk".to_owned()]), Vec::<String>::new());
|
||||
assert_eq!(Solution::find_words(vec!["adsdf".to_owned(), "sfd".to_owned()]), vec!["adsdf".to_owned(), "sfd".to_owned()]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
use crate::Solution;
|
||||
|
||||
impl Solution {
|
||||
/// [504.七进制数](https://leetcode.cn/problems/base-7/)
|
||||
///
|
||||
/// 2022-08-19 13:38:50
|
||||
///
|
||||
/// 给定一个整数 `num`,将其转化为 **7 进制**,并以字符串形式输出。
|
||||
///
|
||||
/// + **示例 1:**
|
||||
/// + **输入:** num = 100
|
||||
/// + **输出:** "202"
|
||||
/// + **示例 2:**
|
||||
/// + **输入:** num = -7
|
||||
/// + **输出:** "-10"
|
||||
/// + **提示:**
|
||||
/// * `-107 <= num <= 107`
|
||||
/// + Related Topics
|
||||
/// * 数学
|
||||
pub fn convert_to_base7(num: i32) -> String {
|
||||
if num == 0 {
|
||||
return "0".to_owned();
|
||||
}
|
||||
let mut v = Vec::<u8>::new();
|
||||
let b = num.is_negative();
|
||||
let mut num = num.abs();
|
||||
while num != 0 {
|
||||
v.push((num % 7) as u8 + b'0');
|
||||
num /= 7;
|
||||
}
|
||||
if b {
|
||||
v.push(b'-');
|
||||
}
|
||||
v.reverse();
|
||||
String::from_utf8(v).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_q0504() {
|
||||
assert_eq!(Solution::convert_to_base7(100), "202".to_owned());
|
||||
assert_eq!(Solution::convert_to_base7(-7), "-10");
|
||||
assert_eq!(Solution::convert_to_base7(0), "0");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue