
leetcode
文章平均质量分 75
吴正伟的博客
这个作者很懒,什么都没留下…
展开
-
(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)
Search in Rotated Sorted Array I && IILeetcode对有序数组进行二分查找(下面仅以非递减数组为例):int binarySort(int A[], int lo, int hi, int target){ while(lo hi) { int mid = lo + (hi - lo)/2;原创 2014-12-10 21:26:50 · 1186 阅读 · 0 评论 -
每日算法之四十五:Merge Two Sorted Lists(合并有序链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *me原创 2014-09-11 19:17:31 · 702 阅读 · 0 评论 -
每日算法之四十四:Unique Path(矩阵中不重复路径的数目)
Unique Paths:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is t原创 2014-09-06 21:30:00 · 1445 阅读 · 0 评论 -
每日算法之四十三:Rotate List (列表旋转k个元素)
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.这里的k可能是比链表长度要大的数字,因此实际旋转的位置就是k%le原创 2014-08-20 21:31:46 · 852 阅读 · 0 评论 -
每日算法之四十二:Permutation Sequence (顺序排列第k个序列)
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""3原创 2014-08-19 20:52:39 · 903 阅读 · 0 评论 -
每日算法之三十五:Wildcard Matching
模式匹配的实现,'?'代表单一字符,'*'代表任意多的字符,写代码实现两个字符串是否匹配。Implement wildcard pattern matching with support for '?' and '*'.、'?' Matches any single character.'*' Matches any sequence of characters (inclu原创 2014-06-18 09:42:49 · 1256 阅读 · 0 评论 -
每日算法之十七: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.Input:Digit st原创 2014-06-01 14:59:00 · 548 阅读 · 0 评论 -
每日算法之二十八:Longest Valid Parentheses
求最长合法匹配的长度,这道题可以用一维动态规划逆向求解。假设输入括号表达式为String s,维护一个长度为s.length的一维数组dp[],数组元素初始化为0。 dp[i]表示从s[i]到s[s.length - 1]包含s[i]的最长的有效匹配括号子串长度。则存在如下关系:原创 2014-06-03 11:08:22 · 1330 阅读 · 0 评论 -
每日算法之三十四:Multiply Strings
大数相乘,分别都是用字符串表示的两个大数,求相乘之后的结果表示。首先我们应该考虑一下测试用例会有哪些,先准备测试用例对防御性编程会有比较大的帮助,能够考虑一些极端情况。有以下几种用例:1)"0","0" 2)"0","879127346783" 其中一个是零3)"as234","123343" 存在非法字符4)"000000000000001234","2546" 存在零原创 2014-06-15 16:57:38 · 895 阅读 · 0 评论 -
每日算法之二十四:Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.下面是两种方法:(1)原创 2014-06-01 09:59:00 · 674 阅读 · 0 评论 -
每日算法之二十三:Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2014-05-31 11:18:55 · 902 阅读 · 0 评论 -
每日算法之三十三:Trapping Rain Water
这是一个很有意思的问题,求解最大容积问题,值得动脑筋想一想。原题如下:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For原创 2014-06-13 22:04:06 · 809 阅读 · 0 评论 -
每日算法之四十七:Valid Number (验证是否为数字)
要想正确的写出这个函数不是件容易的事情,因为要考虑的事情很多:1)字符串的前后都可能会有空格,但是中间不允许有空格。2)可能有小数,1.235,或者.3522这种形式3)可能有指数形式,2e10 2e-1等形式Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => t原创 2014-09-14 15:46:51 · 1312 阅读 · 0 评论 -
每日算法之四十六:Add Binary(二进制字符创相加)
二进制字符创相加,通过进位的方式逐位考虑。也可以把相加的过程抽象成一个函数。Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".方法一:class Solution {public: s原创 2014-09-14 14:28:18 · 1248 阅读 · 0 评论 -
每日算法之四十八:Plus One (数组表示的十进制加一进位)以及求Sqrt(x)
给定数组表示的十进制数,加一操作。结果依然用十进制的数组表示。这里主要注意最高位(digit[0])依然有进位,即溢出的情况。Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most si原创 2014-09-15 19:33:27 · 1214 阅读 · 0 评论 -
(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)
Remove Duplicates from Sorted Array IILeetcode题目:Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Y原创 2014-12-10 20:07:46 · 835 阅读 · 0 评论 -
(每日算法)Leetcode --- Maximal Rectangle(最大子矩阵)
求在0-1矩阵中找出面积最大的全1矩阵Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.首先,想使用遍历两次的暴力方法解决是不靠谱的,先打消这个念头。这道题的解法灵感来自于 Larg原创 2014-12-25 21:35:42 · 1322 阅读 · 0 评论 -
(每日算法)LeetCode --- Word Search(矩阵中查找单词)
在矩阵中查找给定的单词是否出现,可以向上、向下、向左和向右查找。不能在一个字母上重复查找。Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "a原创 2014-12-08 21:02:34 · 2156 阅读 · 0 评论 -
(每日算法)LeetCode --- Subsets(子集合)
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,原创 2014-12-08 19:55:46 · 970 阅读 · 0 评论 -
(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)
思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解。比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形。Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, f原创 2014-12-18 21:39:57 · 944 阅读 · 0 评论 -
(每日算法)LeetCode --- Combinations (组合数)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2014-12-01 19:05:43 · 965 阅读 · 0 评论 -
(每日算法)LeetCode--Set Matrix Zeroes (矩阵置零)
给定一个矩阵,如果有零元素那么就将零元素所在的行和列都置为零。Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.最直观的解法就是开辟一个新的矩阵,当原矩阵存在零元素的时候,就将新矩阵的对应行和列置为零。这样空间复杂度较高,也是题目不允许的。题目的难原创 2014-11-24 20:09:19 · 1144 阅读 · 0 评论 -
(每日算法)LeetCode---Search a 2D Matrix(查找元素是否存在)
通过二分查找查找元素是否存在矩阵中。原创 2014-11-24 22:02:49 · 604 阅读 · 0 评论 -
(每日算法)LeetCode---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 "BAN原创 2014-11-30 09:51:16 · 1071 阅读 · 0 评论 -
(每日算法)Leetcode--Edit Distance(编辑距离)
简单地说,就是仅通过插入(insert)、删除(delete)和替换(substitute)个操作将一个字符串s1变换到另一个字符串s2的最少步骤数。熟悉算法的同学很容易知道这是个动态规划问题。 其实一个替换操作可以相当于一个delete+一个insert,所以我们将权值定义如下:I (insert):1D (delete):1S (substitute):1原创 2014-11-13 20:58:16 · 2858 阅读 · 0 评论 -
(每日算法)Leetcode--Simplify Path (简单路径)
给定一个Unix风格的路径,简化之。使其不改变路径的结果,但是去掉中间无用的字符。因为系统执行的时候也是逐段查看的,因此最直观的做法就是使用栈来简化,当是/..时,出栈;当是/.时,忽视;当时其他时才进栈。Given an absolute path for a file (Unix-style), simplify it.For example,path = "/ho原创 2014-11-12 21:31:00 · 942 阅读 · 0 评论 -
每日算法之二十二:Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space.原创 2014-05-30 20:11:11 · 931 阅读 · 0 评论 -
每日算法之二十一:Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.原创 2014-05-28 11:36:37 · 683 阅读 · 0 评论 -
每日算法之十三:Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.求解原创 2014-05-21 11:45:10 · 618 阅读 · 0 评论 -
每日算法之二十六:Substring with Concatenation of All Words
给定一个字符串,然后再给定一组相同长度的单词列表,要求在字符串中查找满足以下条件的起始位置:1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次。2)在出现的过程中不能出现其他的干扰单词。3)出现的位置可能有多个。4)单词的出现顺序不做要求。下面是一个例子:S:"barfoothefoobarman"L:"foo","bar"位置0是出现位置,;两个单词均出现仅出原创 2014-06-02 10:59:03 · 972 阅读 · 0 评论 -
每日算法之二十五:Divide Two Integers
Divide two integers without using multiplication, division and mod operator.不使用乘法、除法和求模运算求两个数相除。原创 2014-06-01 15:08:16 · 897 阅读 · 0 评论 -
每日算法之二十七:Next Permutation(下一个字典序)
求解的是下一个字典序,下面给出两个不同理解方式的字典序的定义:可以直接看第二种定义,但是算法还是用官方的定义来求解。1)官方定义:字典序法是由当前序列直接生成下一个排列的算法:排列定义:P = P1P2```Pn 第一步:求满足关系式P(k-1)i = max{k|P(k-1) 第二步:求满足关系式P(i-1)j = max{k|P(i-1) 第三步:P(原创 2014-06-02 15:20:30 · 1324 阅读 · 0 评论 -
每日算法之十二:Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.class Solution {public: int toNUm(char ch) { switch(ch) { case 'I':return 1;原创 2014-05-19 22:11:22 · 720 阅读 · 0 评论 -
每日算法之十一:Integer to Roman
题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.罗马表示方式如下:I = 1;V = 5;X = 10;L = 50;C = 100;D = 500;M = 1000;其中每原创 2014-05-18 22:08:45 · 866 阅读 · 0 评论 -
每日算法之十:Container With Most Water
给定一个向量,其中的每个元素代表了高度,比如height[3] = 5,说明在坐标轴中在点3处存在高度为5的竖线,这样所有的元素就形成一个琴状的形状,最后要求的就是两条竖线之间的矩形形状最大的面积。最直觉的做法就是穷举,这样的复杂度是O(n2),显然还有更合适的方法,因为在这样的方法中有很多确定要小的面积也进行了计算。关于面积有两个变量,一个是横轴之间的距离,;另一个是两条竖轴之间的距离。我们可以固定其中一个变量,很显然,我们可以先取最远的两条竖线进行比较,然后使两个辅助指针逐渐缩小,取两个辅助指针中的较小原创 2014-05-18 21:05:12 · 826 阅读 · 0 评论 -
每日算法之八:String to Integer (atoi) 及溢出分析
要求就是把字符串转化为整形,按照我们的理解就可以逐字符遍历,转化为整形即可,比如字符串"123454",我们只要取出第零个字符'1'-‘0’就可以得到数字1,然后把1乘以10,再加上‘2’-‘0’·····这样依次就可以得到转化之后的整形数字。当然有几个地方需要注意:1)字符串可能以正负号开始,也可能包含一些其他的非数字型字符,是不能转化为数字的,是忽略还是报错看需求2)越界,字符串转化到整形数字之后的数字串可能超出表示范围,超出的可以置为INT_MAX或者INT_MIN。原创 2014-05-10 16:51:43 · 1812 阅读 · 0 评论 -
每日算法之九:Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input原创 2014-05-16 22:13:49 · 802 阅读 · 0 评论 -
每日算法之七:Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321hen原创 2014-05-08 11:06:12 · 872 阅读 · 0 评论 -
每日博客之六:ZigZag Conversion
题目要求是Z字形输出,先竖排,到了nRows之后,之后的字符放在nRows-1行,单独成一列,依次递推,到底一行后,之后的元素放在前一个元素的下方,以此类推即可。原创 2014-05-04 22:08:07 · 799 阅读 · 0 评论 -
每日算法之二十九:Search in Rotated Sorted Array
在一个经过旋转后的有序数组中查找一个目标元素。Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search原创 2014-06-04 22:05:54 · 773 阅读 · 0 评论