随笔分类 - Leetcode
Leetcode: 4Sum
摘要:可以按照3Sum的思路来做,并以此类推,KSum的复杂度就是O(N^(k-1)). 在3Sum外面再套一层循环,相当于N次求3Sum 若还有时间,可以参考https://discuss.leetcode.com/topic/29585/7ms-java-code-win-over-100 有更多优化
阅读全文
Leetcode: Reverse Words in a String
摘要:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Cl...
阅读全文
Leetcode: Longest Consecutive Sequence
摘要:Best Solution: only scan one direction, If the number x is the start of a streak (i.e., x-1 is not in the set), then test y = x+1, x+2, x+3, ... and s
阅读全文
Leetcode: Spiral Matrix
摘要:难度:87,这道题跟 Rotate Image 很相似,都是需要把矩阵分割成层来处理,每一层都是按:1. 正上方;2. 正右方;3. 正下方;4. 正左方这种顺序添加元素到结果集合。实现中要注意两个细节,一个是因为题目中没有说明矩阵是不是方阵,因此要先判断一下行数和列数来确定螺旋的层数,mina(行
阅读全文
Leetcode: Word Ladder
摘要:难度:96.这道题看似一个关于字符串操作的题目,其实要解决这个问题得用图的方法。我们先给题目进行图的映射,顶点则是每个字符串,然后两个字符串如果相差一个字符则我们进行连边。接下来看看这个方法的优势,注意到我们的字符集只有小写字母,而且字符串长度固定,假设是L。那么可以注意到每一个字符可以对应的边则有
阅读全文
Leetcode: Simplify Path
摘要:Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 难度:85,虽然不难,但是里
阅读全文
Leetcode: Max Points on a line
摘要:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.Brute Force的做法,N个点两两可以构成N(N-1)/2条线,我们可以找这N(N-1)/2条线...
阅读全文
Leetcode: Surrounded Regions
摘要:难度:92。这个题目用到的方法是图形学中的一个常用方法:Flood fill算法,其实就是从一个点出发对周围区域进行目标颜色的填充。背后的思想就是把一个矩阵看成一个图的结构,每个点看成结点,而边则是他上下左右的相邻点,然后进行一次广度或者深度优先搜索。 这道题首先四个边缘上的‘O’点都不是被surr
阅读全文
Leetcode: 3Sum Closest
摘要:这道题跟3Sum很像,区别就是要维护一个最小的diff,求出和目标最近的三个和。brute force时间复杂度为O(n^3),优化的解法是使用排序之后夹逼的方法,总的时间复杂度为O(n^2+nlogn)=(n^2),空间复杂度是O(n)。 第二遍做法: 第一遍做法:
阅读全文
Leetcode: 3Sum
摘要:Best 做法:不用Extra Space, Time Complexity still O(N^2) Good syntax: // creating Arrays of String type String a[] = new String[] { "A", "B", "C", "D" }; /
阅读全文
Leetcode: Wildcard Matching
摘要:想法是建立一个2维的boolean数组,booleen[][] check = new boolean[s.length()+1][p.length()+1],注意最好比string的length大一行和一列,来包括第0行和第0列的情况。这样初始化比较方便。check[m][n]表示s的前m个元素是
阅读全文
Leetcode: Edit Distance
摘要:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have t...
阅读全文
Leetcode: Best Time to Buy and Sell Stock III
摘要:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet...
阅读全文
Leetcode: Combination Sum II
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each numb...
阅读全文
Leetcode: Next Permutation
摘要:这道题的算法第一次可能有点难想,但是做过一次后就知道了 Shi Li Analysis(会好理解很多): Starting from the last position, the first time we find that num[i]<num[i+1], we stop. The positi
阅读全文
Leetcode: Merge Intervals
摘要:这道题跟Insert Intervals这道题很像,最开头要先对Intervals里面的Interval排序一下,用到了java的Collections.sort(List<Interval> list, Comparator<? super Interval> c)。 排序以Interval的st
阅读全文
Leetcode: Minimum Window Substring
摘要:难度:90 String问题里面有很多不好做的,动不动就DP什么的,参考了一些资料http://blog.csdn.net/fightforyourdream/article/details/17373203 For example,S = “ADOBECODEBANC”T = “ABC”Minim
阅读全文
Leetcode: Scramble String
摘要:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representatio...
阅读全文
Leetcode: Maximal Rectangle
摘要:难度:90 这是一道非常综合的题目,要求在0-1矩阵中找出面积最大的全1矩阵。刚看到这道题会比较无从下手,brute force就是对于每个矩阵都看一下,总共有m(m+1)/2*n(n+1)/2个子矩阵(原理跟字符串子串类似,字符串的子串数有n(n+1)/2,只是这里是二维情形,所以是两个相乘),复
阅读全文
浙公网安备 33010602011771号