国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Leetcode-Algorithms Keyboard Row

2019-11-08 02:08:06
字體:
來源:轉載
供稿:網友

Given a List of Words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.

Example 1: Input: [“Hello”, “Alaska”, “Dad”, “Peace”] Output: [“Alaska”, “Dad”]

給一個有一些單詞的list,返回一個包含可以在美式鍵盤上只用其中一行字母就可以打出來的單詞的list。(q-p,a-l,z-m) 一開始想到的最蠢的辦法是給三行字母創建三個list用loop一個字母一個字母的檢測是不是在這個list里面。后來看到了用正則表達式(regex)來判斷的簡便方法。

class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ result=[] for w in words: new_word = w.lower() rst1 = re.match('[qwertyuiop]*',new_word).group(0) rst2 = re.match('[asdfghjkl]*',new_word).group(0) rst3 = re.match('[zxcvbnm]*',new_word).group(0) if rst1 != None: if rst1 == new_word: result.append(w) elif rst2 == new_word: result.append(w) elif rst3 == new_word: result.append(w) return result

附上leetcode上的答案:

def findWords(self, words): return filter(re.compile('(?i)([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$').match, words)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塘沽区| 潞城市| 革吉县| 星子县| 河北省| 清涧县| 海阳市| 武山县| 新和县| 从化市| 沂源县| 安庆市| 潞城市| 嘉禾县| 五莲县| 泊头市| 天柱县| 池州市| 麻栗坡县| 大安市| 汕尾市| 江口县| 伊金霍洛旗| 如皋市| 永福县| 新竹市| 遂平县| 济源市| 运城市| 托里县| 荥阳市| 罗源县| 荔浦县| 襄汾县| 怀宁县| 神农架林区| 乌什县| 唐河县| 安福县| 石门县| 开阳县|