陪suki去coffee chat,出去走了走,还上了willis tower,然而下雨,全都被云挡住了,好不容易有点风,把云拨开了,刚想摆个pose,另一片云又来了。。。
Reverse String
没什么好说的,两个指针Reverse Words in a String
I love yahoo --> yahoo love I
先reverse一下整个String
然后再以空格为界,用两个指针确定单一word,reverse单一word
Right Shift By N Characters
abcdef k = 4 -> abcd | ef -> dcba | fe -> ef | abcd
同上题,先reverse拆分部分,再reverse所有
type5 char replacement
ex. "student" -> "stuxxt" (den -> xx) s1 to s2
two pointer
case1: s1.length() >= s2.length()
照常两个pointer来做
case2:s1.length() < s2.length() (string的长度会超出原来的)
step1: compute the new length after replacement
step2: use two pointer, moving from right to left to guarantee slow is always behind fast.
String shuffling
ABCDE12345 -> A1B2C3D4E5
分成四份(要考虑奇偶性),是的保证chunk1.size = chunk3.size, chunk2.size = chunk4.size,将中间两部分reverse,最后再merge
String permutation
has duplicate letters
String en/decoding
aaaabccaaaaa -> a4b1c2a5
case1: unchaged/shorter -> # of occ >= 2
case2: longer -> # of occ < 2
两个pointer做, 加个count,第一遍做case1并保留单个出现的字符 变成a4bc2a5,第二遍做case2,倒着做得出结果,类似前面的char replacement
Sliding window
也是2pointer,但是物理意义不同,[slow, fast]中间才是需要的,slow前面和fast后面没用
Longest Substring Without Repeating Characters
为保证没有重复char,先用hashset记录the chars in the sliding window
if str[fast] not in hashset, add it and fast++
if str[fast] in hashset, remove str[slow], slow++Find All Anagrams in a String
fixed length
hashmap to record the count of distinct chars
record matchChars,# of char already matched
需要理清思路,有必要重新写一遍
今天看的string2 的lec,各个例题实在是有难度,都是onsite级别的题,思路理解差不多了,但是implement起来还是困难,先放一放