• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅
12 2016 档案
Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

摘要:referred to: https://discuss.leetcode.com/topic/69137/java-o-1-accept-solution-using-hashmap-doublelinkedlist-and-linkedhashset Two HashMaps are used, 阅读全文
posted @ 2016-12-24 03:43 neverlandly 阅读(580) 评论(0) 推荐(0)
Leetcode: Poor Pigs

摘要:My binary encoding method gives a 8 for the first problem, however, there is a smarter method that generates a answer of 5, which can be found here: h 阅读全文
posted @ 2016-12-23 11:37 neverlandly 阅读(1112) 评论(0) 推荐(0)
Leetcode: Validate IP Address

摘要:Be careful, split() will not include trailing empty strings in the result array The string "boo:and:foo", for example, split(":")的结果是 {“boo”, "and", " 阅读全文
posted @ 2016-12-23 08:44 neverlandly 阅读(1089) 评论(0) 推荐(0)
Leetcode: Encode String with Shortest Length && G面经

摘要:DP: Initially I think of 1D DP, dp[i] stands for the shortest string of first i characters, then: dp[i] = minLen{dp[k] + encode(substring(k+1, i))} th 阅读全文
posted @ 2016-12-23 06:25 neverlandly 阅读(2170) 评论(0) 推荐(0)
Leetcode: Concatenated Words

摘要:Scan through array and DP: We iterate through each word and see if it can be formed by using other words. The subproblem is Word Break I. But it is a 阅读全文
posted @ 2016-12-23 01:40 neverlandly 阅读(1339) 评论(0) 推荐(0)
Leetcode: Convex Polygon

摘要:https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation https://discuss.leetcode.com/topic/70664/c-7-line- 阅读全文
posted @ 2016-12-22 13:14 neverlandly 阅读(966) 评论(0) 推荐(0)
Leetcode: Unique Substrings in Wraparound String

摘要:这道题我一开始发现了数学规律: length of consecutive chars vs number of different substring “a” -> 1 = 1 "ab" -> 3 = 1+2 "abc" -> 6 = 1+2+3 "abcd" -> 10 = 1+2+3+4 于是 阅读全文
posted @ 2016-12-22 11:05 neverlandly 阅读(631) 评论(0) 推荐(0)
Leetcode: Count The Repetitions

摘要:目前只想出了Brute Force做法(1165ms), 看到有<20ms的做法,未深究: 阅读全文
posted @ 2016-12-22 07:24 neverlandly 阅读(666) 评论(0) 推荐(0)
Leetcode: Optimal Account Balancing

只有注册用户登录后才能阅读该文。
posted @ 2016-12-22 05:53 neverlandly 阅读(1705) 评论(0) 推荐(0)
Leetcode: Matchsticks to Square && Grammar: reverse an primative array

摘要:DFS, my solution is to fill each edge of the square one by one. DFS to construct the 1st, then 2nd, then 3rd, then 4th. For each edge I scan all the m 阅读全文
posted @ 2016-12-21 07:31 neverlandly 阅读(605) 评论(0) 推荐(0)
Leetcode: Serialize and Deserialize BST

摘要:So the first question is: what is the difference between this and #297? This here is BST, however, in #297, it's BT. "The encoded string should be as 阅读全文
posted @ 2016-12-21 04:44 neverlandly 阅读(754) 评论(0) 推荐(0)
Leetcode: Circular Array Loop

摘要:注意The loop must be "forward" or "backward'. 所以这就是为什么[-2, 1, -1, -2, -2]是false的原因 Just think it as finding a loop in Linkedlist, except that loops with 阅读全文
posted @ 2016-12-20 11:39 neverlandly 阅读(1541) 评论(0) 推荐(0)
Leetcode: Sequence Reconstruction

摘要:Topological Sort: This problem is to determine if there's one, and only one sequence to sort a DAG. The method is to check if the queue's size is alwa 阅读全文
posted @ 2016-12-20 07:15 neverlandly 阅读(973) 评论(0) 推荐(0)
Leetcode: Ternary Expression Parser

摘要:My First Solution: Use Stack and String operation, from the back of the string, find the first '?', push the right to stack. Depends on whether the ch 阅读全文
posted @ 2016-12-20 05:11 neverlandly 阅读(843) 评论(0) 推荐(0)
Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)

