当前位置:网站首页>leetcode-每日一题1408. 数组中的字符串匹配(暴力枚举)和Golang里关于Index方法和Contains方法区别
leetcode-每日一题1408. 数组中的字符串匹配(暴力枚举)和Golang里关于Index方法和Contains方法区别
2022-08-06 16:20:00 【lin钟一】

题目链接:https://leetcode.cn/problems/string-matching-in-an-array/
思路
方法一、暴力枚举
直接想法
题目要求我们找到字符串数组中存在字符串是其他单词的子字符串,看到题目给我们的n的范围是[1,100],所以我们可以通过暴力枚举用两个for循环一层指子串一层指找存在这个子串的单词,找到则找下个一个子串
代码示例
func stringMatching(words []string) (ans []string) {
for i, x := range words{
for j, y := range words{
if i != j && strings.Contains(y, x){
//strings.Index(y, x) >= 0
ans = append(ans, x)
break
}
}
}
return
}
注意:Index方法和Contains方法在这里的速度和效率是一样的,Contains的源码里是直接调用的Index方法,所以二者其实本质上都是Index方法,Index方法的内部其实也是用暴力求解,但是不同的是,他有偷懒且灵巧机制,不是纯暴力求,有兴趣的可以去Index的源码看看

复杂度分析
- 时间复杂度:O(n2),其中n表示字符串数组的长度,需要用到两个for循环
- 空间复杂度:O(1),不需要额外申请空间
方法二、暴力枚举
直接思想
这个方法是从leetcode其他ac人看到的,本质上也是暴力枚举,但是不同的是,先把字符串数组按照长度从长到短排序,然后只需要去找后面比自己短的子串是不是包含在自己当中即可
代码示例
func stringMatching(words []string) []string {
sort.Slice(words, func(i, j int) bool {
return len(words[i]) < len(words[j])
})
n := len(words)
var res []string
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if strings.Index(words[j], words[i]) >= 0 {
res = append(res, words[i])
break
}
}
}
return res
}

复杂度分析
- 时间复杂度:O(n logn + n2),其中n表示字符串数组的长度,对字符串数组排序需要O(n logn)的时间,查找子串需要O(n2)的时间
- 空间复杂度:O(1),不需要额外申请空间
边栏推荐
猜你喜欢

Coggle 30 Days of ML【打卡】广告-信息流跨域ctr预估

LeetCode·每日一题·1408.数组中的字符串匹配·模拟

5G NR SIB1介绍

阿里架构师力推jvm架构解析文档,把JVM调优实战讲的明明白白

什么是数据库

13. Implementation of paging loading datasets for SAP ABAP OData service (Paging)

Detailed explanation of IK tokenizer

Intel transformation was not successful and into loss, AMD grow again and get substantial profits

LeetCode:206. 反转链表————简单

d2-admin基本使用
随机推荐
Unity editor extension - top menu bar extension
内网穿透工具 frp 使用教程
ASEMI整流桥GBL610参数,GBL610尺寸,GBL610特征
【瑞吉外卖】day02:后台系统登录、退出功能
cmd命令行工具
网络设备批量操作
LeetCode SQL专项练习(4)组合查询 & 指定选取
##mysql数据库的安装部署
瑞吉外卖项目实战Day05
Samsung's Vietnam factory is operating at less than half, regretting closing production lines in China
获取 table 距离窗口上方的高度(有深度的文章)
终于有人把IaaS、PaaS、SaaS讲明白了
【GO】go mod 和vendor依赖管理工具
【深度学习】华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)...
[C Supplement] Precautions for two-dimensional arrays as formal parameters
-迷宫出口-
06、GO异常处理
GBL210-ASEMI机箱电源适配器整流桥GBL210
阿里架构师力推jvm架构解析文档,把JVM调优实战讲的明明白白
【C补充】int *a[1], int (*a)[10], int (*a)(int) 等的区别举例