題目套路
對(duì)指定的類型(數(shù)組僧凤,單鏈表柴我,字符串)以指定規(guī)則進(jìn)行扭轉(zhuǎn)芹敌。
題目列表和思路:
189. Rotate Array
https://leetcode.com/problems/rotate-array
數(shù)組n账蓉,扭轉(zhuǎn)次數(shù)k,現(xiàn)根據(jù)k和n取真實(shí)的扭轉(zhuǎn)軸pivot。
然后將整個(gè)數(shù)組逆轉(zhuǎn)矾兜,然后在軸的兩邊分別扭轉(zhuǎn)。
舉例:
nums=[1,2,3,4,5,6,7],k=2,n=7,move=k%n=2
[1,2,3,4,5,6,7]->[7,6,5,4,3,2,1]->[6,7,1,2,3,4,5]
61. Rotate List
https://leetcode.com/problems/rotate-list
思路與上題類似,先使用一個(gè)指針fast趾撵,計(jì)算出鏈表的長(zhǎng)度。
151. Reverse Words in a String
https://leetcode.com/problems/reverse-words-in-a-string
186. Reverse Words in a String II
https://leetcode.com/problems/reverse-words-in-a-string-ii
思路為將整個(gè)字符串逆轉(zhuǎn)共啃,然后按照每個(gè)單詞再逆轉(zhuǎn)
"the sky is blue"->"eulb si yks eht"->"blue is sky the"
要注意占调,去除多余的空格
153. Find Minimum in Rotated Sorted Array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array
154. Find Minimum in Rotated Sorted Array II
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii
33. Search in Rotated Sorted Array
https://leetcode.com/problems/search-in-rotated-sorted-array
81. Search in Rotated Sorted Array II
https://leetcode.com/problems/search-in-rotated-sorted-array-ii
在已經(jīng)被扭轉(zhuǎn)的數(shù)組中進(jìn)行搜索。
思路:先用二分查找找到扭轉(zhuǎn)周pivot移剪,然后再根據(jù)pivot的位置計(jì)算真實(shí)的mid究珊,繼續(xù)通過二分查找查找需要的元素。
CheckPoint1:怎么找扭轉(zhuǎn)軸
CheckPoint2:找到扭轉(zhuǎn)軸后如何利用真實(shí)的mid二分查找