摘要:Backtracking + Trie: referred to https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16 A better approach is to ch 阅读全文
posted @ 2016-12-20 01:38 neverlandly 阅读(685) 评论(0) 推荐(0)
Leetcode: Ones and Zeroes

摘要:This is a 0/1 backpacking problem The problem can be interpreted as: What's the max number of str can we pick from strs with limitation of m "0"s and  阅读全文
posted @ 2016-12-19 13:32 neverlandly 阅读(488) 评论(0) 推荐(0)
Leetcode: Heaters

摘要:Binary Search My solution: Be careful in my binary search function, l, r may go out of the range of the array Solution with the highest vote: 阅读全文
posted @ 2016-12-19 13:09 neverlandly 阅读(466) 评论(0) 推荐(0)
Leetcode: Total Hamming Distance

摘要:Example: 0 0 0 0 0 1 0 0 1 1 1 0 0 0 1 0 Total Hamming Distance = (3*1) + (2*2) + (2*2) + (4*0) = 11 so the idea is count the number of 1 and 0 on eac 阅读全文
posted @ 2016-12-19 11:03 neverlandly 阅读(954) 评论(0) 推荐(0)
Leetcode: Hamming Distance

摘要:Solution 1: Solution 2: 阅读全文
posted @ 2016-12-19 09:17 neverlandly 阅读(238) 评论(0) 推荐(0)
Leetcode: Valid Word Square

摘要:这题看起来简单但是其实很容易写错 本来想j 不从0开始,而是从 i+1开始检查,可以节省时间,但是一些为Null的情况会使讨论很复杂 比如 阅读全文
posted @ 2016-12-19 08:37 neverlandly 阅读(295) 评论(0) 推荐(0)
Leetcode: Sentence Screen Fitting

摘要:先来一个brute force, 类似Text Adjustment 但是在稍微大一点的case就TLE了,比如: ["a","b","e"] 20000 20000, 花了465ms 所以想想怎么节约时间, 提示是可以DP的,想想怎么复用,refer to: https://discuss.lee 阅读全文
posted @ 2016-12-19 07:16 neverlandly 阅读(1141) 评论(0) 推荐(0)
Leetcode: Minimum Unique Word Abbreviation

摘要:Refer to https://discuss.leetcode.com/topic/61799/java-bit-mask-dfs-with-pruning bit mask refer to http://bookshadow.com/weblog/2016/10/02/leetcode-mi 阅读全文
posted @ 2016-12-19 04:47 neverlandly 阅读(888) 评论(0) 推荐(0)
Leetcode: Design Phone Directory

摘要:my HashSet+ ArrayList, 删除的时候把要删的index与末尾对调。get()其实不需要random, 因为anyone is ok HashSet+ Queue网上vote最高的solution, 阅读全文
posted @ 2016-12-18 13:37 neverlandly 阅读(343) 评论(0) 推荐(0)
Leetcode: Valid Word Abbreviation

摘要:Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word",... 阅读全文
posted @ 2016-12-18 12:49 neverlandly 阅读(530) 评论(0) 推荐(0)
Leetcode: Range Addition

摘要:Time Complexity: O(N+K), Space: O(1) 阅读全文
posted @ 2016-12-18 11:49 neverlandly 阅读(335) 评论(0) 推荐(0)
Leetcode: Find Leaves of Binary Tree

摘要:Better Solution: https://discuss.leetcode.com/topic/49194/10-lines-simple-java-solution-using-recursion-with-explanation/2 For this question we need t 阅读全文
posted @ 2016-12-18 09:12 neverlandly 阅读(299) 评论(0) 推荐(0)
Leetcode: Design Hit Counter

摘要:Use Queue Better Solution: can solve follow up, refer to https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data- 阅读全文
posted @ 2016-12-18 07:56 neverlandly 阅读(723) 评论(0) 推荐(0)
Leetcode: Bomb Enemy

摘要:Walk through the matrix. At the start of each non-wall-streak (row-wise or column-wise), count the number of hits in that streak and remember it. For 阅读全文
posted @ 2016-12-18 06:52 neverlandly 阅读(500) 评论(0) 推荐(0)
Bloomberg面经准备: Josephus problem

