
LeetCode
皓波
这个作者很懒,什么都没留下…
展开
-
[Leetcode P10]Regular Expression Matching 正则匹配
总共用了两种解法,一种是我自己递归求解的,效率很低,边界条件很难理清楚,代码很长,第二种是DP解法,把递归的子问题的解存到一个数组里,代码很短。 1. 递归解法 主要想法是根据当前的正则表达式,来决定接下去如何匹配,我们可以用两个字符串的子串去匹配,麻烦的是,可能相同的子字符串被求解了很多遍,特别是.*对应的字符串需要循环求解。写完这个解法以后,我很自然地就知道可以用DP递归求解了,不过原创 2017-07-31 10:50:47 · 339 阅读 · 0 评论 -
[LeetCode P76] Minimum Window Substring
原题:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".Note:原创 2017-08-29 10:46:20 · 248 阅读 · 0 评论 -
[LeetCode P96] Unique Binary Search Trees II
原题:原题:Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1原创 2017-09-06 17:06:08 · 318 阅读 · 0 评论 -
[LeetCode P72] 编辑距离Levenshtein Distance算法[经典动态规划]
突然就写到72题了…中间也遇到了一些难题,60~70的题目有好几题坑题,不过Unique Path里有一题挺不错的,我想用迷宫回溯的方法求解,发现时间越界,其实也是用DP,有空放上来。 原题:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (e原创 2017-08-25 10:54:29 · 463 阅读 · 0 评论 -
[LeetCode P41] First Missing Positive
原题:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.解原创 2017-08-14 13:05:34 · 224 阅读 · 0 评论 -
[LeetCode 37] Sudoku Solver回溯解法
原题:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.解决一个数独,我认为还是比较直观的,一直觉得计算原创 2017-08-12 14:24:04 · 526 阅读 · 0 评论 -
[LeetCode P45] Jump Game II
原题:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to r原创 2017-08-19 12:40:38 · 207 阅读 · 0 评论 -
[LeetCode P32] Longest Valid Parentheses 四种解法
又是一道Hard题,但AC却很简单,用暴力算法,大概5-10分钟就搞定了,比较难的是这题有很多种解法,把这些解法都理解一遍比较好。四种解法都做了注释,可以直接看代码。/*原题:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) p原创 2017-08-11 01:44:22 · 675 阅读 · 0 评论 -
[Leetcode P15] Three Sum 三数之和
很有意思的问题,也让我重新看了一下第一题。 原题:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution s原创 2017-08-03 01:33:53 · 255 阅读 · 0 评论 -
[Leetcode P17] Letter Combinations of a Phone Number(看作递增加法器)
原题:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.(一个手机的九宫格)虽然这题不是很难,但是我原创 2017-08-05 01:08:20 · 282 阅读 · 0 评论 -
[LeetCode P4] Median of Two Sorted Arrays 解法
做到这题的时候,开始觉得不是很难,毕竟O(m+n)的遍历算法是比较容易的,题目要的log(m+n)的算法,乍一看也是很容易想到的,只要我们用QuickSort或者说二分法的想法找到这个rank=(m+n)/2的点就可以了。但是做下去的时候,发现边界条件太复杂了,很难理清楚,特别是奇数和偶数带来的0.5的偏差,让二分做不到真正的二分。相信看到这篇博客的同学也是刷过题,知道其中的难度的,因此本文就主要简原创 2017-07-24 14:59:58 · 342 阅读 · 0 评论 -
[Leetcode P11]Container With Most Water
原题: Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find t原创 2017-07-31 14:24:02 · 271 阅读 · 0 评论 -
[Leetcode P28] Implement strStr()(KMP算法)
原题:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.刚看到这题,我就放了个string.find()上去:return haystack.find(needle);完美AC。 但是转念一想,这题虽然原创 2017-08-07 22:49:29 · 548 阅读 · 0 评论 -
[LeetCode P97] Interleaving String动态规划
原题:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.原创 2017-09-07 10:36:24 · 335 阅读 · 0 评论