随笔分类 - Leetcode
Leetcode: Word Search II
摘要:如果跟Word Search做法一样,还按照DFS回溯的方法,逐个检查每个word是否在board里,显然效率是比较低的。我们可以利用Trie数据结构,也就是前缀树。然后dfs时,如果当前形成的单词不在Trie里,就没必要继续dfs下去了。如果当前字符串在trie里,就说明board可以形成这个wo
阅读全文
Leetcode: Rectangle Area
摘要:Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as sh...
阅读全文
Leetcode: Shortest Palindrome
摘要:本题最简单的思路是从后往前判断子串是否是回文串,然后把后面的弄到前面即可,不过这样仅仅能擦边通过测试 Brute Force: O(N^2), space O(1) DP: O(N^2), space O(N^2) KMP介绍:http://kb.cnblogs.com/page/176818/ 实
阅读全文
Leetcode: Combination Sum III
摘要:ind all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a...
阅读全文
Leetcode: House Robber II
摘要:Analysis: if the last one is not robbed, then you are free to choose whether to rob the first one. you can break the circle by assuming the first hous
阅读全文
Leetcode: Contains Duplicate II
摘要:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the ...
阅读全文
Leetcode: Kth Largest Element in an Array
摘要:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For examp...
阅读全文
Leetcode: Minimum Size Subarray Sum
摘要:Haven't think about the O(nlogn) solution. O(n) solution is to maintain a window My solution: shrink as much as possible while maintaining the window
阅读全文
Leetcode: Add and Search Word - Data structure design
摘要:方法1:brute force: search function compares each word with the pattern, time complexity: O(nk), n is number of words in dictionary, k is averge length 方
阅读全文
Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
摘要:参考百度百科:Trie树 a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes) The time complexity to ins
阅读全文
Leetcode: Course Schedule II
摘要:There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have t
阅读全文
Leetcode: Course Schedule
摘要:The hint provided by leetcode is: Hints: BFS: 典型的拓扑排序。原理也很简单,在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果
阅读全文
Leetcode: Isomorphic Strings
摘要:Best: O(N) time, constant space 复杂度 时间 O(N) 空间 O(N) 思路 用一个哈希表记录字符串s中字母到字符串t中字母的映射关系,一个集合记录已经映射过的字母。或者用两个哈希表记录双向的映射关系。这里不能只用一个哈希表,因为要排除egg->ddd这种多对一的映射
阅读全文
Leetcode: Reverse Linked List
摘要:基本迭代 Time: O(N), Space: O(1) 基本递归 Time: O(N), Space: O(N)递归栈大小
阅读全文
Leetcode: Count Primes
摘要:Count the number of prime numbers less than a non-negative number, n.Algorithm: The sieve of Eratosthenes,Referring to Summary: Primes0 and 1 are not ...
阅读全文
Leetcode: Remove Linked List Elements
摘要:set Dummy node 这题有个重要的follow up,就是在一个环里怎么删除节点: 参考:http://www.cnblogs.com/EdwardLiu/p/5975748.html
阅读全文
Leetcode: Bitwise AND of Numbers Range
摘要:Given a range [m, n] where 0 maxRange) continue; 9 int mi = (m>>i) & 1;10 int ni = (n>>i) & 1;11 if (mi == 1 && n...
阅读全文
Leetcode: Happy Number
摘要:Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive intege
阅读全文
Leetcode: House Robber
摘要:This particular problem and most of others can be approached using the following sequence: https://leetcode.com/problems/house-robber/discuss/156523/F
阅读全文
浙公网安备 33010602011771号