摘要:Solution 1: Math Let f(n,k) denote the position of the survivor. After the kth person is killed, we're left with a circle of n-1, and we start the nex 阅读全文
posted @ 2016-12-17 01:53 neverlandly 阅读(1276) 评论(0) 推荐(0)
Leetcode: Plus One Linked List

摘要:Solution: Use Stack, O(N) space my another solution: don't create new node, modify the existed node Solution 3: reverse the inputs, add one, then reve 阅读全文
posted @ 2016-12-16 13:31 neverlandly 阅读(284) 评论(0) 推荐(0)
Leetcode: Sort Transformed Array

摘要:参考:https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution the problem seems to have many cases a>0, a=0 阅读全文
posted @ 2016-12-16 12:31 neverlandly 阅读(225) 评论(0) 推荐(0)
Leetcode: Rearrange String k Distance Apart

摘要:Analysis: Solution 1: The greedy algorithm is that in each step, select the char with highest remaining count if possible (if it is not in the waiting 阅读全文
posted @ 2016-12-16 11:25 neverlandly 阅读(821) 评论(0) 推荐(0)
Leetcode: Line Reflection

摘要:本题精妙之处在于:1. 如何最快找到possible的line的x axis(我最开始想到要用quickselect find median的方法,结果别人有min max方法) 2. 如何最方便确定一个点关于该line的reflection是否存在,由于既有x又有y,不太好处理,别人有个聪明的办法 阅读全文
posted @ 2016-12-16 06:24 neverlandly 阅读(252) 评论(0) 推荐(0)
Leetcode: Logger Rate Limiter

摘要:HashMap 阅读全文
posted @ 2016-12-16 05:10 neverlandly 阅读(403) 评论(0) 推荐(0)
Leetcode: Design Snake Game

摘要:HashSet + Queue: 吃东西的时候保留尾巴,不吃的时候删去尾巴 one case to notice蛇转弯的时候,要先删去尾巴,再把新的点加进去。如果这个顺序不对的话,本来不会撞上尾巴的结果会判断会撞上 如果没有39行,程序会说dir没有initialize, 其实更好的写法应该是 阅读全文
posted @ 2016-12-16 04:53 neverlandly 阅读(522) 评论(0) 推荐(0)
Leetcode: Android Unlock Patterns

摘要:我自己的backtracking做法 最开始把cur设置为一个dummy value 0 最好的DFS with Optimization beat 97%: https://discuss.leetcode.com/topic/46260/java-dfs-solution-with-clear- 阅读全文
posted @ 2016-12-16 02:01 neverlandly 阅读(543) 评论(0) 推荐(0)
Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结

摘要:我的做法:维护一个window,r移动到超出k distinct character限制是更新max,然后移动l使distinct character <=k; 这种做法更新max只发生在超出限制的时候,有可能永远都没有超出限制,所以while loop完了之后要补上一个max的更新 别人非常棒的做 阅读全文
posted @ 2016-12-15 11:41 neverlandly 阅读(209) 评论(0) 推荐(0)
Design Tic-Tac Toe

摘要:Hint: 看了hint之后想到:既然用数组的话,一行一列都是一个element来代表,估计这个element是要用sum了,那么,能不能用sum来代表一行,使它有且只有一种可能,全部是player1完成的/全部是Player2;所以想到了是Player1就+1,是player2就-1,看最后sum 阅读全文
posted @ 2016-12-15 10:59 neverlandly 阅读(389) 评论(0) 推荐(0)
Leetcode: Moving Average from Data Stream

摘要:Queue method: Array method: 非常聪明 阅读全文
posted @ 2016-12-15 09:54 neverlandly 阅读(397) 评论(0) 推荐(0)
Leetcode: Nested List Weight Sum II

摘要:Inspired by: https://discuss.leetcode.com/topic/49041/no-depth-variable-no-multiplication Instead of multiplying by depth, add integers multiple times 阅读全文
posted @ 2016-12-15 07:35 neverlandly 阅读(657) 评论(0) 推荐(0)
Leetcode: Nested List Weight Sum

摘要:DFS: 阅读全文
posted @ 2016-12-15 06:31 neverlandly 阅读(267) 评论(0) 推荐(0)
Leetcode: Largest BST Subtree

摘要:refer to https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments/2 这道题不好从root到leaf一层一层限制subtree取值范围,因为有可能p 阅读全文
posted @ 2016-12-15 05:59 neverlandly 阅读(332) 评论(0) 推荐(0)
Leetcode: Wiggle Sort II

摘要:这道题应该是hard,首先思想上面参考了https://discuss.leetcode.com/topic/32861/3-lines-python-with-explanation-proof I put the smaller half of the numbers on the even i 阅读全文
posted @ 2016-12-15 03:36 neverlandly 阅读(477) 评论(0) 推荐(0)
Leetcode: Sort Characters By Frequency

摘要:Bucket Sort + HashMap HashMap+ Heap+Wrapper Class 阅读全文
posted @ 2016-12-14 13:37 neverlandly 阅读(293) 评论(0) 推荐(0)
Leetcode: Delete Node in a BST

摘要:recursively find the node that needs to be deleted Once the node is found, have to handle the below 4 cases node doesn't have left nor right - return 阅读全文
posted @ 2016-12-14 12:19 neverlandly 阅读(500) 评论(0) 推荐(0)
Leetcode: Add Two Numbers II

摘要:The key of this problem is to think of using Stack 阅读全文
posted @ 2016-12-14 10:54 neverlandly 阅读(283) 评论(0) 推荐(0)
2Sigma OA prepare: Longest Chain

摘要:DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新 阅读全文
posted @ 2016-12-14 08:19 neverlandly 阅读(8623) 评论(0) 推荐(0)
2Sigma OA prepare: Friends Circle

摘要:DFS & BFS: 关键在于构造graph 如果想把String[] array转化为2d char array, code如下,写的时候这里出了点小问题。friends是String[] Union Find 阅读全文
posted @ 2016-12-14 07:21 neverlandly 阅读(1555) 评论(0) 推荐(0)
Leetcode: Find All Numbers Disappeared in an Array

摘要:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Cou... 阅读全文
posted @ 2016-12-13 12:36 neverlandly 阅读(438) 评论(0) 推荐(0)
Leetcode: Find All Duplicates in an Array

摘要:my solution: put integer A[i] at index A[i]-1 unless A[i] already equals i +1 or A[i] == A[A[i]-1] later scan again if at index i, A[i] != i+1, then A 阅读全文
posted @ 2016-12-13 12:12 neverlandly 阅读(327) 评论(0) 推荐(0)
Leetcode: Minimum Genetic Mutation

摘要:the same with word ladder 写的时候语法上出了一些问题 第5行不用给char数组赋大小 第20行用stringbuilder的时候曾经写成:String afterMutation = new StringBuilder(cur).setCharAt(i, c).toStri 阅读全文
posted @ 2016-12-13 11:03 neverlandly 阅读(1079) 评论(0) 推荐(0)
Leetcode: Island Perimeter

摘要:You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely... 阅读全文
posted @ 2016-12-13 08:37 neverlandly 阅读(299) 评论(0) 推荐(0)
Leetcode: Can I Win

摘要:refer to https://discuss.leetcode.com/topic/68896/java-solution-using-hashmap-with-detailed-explanation After solving several "Game Playing" questions 阅读全文
posted @ 2016-12-13 07:59 neverlandly 阅读(1148) 评论(0) 推荐(0)
Leetcode: Repeated Substring Pattern

摘要:这道题应该没法用DP等解,只能brute force 或者 KMP(为深究) BruteForce, Best Solution for now except KMP 作为Encode String with Shortest Length的subproblem KMP解法未研究,https://d 阅读全文
posted @ 2016-12-13 04:43 neverlandly 阅读(731) 评论(0) 推荐(0)
Leetcode: Assign Cookies

摘要:Solution 1: Greedy, Time:O(nlogn) Just assign the cookies starting from the child with less greediness to maximize the number of happy children . Solu 阅读全文
posted @ 2016-12-13 02:23 neverlandly 阅读(336) 评论(0) 推荐(0)
Leetcode: 132 Pattern

摘要:我觉得这道题是hard,难点第一是要想到用stack,第二是要维护一个这样子的min-max序列:So at any time in the stack, non-overlapping Pairs are formed in descending order by their min value, 阅读全文
posted @ 2016-12-12 06:38 neverlandly 阅读(654) 评论(0) 推荐(0)
Leetcode: Arithmetic Slices II - Subsequence

摘要:参考了https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution 这道题DP思路还是能想出来,Time O(N^2), Space O(N^2) T(i, d), which denote 阅读全文
posted @ 2016-12-12 02:22 neverlandly 阅读(615) 评论(0) 推荐(0)
Leetcode: Minimum Moves to Equal Array Elements II

摘要:Just like meeting point problem, find the median elements in the array after sorting, so Solution 1: Sort, find the median: O(NlogN) Solution 2: Quick 阅读全文
posted @ 2016-12-10 02:59 neverlandly 阅读(408) 评论(0) 推荐(0)
Leetcode: 4Sum II

摘要:一开始想了一个O(n^3), space O(N)的做法,后来发现还可以优化 Solution: time O(N^2), space O(N^2) 阅读全文
posted @ 2016-12-09 01:03 neverlandly 阅读(333) 评论(0) 推荐(0)
Leetcode: K-th Smallest in Lexicographical Order

摘要:第二遍做法:参考https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination Actually this is a denary tree (each n 阅读全文
posted @ 2016-12-08 07:37 neverlandly 阅读(625) 评论(0) 推荐(0)
Leetcode: Minimum Number of Arrows to Burst Balloons

摘要:我的Greedy+Heap做法: Array按start moment升序sort,Heap里按end moment升序sort,一旦到Heap里结束时间最早的气球的end,就要扔arrow,这时在heap里的气球都会被引爆 改进:因为一旦气球爆了的话,Heap里面的元素要全部清空,不需要知道Hea 阅读全文
posted @ 2016-12-08 04:33 neverlandly 阅读(477) 评论(0) 推荐(0)
Leetcode: Minimum Moves to Equal Array Elements

摘要:idea 1: suppose it takes y steps to equal each elements, then this equation stands: (min + y) * num.length == sum + (num.length-1)*y, where min + y is 阅读全文
posted @ 2016-12-08 00:48 neverlandly 阅读(388) 评论(0) 推荐(0)
Leetcode: Number of Boomerangs

摘要:Solution: Use HashTable, Time: O(N^2), Space: O(N) 我的:注意14行是有value个重复distance,表示有value个点,他们跟指定点距离都是distance,需要选取2个做permutation, 所以是value * (value-1) 别 阅读全文
posted @ 2016-12-07 13:51 neverlandly 阅读(496) 评论(0) 推荐(0)
Leetcode: Arranging Coins

摘要:count is the # of level, sum is the accumulated coins Better Solution: Binary Search, 因为怕溢出,所以(1+m)m/2表示成了line6那种样子. 用m去估计最后返回的row 阅读全文
posted @ 2016-12-07 13:44 neverlandly 阅读(290) 评论(0) 推荐(0)
Leetcode: Path Sum III

摘要:Add the prefix sum to the hashMap, and check along path if hashMap.contains(pathSum+cur.val-target); My Solution 一个更简洁的solution: using HashMap to stor 阅读全文
posted @ 2016-12-07 12:17 neverlandly 阅读(611) 评论(0) 推荐(0)
Leetcode: All O`one Data Structure

摘要:Solution: O(1) time complexity 解题思路主要参考了网友ivancjw的帖子,数据结构参考了https://discuss.leetcode.com/topic/65634/java-ac-all-strict-o-1-not-average-o-1-easy-to-re 阅读全文
posted @ 2016-12-07 09:09 neverlandly 阅读(1427) 评论(0) 推荐(0)
Leetcode: Find Right Interval

摘要:Solution 1: TreeMap, Time complexity: O(NlogN) 像这种在一个集合里面寻找有没有比某个数小的数,一般要么treeMap要么treeSet。Interval的题经常需要用treeMap, Data Stream as Disjoint Intervals 就 阅读全文
posted @ 2016-12-07 04:53 neverlandly 阅读(449) 评论(0) 推荐(0)
Leetcode: Non-overlapping Intervals

摘要:Actually, the problem is the same as "Given a collection of intervals, find the maximum number of intervals that are non-overlapping." (the classic Gr 阅读全文
posted @ 2016-12-07 01:28 neverlandly 阅读(765) 评论(0) 推荐(0)
Leetcode: Number of Segments in a String

摘要:用split() 不用API, better solution, O(N) time O(1) space 阅读全文
posted @ 2016-12-07 00:36 neverlandly 阅读(417) 评论(0) 推荐(0)
Leetcode: Strong Password Checker

摘要:refer to https://discuss.leetcode.com/topic/63854/o-n-java-solution-by-analyzing-changes-allowed-to-fix-each-problem http://www.cnblogs.com/grandyang/ 阅读全文
posted @ 2016-12-06 05:38 neverlandly 阅读(573) 评论(0) 推荐(0)
G 面经 && Leetcode: Longest Repeating Character Replacement

摘要:真是被这道题气死了,总是弄不对 The problem says that we can make at most k changes to the string (any character can be replaced with any other character). So, let's 阅读全文
posted @ 2016-12-05 13:58 neverlandly 阅读(440) 评论(0) 推荐(0)
Leetcode: Reconstruct Original Digits from English

摘要:# of '0': # of 'z' # of '2': # of 'w' 4: u 6: x 8: g 3: h - 8 5: f - 4 7: s - 6 1: o - 0 - 2 - 4 9: i - 5 - 6 - 8 我的code用了一个数组来存char count 阅读全文
posted @ 2016-12-05 08:51 neverlandly 阅读(362) 评论(0) 推荐(0)
Leetcode: Maximum XOR of Two Numbers in an Array

摘要:Solution 1: Bit Manipulation: 这题真心有点难,get the max XOR bit by bit note that if A^B=C, then A^C=B, B^C=A Solution 2: Trie, (未研究) https://discuss.leetcod 阅读全文
posted @ 2016-12-05 07:48 neverlandly 阅读(552) 评论(0) 推荐(0)
Leetcode: Battleships in a Board

摘要:只计算每个battleship的第一个元素,所以后面‘X’如果above或者left也是'X'的话,不被计算 阅读全文
posted @ 2016-12-05 06:10 neverlandly 阅读(429) 评论(0) 推荐(0)
Leetcode: Split Array Largest Sum

只有注册用户登录后才能阅读该文。
posted @ 2016-12-05 04:44 neverlandly 阅读(546) 评论(0) 推荐(0)
Leetcode: Find All Anagrams in a String

摘要:Time Complexity will be O(n) because the "start" and "end" points will only move from left to right once. Sliding Window: Use a count to denote the di 阅读全文
posted @ 2016-12-03 13:14 neverlandly 阅读(1403) 评论(0) 推荐(0)
Leetcode: Pacific Atlantic Water Flow

摘要:这题考点在于需要设置两个visited数组 Two Queue and add all the Pacific border to one queue; Atlantic border to another queue. Keep a visited matrix for each queue. I 阅读全文
posted @ 2016-12-03 12:05 neverlandly 阅读(799) 评论(0) 推荐(0)
Leetcode: Partition Equal Subset Sum

摘要:Backpack problem: dp[i][j] means if the first i elements can sum up to value j dp[i][j] = dp[i-1][j] || (j>=nums[i-1] && dp[i-1][j-nums[i-1]]) the res 阅读全文
posted @ 2016-12-03 10:29 neverlandly 阅读(308) 评论(0) 推荐(0)
Leetcode: Third Maximum Number

摘要:My Solution: Highest votes in discussion 阅读全文
posted @ 2016-12-03 06:22 neverlandly 阅读(374) 评论(0) 推荐(0)
Leetcode: Arithmetic Slices

摘要:A slice (P, Q)其实就是从P到Q的序列,(P,Q)是arithmetic slice要求是等差数列且至少有三个数,问array里面有多少个这样的(P, Q) 这道题难点在于想到用DP,一旦想到用DP,就迎刃而解 dp[i]表示以i结束的slice有多少个 阅读全文
posted @ 2016-12-03 05:12 neverlandly 阅读(370) 评论(0) 推荐(0)
Leetcode: Trapping Rain Water II

摘要:The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain. After the rain, water are trapped between th 阅读全文
posted @ 2016-12-03 03:16 neverlandly 阅读(558) 评论(0) 推荐(0)
Leetcode: Remove K Digits

摘要:Greedy + Stack: 用一个栈维护最后要留存下来的digits 需要注意的是:如果遍历到string的某个char, string后面的char数刚刚好能填满这个栈,那么即使这个char比栈顶还要小,也不出栈,直接入栈 最后要删除leading 0 用char[]实现栈,思路一样,要快很多 阅读全文
posted @ 2016-12-03 00:24 neverlandly 阅读(439) 评论(0) 推荐(0)
Leetcode: Add Strings

摘要:Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is =0 || j>=0 || carry!=0) { 8 int sum = 0; ... 阅读全文
posted @ 2016-12-02 12:46 neverlandly 阅读(324) 评论(0) 推荐(0)
Leetcode: Longest Palindrome

摘要:Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not cons... 阅读全文
posted @ 2016-12-02 12:26 neverlandly 阅读(254) 评论(0) 推荐(0)
Leetcode: Queue Reconstruction by Height

摘要:refer to: https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution E.g.input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]subar 阅读全文
posted @ 2016-12-02 11:58 neverlandly 阅读(371) 评论(0) 推荐(0)
Leetcode: Convert a Number to Hexadecimal

摘要:记住要delete begining 0 要记住单独处理 num == 0的情况 阅读全文
posted @ 2016-12-02 11:06 neverlandly 阅读(409) 评论(0) 推荐(0)
Leetcode: Frog Jump

摘要:Solution 1: Backtracking, but will TLE Better Solution: DP (refer to https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-wi 阅读全文
posted @ 2016-12-02 08:35 neverlandly 阅读(773) 评论(0) 推荐(0)
Leetcode: Binary Watch

摘要:Solution 1: Bit Manipulation use Integer.bitCount() Solution 2: Backtracking, 非常精妙之处在于用了两个数组来帮助generate digit(例如:1011 -> 11) 阅读全文
posted @ 2016-12-02 05:00 neverlandly 阅读(244) 评论(0) 推荐(0)
Leetcode: Evaluate Division

摘要:Graph, DFS (1) Build the map, the key is dividend, the value is also a map whose key is divisor and value is its parameter. For example, a / b = 2.0, 阅读全文
posted @ 2016-12-02 02:10 neverlandly 阅读(329) 评论(0) 推荐(0)
Leetcode: Fizz Buzz

摘要:Solution 1: Solution with out using % 阅读全文
posted @ 2016-12-01 12:46 neverlandly 阅读(422) 评论(0) 推荐(0)
Leetcode: Integer Replacement

摘要:Refer to: https://discuss.leetcode.com/topic/58334/a-couple-of-java-solutions-with-explanations/2 When to add 1 instead of minus 1, here is an example 阅读全文
posted @ 2016-12-01 12:31 neverlandly 阅读(364) 评论(0) 推荐(0)
Leetcode: Longest Substring with At Least K Repeating Characters

摘要:Analysis: Given a string s, find out all chars that are invalid (i.e., count < k). The longest substring must reside in one of the substrings divided 阅读全文
posted @ 2016-12-01 11:33 neverlandly 阅读(427) 评论(0) 推荐(0)
Leetcode: Rotate Function

摘要:F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 [4, 3, 2, 6] F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 [6, 阅读全文
posted @ 2016-12-01 08:12 neverlandly 阅读(226) 评论(0) 推荐(0)
Leetcode: UTF-8 Validation

摘要:这道题题干给出了判断 one single UTF-8 char的方法,然后给一个UTF-8 char sequence,判断是不是正确sequence. (读题读了很久) 这道题关键是要学到用 & 取出一个bit sequence当中几位的方法 二进制数表示法:在前面加 0b, 八进制加0o, 十 阅读全文
posted @ 2016-12-01 07:09 neverlandly 阅读(733) 评论(0) 推荐(0)
Leetcode: Decode String

摘要:自己的做法:这种括号问题肯定是用栈,最好是先在栈里存一个空元素,然后stack.peek().append()各种操作 一个栈存string, 一个栈存number, 维护一个指针numStart指向数字的开始 1. 遇到数字啥也不做 2. 遇到char: stack.peek().append(c 阅读全文
posted @ 2016-12-01 05:07 neverlandly 阅读(474) 评论(0) 推荐(0)
Leetcode: Perfect Rectangle

摘要:Refer to https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java and https://discuss.leetcode.com/topic/55923/o-n-solutio 阅读全文
posted @ 2016-12-01 02:12 neverlandly 阅读(562) 评论(0) 推荐(0)

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3