当前位置:网站首页>【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>>()
}
}
执行结果

边栏推荐
- 第十二届蓝桥杯《杨辉三角》-二分法
- 优刻得“失速”:营收转降,定向增发股东浮亏超三成
- xxd命令(反编译、二进制文件转十六进制文件)
- Knowledge points and written test questions related to shift operations, bit operations, and logical operations
- 哈佛大学砸场子:DALL-E 2只是「粘合怪」,生成正确率只有22%
- 【JS高级】ES5标准规范之严格模式下的保护对象_09
- 【C语言】深度剖析数据在内存中的存储
- KD-SCFNet: More Accurate and Efficient Salient Object Detection Through Knowledge Distillation (ECCV2022)
- The use of string function, character function, memory function and its analog implementation
- webgl 基础
猜你喜欢

HackTheBox | Previse
![[C language] In-depth analysis of data storage in memory](/img/7c/a277657a85cc0e29db8df919439949.png)
[C language] In-depth analysis of data storage in memory

医药行业转型发展,探索数字化供应链升级之道

将小部分源码设计精髓带入到开发中来(工厂模式、适配器模式、抽象类、监听器)
MySQL database storage series (5) the InnoDB storage format

C language small project - complete code of minesweeper game (recursive expansion + selection mark)
![[C language] Detailed explanation of custom types: structure, enumeration, union](/img/5c/69d238a71e24b34c2232c20ed7e09e.png)
[C language] Detailed explanation of custom types: structure, enumeration, union

哈佛大学砸场子:DALL-E 2只是「粘合怪」,生成正确率只有22%

Docker - persistent database (data volume)

Prometheus监控Harbor(二进制版)
随机推荐
简析LDO静态电流与功耗的关系
(7)FlinkSQL将kafka数据写入到mysql方式二
什么是IP SSL证书,如何申请?
连锁小酒馆第一股,海伦司能否梦圆大排档?
R语言ggpubr包的ggsummarystats函数可视化分面箱图(通过ggfunc参数和facet.by参数设置)、添加描述性统计结果表格、palette参数配置不同水平可视化图像和统计值的颜色
Prometheus监控Harbor(二进制版)
(5)FlinkSQL将socket数据写入到mysql方式二
Verilog HDL Bits training 09 grammar foundation
深入浅出对话系统——任务型对话系统技术框架
[C language] file related operations
专访|360高瀚昭:ISC十年,360数字安全大脑能够“看见”什么?
Flink1.15 组件RPC通信过程概览图
6.【opencv鼠标回调事件】
服务器配置——Linux系统安装Redis
R语言基于指定规则、条件删除列表中的元素:使用purrr包的discard函数移除列表数据中的NA值
第十二届蓝桥杯《杨辉三角》-二分法
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、设置可视化图像的主题为theme_gray
移位运算、位运算、逻辑运算相关知识点及笔试题
SSL证书最长有效期13个月,还有必要一次申请多年吗?
Verilog语法基础HDL Bits训练 09