題290(easy)
題目要求如果pattern裡的字母重複模式和單詞重複模式一緻,就傳回true,否則傳回false。
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
1.pattern = “abba”, str = “dog cat cat dog” should return true.
2.pattern = “abba”, str = “dog cat cat fish” should return false.
3.pattern = “aaaa”, str = “dog cat cat dog” should return false.
4.pattern = “abba”, str = “dog dog dog dog” should return false.
思路是首先将str中的單詞分離出來。然後用一個list1儲存所有pattern的不同狀态,比如’a’’b’’c’。用另一個list2儲存pattern 和str單詞的對應關系。如果list2的數量大于list1的數量,則代表有不同的對應關系出現,即出現了和模式不一樣的對應,則傳回False。
代碼如下:
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
num=[]
for i in range(len(pattern)): #分出str中的單詞
num.append(str.split(' ')[i])
res=[]
biao1=[] # 存儲[pattern word]對
biao2=set(pattern) # 存儲不同的pattern
for i in range(len(pattern)):
res.append([pattern[i],num[i]])
for j in res:
if j not in biao1:
biao1.append(j)
if len(biao1)>len(biao2): # 如果有大于pattern數目的對應關系代表,代表了出現和pattern不一樣的關系
return False
else:
return True
pa="abba"
num="cat dog cat cat"
s=Solution()
print(s.wordPattern(pa,num))
結果:

題17 (medium)
題目要求是輸出對應電話機數字的字母小标,如下圖所示。
示例:
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
我的思路是:
1.首先挑出輸入數字所對應的字母存入res[],對應長度就是我們要選出的字母的長度l
2.首先tmp指派res[0],對應第一個字母的所有選擇。(注意這裡我用了copy.copy()隻進行值的複制,否則之後res數組會變化)
3.進行剩下的l-1次選擇,利用周遊把目前res中的數添加到之前tmp中的數的末尾,然後進行長度判斷:
長度=i+1對應于上次選擇的結束,該次直接在後面添加這次的選擇即可。如果長度已經達到指定要求,将結果append到res1。
最後傳回res1即可。
代碼如下:
import copy
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if digits=="":
return []
res = [] # 存儲挑出來的數字對應的字母
res1 = [] # 存儲結果
nums = [[],['a','b','c'],['d','e','f'],['g','h','i'],
['j','k','l'],['m','n','o'],['p','q','r','s'],
['t','u','v'],['w','x','y','z'], ['+'],
[''],['#']]
l = len(digits)
for i in range(l):
di = int(digits[i])-
res.append(nums[di])
tmp = copy.copy(res[])
if l==:
return tmp
for i in range(l-): # 挑選次數
for j in res[i+]:
for n in j:
for k in tmp:
if len(k)==i+:
tmp.append(k+n)
if len(k+n)==l:
res1.append(k+n) # 如果達到指定長度就結束,增加結果到res1
continue
return res1
s = Solution()
print(s.letterCombinations("23"))
結果如下:
題605(easy)
題目是說玩一個養花遊戲,有1的位置代表有花種在這裡,花隻能種在list中為0的位置,且該位置左右不能為1。
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False
我的思路如下:
1.種花時不僅考慮該位置為空,同時它的左右兩邊都要為空。
2.當種下花後,把該位置左右兩邊的值都設為1,表示下一次種花不能種在這裡。
3.如果能成功種植的花把位置傳給res相應位置,不能種植的花,值設為-1
4.最後判斷res中是否有-1的存在,若有,則不能成功種花,傳回False。
代碼如下:
class Solution:
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
res=[- for i in range(n)]
for i in range(n):
if flowerbed[]== and flowerbed[]==:
flowerbed[]=
flowerbed[]=
res[i]=
if flowerbed[-]== and flowerbed[-]==:
flowerbed[-]=
flowerbed[-]=
res[i]=len(flowerbed)-
for j in range(,len(flowerbed)-):
if flowerbed[j]== and flowerbed[j-]== \
and flowerbed[j+]==:
flowerbed[j]=
flowerbed[j-]=
flowerbed[j+]=
res[i]=j
if - in res:
return False
else:
return True
flowerbed=[,,,,]
s = Solution()
print(s.canPlaceFlowers(flowerbed,))
結果如下: