發表文章

目前顯示的是 2月, 2017的文章

[LeetCode] 78. Subsets

圖片
Given a set of  distinct  integers,  nums , return all possible subsets. Note:  The solution set must not contain duplicate subsets. For example, If  nums  =  [1,2,3] , a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] https://www.sigmainfy.com/blog/leetcode-subset-i-and-ii.html <== 完整的分析 思路: DFS, recursion,iterative, bit operation 應該都可以做. 題目已經清楚表明數字不重複 所以可以一次挑一個數字往上堆 再加上原來的subset 1.) subset  一開始是空 [[]] 2.) 拿1出來往空list 堆 => [1] 3.) 再加上原來的 subset => [], [1] 4.) 拿2出來list 堆 => [2], [1,2] 5.) 再加上原來的 subset => [], [1], [2], [1,2] 6.) 拿3出來list 堆 => [3], [1,3], [2,3], [1,2,3] 7.) 再加上原來的 subset => [], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]  

[LeetCode] 278. First Bad Version

圖片
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have  n  versions  [1, 2, ..., n]  and you want to find out the first bad one, which causes all the following ones to be bad. You are given an API  bool isBadVersion(version)  which will return whether  version  is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. 思路: 標準的2分法問題, 題目要求越少存取API 越好. 用2分法可以得到 O(log n) 每次從中間切一刀 然後再做判斷.  關鍵在 line_15 跟 line_19

[LeetCode] 438. Find All Anagrams in a String

圖片
438. Find All Anagrams in a StringAdd to List Difficulty: Easy Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter. Example 1: Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc". Example 2: Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start index = 0 is "ab", which is an anagram of "ab". The substring with start index = 1 is "ba", which is an anagram of "ab". The substring with start index = 2 is "ab", which is ...

[LeetCode] 500. Keyboard row

圖片
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"] Note: You may use one character in the keyboard more than once. You may assume the input string will only contain letters of alphabet. 笨笨的方法 還是可以accept. 版上的高手 一行解決! def findWords (self, words) : return filter(re.compile( '(?i)([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$' ).match, words)