267. Palindrome Permutation II: 其實(shí)就是一個(gè)backtracking蕉毯,把每一個(gè)字符加入map杜秸,然后進(jìn)行backtracking就可以了埋泵。
271. Encode and Decode Strings: 一道設(shè)計(jì)的題目砂竖,所有設(shè)計(jì)的題目最后都去看一遍答案遂蛀,因?yàn)轱@然別人都會(huì)有更有解谭跨,而我只是有解。
274. H-Index: 可以先倒排,然后找出citations[i] < i + 1 的第一個(gè)值螃宙,然后返回這個(gè)值就可以了蛮瞄。這題如果是不準(zhǔn)排序,也就是要求時(shí)間復(fù)雜度為n谆扎,那么用一個(gè)array做hash挂捅,這時(shí)候array既保持了一些index的特性,又有hash的計(jì)數(shù)性堂湖。
275. H-Index II: 如果是直接先排好序的闲先,可以用二分法。
277. Find the Celebrity: 這題一開始的思路是无蜂,先找出一個(gè)人都不認(rèn)識(shí)的candidates伺糠,然后再在其中找出,一個(gè)所有人都認(rèn)識(shí)的人
class Solution(object):
def findCelebrity(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 0:
return 0
candidate = 0
for i in xrange(1, n): #因?yàn)閏ele是一個(gè)人都不認(rèn)識(shí)斥季,所以如果一個(gè)人不認(rèn)識(shí)candidate退盯,那么這個(gè)人才可能是cele,而此時(shí)candidate不可能是cele泻肯,所以一遍loop就可以找出candidate
if not knows(i, candidate):
candidate = i
# 驗(yàn)證所有人都認(rèn)識(shí)candidate
for i in xrange(candidate):
if not knows(i, candidate):
return -1
# 驗(yàn)證candidate誰不都認(rèn)識(shí)
for i in xrange(n):
if i == candidate:
continue
if knows(candidate, i):
return -1
return candidate
279. Perfect Squares: 一道dp的題目
280. Wiggle Sort: 這事wiggle sort1渊迁,帶等號(hào)所以還算是簡(jiǎn)單吧。
class Solution(object):
def wiggleSort(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
# 對(duì)于每一個(gè)i%2 == 1的值來說灶挟,如果 nums[i] < nums[i-1]的值琉朽,就進(jìn)行swap
# 每交換一次就可以保證左邊是有序的
# 同樣,對(duì)于 i%2 == 0的值來說稚铣,如果 nums[i]>nums[i-1],就進(jìn)行swap
# 對(duì)于 i-1箱叁, i,i+1這三個(gè)值來說(nums[i]大于兩邊的值)
# 如果 nums[i-1] > nums[i] 那么swap惕医,就變成了 i, i-1, i+1 此時(shí) nums[i-1] > nums[i] 左邊滿足
# 如果 nums[i+1] > nums[i-1]那么nums[i+1]也必定大于 nums[i] swap變成耕漱,nums[i],nums[i+1],nums[i-1]
for i in range(len(nums)):
if i % 2 == 1 and nums[i] < nums[i-1]:
nums[i-1], nums[i] = nums[i], nums[i-1]
elif i % 2 == 0 and i > 0 and nums[i] > nums[i-1]:
nums[i-1], nums[i] = nums[i], nums[i-1]
281. Zigzag Iterator: design的題目,救助當(dāng)前的當(dāng)前的行數(shù)i和列數(shù)j抬伺,然后和當(dāng)前層的length比較就可以了螟够,如果j == length了,就增加i峡钓,如果i也到頭妓笙,就從0開始再循環(huán)一遍。
284. Peeking Iterator: 記錄一個(gè)prev和cur就可以了
285. Inorder Successor in BST: 用stack或者inorder traversal都比較容易做出來