leetcode中的经典题
文章平均质量分 65
zhangwei1120112119
这个作者很懒,什么都没留下…
展开
-
Jump Game II
难度:4 好题。。动态规划O(n^2)会超时,必须O(n) 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 len原创 2013-11-20 12:37:42 · 656 阅读 · 0 评论 -
Merge Sorted Array
难度:2 解法: 归并,如果是按照从小到达从左往右插入A数组,势必要新增一个临时的数组 换成从大到小从右往左,就可以省掉这部分空间 class Solution { public: void merge(int A[], int m, int B[], int n) { int ia=m-1,ib=n-1; int k=m+n-1;原创 2013-11-26 12:23:12 · 527 阅读 · 0 评论 -
Median of Two Sorted Arrays
题意: 给数组a[0],a[1]...a[m-1] 和数组b[0],b[1]...b[n-1] 两个数组均已排序 求中位数 解法: 如果m和n均为偶数或者均为奇数 所求为a[i],b[j]平均值,满足i+j=(m+n)/2-1 如果a[i]>b[j],则应满足a[i]a[i-1] 如果a[i]b[j-1] 如果m和n一个为偶数,一个为奇数 所求为a[i],b[j]较大值,原创 2013-11-11 15:53:51 · 628 阅读 · 0 评论 -
Longest Palindromic Substring
很容易想到O(n^2)的DP做法,不过这题有O(n)做法 表述拙计,还是贴我参考的文章的链接吧 http://blog.csdn.net/hopeztm/article/details/7932245 讲的比较详细 然后是我的代码 class Solution { public: string longestPalindrome(string s) {原创 2013-11-13 16:30:27 · 601 阅读 · 0 评论 -
3Sum 3Sum Closest 4Sum
3Sum的题意: 给出一列数,找出所有的满足条件的三个数,条件:三个数相加等于0 3Sum Closest 的题意: 给出一列数,找出三个数使得三个数相加最接近target 4Sum: 和3sum一样,不过是4个数 解法: 都一样。。基本改几行就能过另外两个题 3Sum的解法: 按从小到大排序 假设找到的三个数a,b,c满足 a+b+c=0 a 从左往右枚举原创 2013-11-15 18:27:15 · 706 阅读 · 0 评论 -
Longest Valid Parentheses
难度:3 求最长的括号匹配的子串 比如 ())()()的括号匹配的子串为(),()()两个,最长的长度为4 解法: 维护一个栈,栈底值为上次匹配失败的位置 首先初始放一个-1入栈代表上次匹配失败的地方为-1 依次扫描字符 若为'(',将位置放入栈中 若为')',若栈中元素大于1个,则代表有'('可匹配,更新最优值,否则更新栈底 显然,对于任意一个部分最长子串,其最后一个字符更新原创 2013-11-18 23:13:44 · 2671 阅读 · 0 评论 -
Submission Details
难度:2 一开始的想法是用二进制位表示行列是否应该清空,- -题目没给行列的范围,不好猜 标准解法是把标志位放在第一行,第一列数上 第一行,第一列的数若为0则代表该行/列应被置0 此外维护两个标记,标记第一行第一列是否应该被清空 Given a m x n matrix, if an element is 0, set its entire row and column to 0原创 2013-11-25 10:32:38 · 609 阅读 · 0 评论 -
Container With Most Water
题意: 给一列数a[0],a[1],,,a[n-1],取其中两个数a[i],a[j]使得min(a[i],a[j])*(j-i)最大 普遍的解法是设置两个索引,分别从左往右,从右往左移动 每次选择a值小的移动 实践证明这个做法是对的,但是这样真的能够遍历到所有情况的证明还没看见,暂时理解为未证明的贪心 class Solution { public: int maxArea(原创 2013-11-15 14:07:51 · 709 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
题意理解为实现STL的unique函数 难度:3 看代码更易理解,挺短的 class Solution { public: int removeDuplicates(int A[], int n) { // IMPORTANT: Please reset any member data you declared, as // the sam原创 2013-11-16 14:15:42 · 559 阅读 · 0 评论 -
Maximum Subarray
最大子段和,本来是打算用单调队列写的 看到这句 More practice: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. 玛雅还有分治法可以做,看了一下,空间复杂度比单原创 2013-11-20 21:36:02 · 693 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
此题让我体会到我的智商真不怎么样。。 给一个字符串,求一个子串,不包含任何重复的字母 比如abca就有重复的字母a 第一想法是动态规划,但是没有想出来怎么定义状态 看了别人的思路 设置两个索引,head和tail,表示当前所枚举到的集合s[head...tail] 依次增加tail,每增加一个字符,若当前集合中存在,则增加head直到当前集合没有这个字符 这样就可以O(n)复杂度枚举原创 2013-11-12 22:03:30 · 606 阅读 · 0 评论 -
Palindrome Number
难度:3 值得注意的地方:输入为1000110001,每次删掉最高位和最低位时bit/=100而不是bit/=10,求一个数的最高位的方式也值得注意一下 题意:输入一个int判断是否为回文,不允许转化为字符串进行判断 解法:每次获得数的最高位和最低位判断,快速获得最高位的的方法见代码 class Solution { public: int HighestBit(int x)//4原创 2013-11-13 18:04:52 · 546 阅读 · 0 评论 -
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原创 2013-11-19 19:50:12 · 1568 阅读 · 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 example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2013-11-19 21:59:07 · 2433 阅读 · 0 评论 -
Single Number
题意: 有n个数,其中n-1个数出现了两次,只有一个数出现了一次 比如2 2 3 3 5 这其中只有5出现了一次,找到这个只出现了一次的数 难度:3 解法: 所有数按位异或,出现了两次的数会被消掉,只剩下出现了一次的数 class Solution { public: int singleNumber(int A[], int n) { int ans=0原创 2013-11-11 19:25:07 · 604 阅读 · 0 评论 -
Gray Code
难度:2 解法: 这题的价值在于有一个结论 二进制数n 对应的格雷码n^(n>>1) class Solution { public: vector grayCode(int n) { vectorans; n=1<<n; for(int i=0;i<n;i++) { ans.pu原创 2013-11-26 12:25:09 · 670 阅读 · 0 评论