
LeetCode
文章平均质量分 50
zhangwei1120112119
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Regular Expression Matching
A串和B串,B串中有特殊字符 '.' Matches any single character. '*' Matches zero or more of the preceding element. 模拟之 设置两个索引PA,PB表示当前匹配到了该位置,从末尾到头匹配 class Solution { public: bool isMatch(const char *s,原创 2013-11-15 13:39:44 · 687 阅读 · 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 · 648 阅读 · 0 评论 -
Edit Distance
难度:2 求编辑距离。。。 opt[i][j]表示word1的前i个字符与word2的前j个字符的editdistance word1[i-1] == word2[j-1] opt[i][j]=opt[i-1][j-1] word1[i-1] != word2[j-1] opt[i][j]=min(opt[i-1][j]+1,opt[i][j-1]+1,opt[i-1][j-1]+1)原创 2013-11-25 10:29:20 · 617 阅读 · 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 · 612 阅读 · 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 · 676 阅读 · 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 · 2483 阅读 · 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 · 736 阅读 · 0 评论 -
Spiral Matrix
难度:1 1Y又失败。。 解法:模拟。。 class Solution { public: const int LEFT=0; const int RIGHT=1; const int DOWN=2; const int UP=3; vector spiralOrder(vector > &matrix) { vectorans; if(matrix.size(原创 2013-11-23 21:59:42 · 565 阅读 · 0 评论 -
Combination Sum II
难度:2 还是DFS裸搜,1Y失败了 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 number in C may only be原创 2013-11-23 20:53:45 · 493 阅读 · 0 评论 -
Combination Sum
难度:2 - -本来想用背包做,发现用来做这题好麻烦,最后还是DFS裸搜了 个人总结:1Y达成 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The s原创 2013-11-23 20:42:49 · 491 阅读 · 0 评论 -
Longest Valid Parentheses
难度:3 求最长的括号匹配的子串 比如 ())()()的括号匹配的子串为(),()()两个,最长的长度为4 解法: 维护一个栈,栈底值为上次匹配失败的位置 首先初始放一个-1入栈代表上次匹配失败的地方为-1 依次扫描字符 若为'(',将位置放入栈中 若为')',若栈中元素大于1个,则代表有'('可匹配,更新最优值,否则更新栈底 显然,对于任意一个部分最长子串,其最后一个字符更新原创 2013-11-18 23:13:44 · 2755 阅读 · 0 评论 -
Palindrome Number
难度:3 值得注意的地方:输入为1000110001,每次删掉最高位和最低位时bit/=100而不是bit/=10,求一个数的最高位的方式也值得注意一下 题意:输入一个int判断是否为回文,不允许转化为字符串进行判断 解法:每次获得数的最高位和最低位判断,快速获得最高位的的方法见代码 class Solution { public: int HighestBit(int x)//4原创 2013-11-13 18:04:52 · 586 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
此题让我体会到我的智商真不怎么样。。 给一个字符串,求一个子串,不包含任何重复的字母 比如abca就有重复的字母a 第一想法是动态规划,但是没有想出来怎么定义状态 看了别人的思路 设置两个索引,head和tail,表示当前所枚举到的集合s[head...tail] 依次增加tail,每增加一个字符,若当前集合中存在,则增加head直到当前集合没有这个字符 这样就可以O(n)复杂度枚举原创 2013-11-12 22:03:30 · 642 阅读 · 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 · 566 阅读 · 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 · 710 阅读 · 0 评论 -
Longest Palindromic Substring
很容易想到O(n^2)的DP做法,不过这题有O(n)做法 表述拙计,还是贴我参考的文章的链接吧 http://blog.youkuaiyun.com/hopeztm/article/details/7932245 讲的比较详细 然后是我的代码 class Solution { public: string longestPalindrome(string s) {原创 2013-11-13 16:30:27 · 637 阅读 · 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 · 757 阅读 · 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 · 756 阅读 · 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 · 1615 阅读 · 0 评论 -
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 · 692 阅读 · 0 评论 -
Unique Paths
难度:2 DP入门题,挺水的 dp[i][j]表示i,j到终点的方法数 dp[i][j]=dp[i+1][j]+dp[i][j+1] 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 e原创 2013-11-24 19:09:55 · 585 阅读 · 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 · 647 阅读 · 0 评论 -
Count and Say
题目:2 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is re原创 2013-11-23 18:30:23 · 616 阅读 · 0 评论 -
Generate Parentheses
难度:2 个人总结:还是有未考虑全面的地方,以及string没有pop_back这个方法吖 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set原创 2013-11-23 14:37:40 · 615 阅读 · 0 评论 -
Merge k Sorted Lists
难度:2 题意: 将多个已排序数组组合成一个 解法: 归并 class Solution { public: ListNode *mergeKLists(vector &lists) { ListNode *ans=NULL,*Back; int n=0; for(int i=0;i<lists.size();i++)原创 2013-11-16 11:59:59 · 649 阅读 · 0 评论 -
Remove Nth Node From End of List
难度:1 删除链表里倒数第n个节点 据说标准算法是两个指针,同时向右扫,相差n-1,当后面的没有后继时就找到了倒数第n个节点 我的算法很简单,从左到右扫一遍,记录个数num,再从头到尾移动num-n次,就找到该节点 题目说尽量只扫一次,我扫了两次, 但是如果我刚才说的标准算法真的是标准算法的话, 会分析复杂度的同学都知道两者的时间复杂度一样的,我的方法相比标准算法更简单明了(一切的前提原创 2013-11-16 10:02:32 · 672 阅读 · 0 评论 -
Divide Two Integers
难度:3 这道题有些意思,不用乘法,除法和模运算实现除法 时间复杂度O(logn) 解法: 我的做法是 除数不断乘以2(加法),直到接近被除数,最后做个处理,注意溢出,轻松愉悦的一道题 class Solution { public: int divide(int argv1, int argv2) { // IMPORTANT: Please原创 2013-11-16 15:53:35 · 577 阅读 · 0 评论 -
Remove Element
删掉一列数中等于elem的所有数,返回新数组的个数 难度:1 看代码吧。。特别容易特别短 class Solution { public: int removeElement(int A[], int n, int elem) { // IMPORTANT: Please reset any member data you declared, as /原创 2013-11-16 14:32:08 · 560 阅读 · 0 评论 -
Implement strStr()
KMP裸题。。 难度:2 class Solution { public: int Fail[400010];//j=fail[i]意为0,,,,,j-1=i-j,,,,,,,i-1 void getFail(char *P) { int m=strlen(P); Fail[0]=0; Fail[1]=0;原创 2013-11-16 14:58:38 · 403 阅读 · 0 评论 -
Swap Nodes in Pairs
难度:2 题意: 给出一个链表,将链表上的相邻的两个节点交换,注意不要交换内容,而是交换指针(如果是交换内容,难度为1) Given 1->2->3->4, you should return the list as 2->1->4->3. 略麻烦,写错了两次,这种指针的交换一定不能凭想象,而要在纸上画 class Solution { public: ListNode原创 2013-11-16 12:30:06 · 592 阅读 · 0 评论 -
Longest Common Prefix
求所有串的最长公共前缀 解法: 枚举每个位置 class Solution { public: string longestCommonPrefix(vector &strs) { if(strs.size()<=0) return ""; int min_len=strs[0].size(); for(int i=1;i<s原创 2013-11-15 14:08:55 · 710 阅读 · 0 评论 -
Same Tree
给两棵树,判断是否相同,很简单 难度评价:2 class Solution { public: bool isSameTree(TreeNode *p,TreeNode *q) { if(p == NULL && q != NULL) return false; if(p != NULL && q == NULL) return fals原创 2013-10-29 11:56:49 · 541 阅读 · 0 评论 -
sqrt(x)
实现sqrt函数,因为输入输出都是int,所以就用二分做了 class Solution { public: int sqrt(int x) { int L=0; int R=x; int ans=0; while(L<=R) { long long mid=(L+R)>>原创 2013-10-29 10:27:08 · 653 阅读 · 0 评论 -
Next Permutation
这题很容易想到解法,不过写代码的时候犯了几个致命的错误,看来1A的能力还是很弱 题意: 求字典序的下一个排列 比如以下序列 1 2 4 5 1 2 5 4 1 4 2 5 1 4 5 2 1 5 2 4 1 5 4 2 解法: 很容易想到从尾至头找到第一个i使得num[i-1] 表示需要从num[i],,,num[n-1]中找最小的一个大于num[i-1]的数来替换num[原创 2013-11-18 21:34:51 · 664 阅读 · 0 评论 -
Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found原创 2013-11-19 19:36:07 · 796 阅读 · 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. If found in the array retur原创 2013-11-19 19:18:58 · 3101 阅读 · 0 评论 -
Add Two Numbers
难度:1 模拟加法。。 class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *ans=NULL; int t=0;//进位 ListNode * ret=NULL; while(l1&&l2)原创 2013-11-22 13:11:01 · 648 阅读 · 0 评论 -
Valid Sudoku
难度:1 个人总结:傻傻地写错两个符号,1Y失败 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character原创 2013-11-23 15:46:08 · 733 阅读 · 0 评论 -
Valid Parentheses
难度:3 个人总结:1Y能力太弱,考虑不周全,基本的数据结构使用不灵活 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the cor原创 2013-11-23 14:10:18 · 597 阅读 · 0 评论 -
Letter Combinations of a Phone Number
难度:1 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. Inpu原创 2013-11-23 13:21:52 · 665 阅读 · 0 评论