Algorithms
438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Example 1:
Input:
s: "cbaebabacd" p: "abc"
Output:
[0, 6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input:
s: "abab" p: "ab"
Output:
[0, 1, 2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
Solution 1:leetcode上面的思路——通过滑动窗口来确定子字符串的长度
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
Map<Character, Integer> map = new HashMap<>();
for (Character c : p.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int counter = map.size();
int begin = 0, end = 0;
while (end < s.length()) {
char c = s.charAt(end);
if (map.containsKey(c)) {
map.put(c, map.get(c) - 1);
if (map.get(c) == 0) {
counter--;
}
}
end++;
while (counter == 0) {
char c1 = s.charAt(begin);
if (map.containsKey(c1)) {
map.put(c1, map.get(c1) + 1);
if (map.get(c1) > 0) {
counter++;
}
}
if (end - begin == p.length()) {
list.add(begin);
}
begin++;
}
}
return list;
}
}
Review
Meetings:Where Work Goes to Die
1、会议不能超过一个小时。
2、每个会议必须有一个简洁明了的目的陈述,明确会议的意义。
3、开会之前每个人必须做好自己的准备工作。
4、让会议对每个参会人员来说是有意义的,要不就没必要参加会议。
5、会议结束的时候每个人都必须总结要做的任务列表。
Tips
网络协议基础知识:传输层—TCP
1、所谓的面向连接,是为了在客户端和服务端维护连接,而建立一定的数据结构来维护双方的状态,用这样的数据结构来保证所谓的面向连接的特性。
2、TCP是面向连接的,可靠的、基于字节流的。
3、TCP解决的问题包括:顺序问题、丢包问题、连接维护、流量控制和拥塞控制。
4、TCP的三次握手除了双方建立连接之外,主要为了确认TCP包的序号问题。
每个TCP连接都要有不同的序号,序号每隔4ms加以1,最大是2的32次方-1,然后再从0开始,如果到重复,需要4个多小时,可以保证包的序号不会重复。
5、TCP的三次握手过程: A向B发起SYN,B向A回复ACK,A向B回复ACK的ACK。
6、TCP的四次挥手过程:A向B发起FIN,B向A回复ACK,然后再向A发送FIN,A向B发送ACK。
7、三次握手只需要处理连接,而四次挥手需要处理数据和连接。