發表文章

目前顯示的是 2017的文章

Excel Sheet

168. Excel Sheet Column Title 思路: 數字 轉 字母. reference map 0 ~ 25 對應到 A - Z 所以在處理n 的時候要 做 (n - 1) // 26. 26 => (26 - 1) % 26 = 25, ref[25] = 'Z' class Solution(object):     def convertToTitle(self, n):         """         :type n: int         :rtype: str         """         dic = {}         res = []         for i in range(65, 91):             dic[i - 65] = str(unichr(i))         while n:             res.insert(0, dic[(n-1) % 26])             n = (n-1) // 26         return "".join(res) 171. ...

[ LeetCode ] 5. Longest Palindromic Substring

圖片
Given a string  s , find the longest palindromic substring in  s . You may assume that the maximum length of  s  is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example: Input: "cbbd" Output: "bb" 思路: DP http://yumodev.com/2016/08/16/ leetcode-5-LongestPalindrome. html https://www.youtube.com/watch?v=Fi5INvcmDos Time: O(n^2) Space: O(n^2) 1. 起一個 2 維 矩陣 dp, dp[start][end] 表示 s[start][end] 是否為 迴文 假設字串 為 "babad" 我們可以看到從左上到右下 都為 1 (True), 因為子字串 "b" "a" "b" "a" "b" 都只有一個字 所以一定是 迴文 (line 14) 2. 接者如何判斷一個子字串是否是迴文呢? 假設現在要判斷 "babab" 是否是迴文, 我們可以拆成兩步, a) 字串 頭尾是不是相等 (line 18) b) 中間的子串 在這裡是 "aba" 是否為迴文 (line 16). 上面黃色的位置     另外 如果現在要判斷的字串長度等於 2. 比方說 "bb" 這裡因為沒有中間的子串     所以直接算是 True (line 17) 3. 每次字串被判定為 迴文的時候 就比較一下現在最長的字串

Teach Yourself Programming in Ten Years - by Peter Norvig

Teach Yourself Programming in Ten Years - by  Peter Norvig http://norvig.com/21-days.html Why is everyone in such a rush? Walk into any bookstore, and you'll see how to  Teach Yourself Java in 24 Hours  alongside endless variations offering to teach C, SQL, Ruby, Algorithms, and so on in a few days or hours. The Amazon advanced search for [ title: teach, yourself, hours, since: 2000  and found 512 such books. Of the top ten, nine are programming books (the other is about bookkeeping). Similar results come from replacing "teach yourself" with "learn" or "hours" with "days."The conclusion is that either people are in a big rush to learn about programming, or that programming is somehow fabulously easier to learn than anything else. Felleisen  et al.  give a nod to this trend in their book  How to Design Programs , when they say "Bad programming is easy.  Idiots  can learn it in  21 days , even if they are  dummies ." The Abtrus...

Like San Mateo

Table of Content  Table of Content   637. Average of Levels in Binary Tree    7 597. Friend Requests I: Overall Acceptance Rate    8 157. Read N Characters Given Read4    8 398. Random Pick Index    8 314. Binary Tree Vertical Order Traversal    9 209. Minimum Size Subarray Sum    10 283. Move Zeroes    11 311. Sparse Matrix Multiplication    11 17. Letter Combinations of a Phone Number    11 91. Decode Ways    12 56. Merge Intervals    12 49. Group Anagrams    13 43. Multiply Strings    14 128. Longest Consecutive Sequence    14 297. Serialize and Deserialize Binary Tree    15 23. Merge k Sorted Lists    16 325. Maximum Size Subarray Sum Equals k    17 252. Meeting Rooms    17 253. Meeting Rooms II ...