当前位置:网站首页>【Rust—LeetCode题解】1408.数组中的字符串匹配
【Rust—LeetCode题解】1408.数组中的字符串匹配
2022-08-08 13:22:00 【点灯大师~】
前言
等级: *
解答:
题目描述
给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。
如果你可以删除 words[j] 最左侧和/或最右侧的若干字符得到 word[i] ,那么字符串 words[i] 就是 words[j] 的一个子字符串。
示例:
示例 1:
输入:words = [“mass”,“as”,“hero”,“superhero”]
输出:[“as”,“hero”]
解释:“as” 是 “mass” 的子字符串,“hero” 是 “superhero” 的子字符串。 [“hero”,“as”] 也是有效的答案。
示例 2:
输入:words = [“leetcode”,“et”,“code”]
输出:[“et”,“code”]
解释:“et” 和 “code” 都是 “leetcode” 的子字符串。
示例 3:
输入:words = [“blue”,“green”,“bu”]
输出:[]
解题思路
- 冒泡排序的思想,左右判断是否满足要求
代码
impl Solution {
pub fn string_matching(words: Vec<String>) -> Vec<String> {
use std::collections::HashSet;
let mut ans = HashSet::new();
for (i, w) in words.iter().enumerate() {
for j in words.iter().skip(i + 1) {
if w.contains(j) {
ans.insert(j.clone());
} else if j.contains(w) {
ans.insert(w.clone());
}
}
}
ans.into_iter().collect::<Vec<String>>()
}
}
执行结果
边栏推荐
- 2022-08-03
- 如何对用户输入进行校验
- 南非 KMP 媒体集团实施了 DMS(文档管理系统)使流程数字化,员工可以再次专注于他们的实际任务,提供了效率
- The programmer essential VS debugging technique
- 家电行业趋势:2022从三方面把握家电产品升级方向
- “自降估值”3个亿的咖啡独角兽要IPO了
- C language small project - complete code of minesweeper game (recursive expansion + selection mark)
- (7) FlinkSQL kafka data written to the mysql way 2
- Kotlin系列之let、with、run、apply、also函数的使用
- [C language] Detailed explanation of custom types: structure, enumeration, union
猜你喜欢
Harvard University smashes the field: DALL-E 2 is just a "glue monster", and the generation accuracy rate is only 22%
Doris学习笔记之优化
移位运算、位运算、逻辑运算相关知识点及笔试题
MySQL的索引和事务
深析C语言的灵魂 -- 指针
【C语言】自定义类型详解:结构体、枚举、联合
HackTheBox | Previse
textarea 禁止拖拽
Implement a customized pin code input control
In-depth analysis of the soul of C language -- pointer
随机推荐
Fluorite, millet against smart camera
6.【opencv鼠标回调事件】
使用shardingjdbc实现读写分离配置
(5) FlinkSQL writes socket data to mysql Method 2
家电行业趋势:2022从三方面把握家电产品升级方向
【黑马早报】巴菲特罕见巨亏近3000亿;周鸿祎回应360不能卸载;三亚倡议酒店不变相提高房价;首个国产抗新冠口服药定价不超300元...
Flink1.15 组件RPC通信过程概览图
将小部分源码设计精髓带入到开发中来(工厂模式、适配器模式、抽象类、监听器)
[C language] file related operations
迁移学习(Transfer Learning)的背景、历史及学习课
Background, History and Lessons of Transfer Learning
SSTI漏洞介绍认识(flask、Werkzeup)
qsort 函数的使用及其模拟实现
TS+Hooks二次封装antd Modal,实现可拖拽
textarea 禁止拖拽
【Redis】位图以及位图的使用场景(统计在线人数和用户在线状态)
C语言的三个经典题目:三步翻转法、杨氏矩阵、辗转相除法
In-depth analysis of the soul of C language -- pointer
一桩事先张扬的网红书店倒闭案
Implementation of FIR filter based on FPGA (1) - using fir1 function design