(Golang) leetcode Hot100 第三部分 滑动窗口

//3.无重复字符的最长子串
func lengthOfLongestSubstring(s string) int {
	res := 0
	left := 0
	m := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		if idx, ok := m[s[i]]; ok && idx >= left {
			left = idx + 1
		}
		m[s[i]] = i
		res = max(res,i - left + 1)
	}
    return res
}
//438、找到字符串中所有字母异位词
func findAnagrams(s string, p string) []int {
	slen, plen := len(s), len(p)
	scount, pcount := [26]int{}, [26]int{}
	res := []int{}
	if plen > slen {
		return res
	}
	for i, v := range p {
		scount[s[i]-'a']++
		pcount[v-'a']++
	}
	if scount == pcount {
		res = append(res, 0)
	}
	for i, ch := range s[:slen-plen] {
		scount[ch-'a']--
		scount[s[i+plen]-'a']++
		if scount == pcount {
			res = append(res, i+1)//注意这里从i+1统计
		}
	}
	return res
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值