Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5499. 重复至少 K 次且长度为 M 的模式 (204周赛题) #54

Open
yankewei opened this issue Aug 30, 2020 · 1 comment
Open

5499. 重复至少 K 次且长度为 M 的模式 (204周赛题) #54

yankewei opened this issue Aug 30, 2020 · 1 comment
Labels
数组 题目类型为数组 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

给你一个正整数数组 arr,请你找出一个长度为 m 且在数组中至少重复 k 次的模式。

模式 是由一个或多个值组成的子数组(连续的子序列),连续 重复多次但 不重叠 。 模式由其长度和重复次数定义。

如果数组中存在至少重复 k 次且长度为 m 的模式,则返回 true ,否则返回  false 。

示例 1:

输入:arr = [1,2,4,4,4,4], m = 1, k = 3
输出:true
解释:模式 (4) 的长度为 1 ,且连续重复 4 次。注意,模式可以重复 k 次或更多次,但不能少于 k 次。

示例 2:

输入:arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
输出:true
解释:模式 (1,2) 长度为 2 ,且连续重复 2 次。另一个符合题意的模式是 (2,1) ,同样重复 2 次。

示例 3:

输入:arr = [1,2,1,2,1,3], m = 2, k = 3
输出:false
解释:模式 (1,2) 长度为 2 ,但是只连续重复 2 次。不存在长度为 2 且至少重复 3 次的模式。

示例 4:

输入:arr = [1,2,3,1,2], m = 2, k = 2
输出:false
解释:模式 (1,2) 出现 2 次但并不连续,所以不能算作连续重复 2 次。

示例 5:

输入:arr = [2,2,2,2], m = 2, k = 3
输出:false
解释:长度为 2 的模式只有 (2,2) ,但是只连续重复 2 次。注意,不能计算重叠的重复次数。

提示:

2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
@yankewei yankewei changed the title 5499. 重复至少 K 次且长度为 M 的模式 5499. 重复至少 K 次且长度为 M 的模式 (204周赛题) Aug 30, 2020
@yankewei yankewei added 数组 题目类型为数组 简单 题目难度为简单 labels Aug 30, 2020
@yankewei
Copy link
Owner Author

做的第一道周赛题

暴力法

根据题目的条件,长度M已经给力,所以我们可以得到数组中的所有长度为M的子数组subArray,然后这个subArray重复K次,得到repeatArr,如果repeatArr也是数组的子数组,表示模式匹配。
但是一个数组是否是另一个数组的子数组,不太好判断,可以转换成string,就比较好做了。

func containsPattern(arr []int, m int, k int) bool {
    var str string
    for _, v := range arr {
	str += strconv.Itoa(v)
    }
    for i := 0; i+m < len(arr); i++ {
	subStr := str[i : i+m]
	var indexStr string
	for j := 0; j < k; j++ {
	    indexStr += subStr
	}
	if strings.Index(str, indexStr) != -1 {
	    return true
	}
    }
    return false
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
数组 题目类型为数组 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant