- 博客(60)
- 资源 (8)
- 收藏
- 关注
原创 LeetCode Merge Two Sorted Lists
2013年最后一天做题好顺呀ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if(l1==NULL) return l2; if(l2==NULL) return l1; ListNode *p=l1; ListNode *q=l2; ListNode *head=NULL,*s=NULL; while(p&&q)
2013-12-31 20:07:29 500
原创 LeetCode Minimum Path Sum
这题其实也没什么,但是出现莫名的错误倒是学到了一点知识,那就是:二维数组分配只能分配最大504*504,分配505*505就会出现栈溢出。一般分配500*500就够了。int minPathSum(vector > &Grid) { int m = Grid.size(); if(m==0) return 0; int n = Grid[0].size(); if(n==0)
2013-12-31 19:55:29 502
原创 LeetCode Unique Paths II
改一次,没测试,通了。int uniquePathsWithObstacles(vector > &obstacleGrid) { int m = obstacleGrid.size(); if(m==0) return 0; int n = obstacleGrid[0].size(); if(n==0) return 0; if(obstacleGrid[0][0]==1
2013-12-31 19:05:33 506
原创 LeetCode Unique Paths
直接写,不测试,通过。int uniquePaths(int m, int n) { if(m==0||n==0) return 0; int dp[101][101]; //memset(dp,0,101*101*sizeof(int)); //dp[m-1][n-1]=0; for (int i=0;i<m;i++) { dp[i][0] = 1; } for (i
2013-12-31 18:52:39 480
原创 LeetCode Rotate List
要说这题没什么新鲜玩意,我确实第一次没提交过,不是什么大难题,考虑不周到,马虎之类,人生有多少时候我们是跌倒在这里呢?// LeetCode_RotateList.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include using namespace std;struct ListNode { int val; Li
2013-12-31 18:39:59 603
原创 LeetCode Permutation Sequence
这题还有点意思,第一次提交是求的所有排列然后排序选择第k个,超时了,仔细一想确实有更好的方法,以4为例,可以先分四个区间,1*** 有6个,2***有6个,3***有6个,4***有6个,这样就可以根据k判断所在的区间在某个区间定位,这样其他的区间就不用判断了。定位到其中一个区间后,还可以用同样的方法递推下去,比如开始k为15,在第三个区间,第一个元素选3,把3从原来的数组中删除,之后就是从第3个
2013-12-30 22:02:19 545
原创 LeetCode Spiral Matrix II
怎么做了好几个题都没什么含金量呢,刷的感觉vector > generateMatrix(int n) { vector > ret; if (n==0) { return ret; } vector temp; for (int i=0;i<n;i++) { temp.clear(); for (int j=0;j<n;j++) { temp.push_b
2013-12-30 19:38:54 586
原创 LeetCode Length of Last Word
这题就是多想一会呗,感觉没什么,但要是一次提交就过也不容易。int lengthOfLastWord(const char *s) { const char *lastspace = s; if(!*s) return 0; while(*lastspace) lastspace++; int sum = 0; lastspace--; while(lastspace!=s
2013-12-30 19:17:56 516
原创 LeetCode Insert Interval
同合并一样,没什么新鲜的东西vector insert(vector &intervals, Interval newInterval) { vector::iterator iter = intervals.begin(); while(iter!=intervals.end()&&iter->start<newInterval.start) { iter++; } inte
2013-12-30 18:49:41 604
原创 LeetCode Merge Intervals
这题最头疼的是如何给vector中的结构体排序,重载sort中自己的编写的排序函数不行,重载小于操作符不行,后来看人家的,是重载结构体中的()操作符的,可以了,之后没什么,不知道有没有O(n)的方法// LeetCode_MergeIntervals.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #includ
2013-12-29 22:19:20 608
原创 LeetCode Jump Game
有jump game 2 http://oj.leetcode.com/problems/jump-game-ii/ 比简单多了,只是需要考虑用最小的步数就行了,找到结果尽快返回,也就是优化。我的思想是:设置一个能达到的最远点,首先判断当前点能不能到达,然后看能都更新最远点,如果最远点超过了最后的索引,直接返回真。如下:bool canJump(int A[], int n) { if
2013-12-29 21:02:26 603
原创 LeetCode Spiral Matrix
记忆清晰呀,高德笔试题,可惜第一次还是提交失败,考虑不周到所致。就是在while里不能只判断横排或者竖排, 必须横竖都判断。// LeetCode_MaximumSubarray.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include using namespace std;int maxSubArray(int A
2013-12-29 20:46:56 527
原创 LeetCode Maximum Subarray
面试题,做过无数次了,不废话。int maxSubArray(int A[], int n) { int curmax = A[0]; int max = curmax; for (int i=1;i<n;i++) { if (curmax + A[i]>A[i]) curmax = curmax + A[i]; else curmax = A[i]; if (
2013-12-29 19:38:50 572
原创 N-Queens II
按照 I 中的做法只是把返回结果变了一下,提交出现超时,这有点意思。想了想,也不能不递归呀,于是做了一些优化,既是在每次求下一值之前(即进行递归之前)先判断当前的布局是否合理,这样就能大大减少递归的次数,以十次为例,这样的提升一下子就能感觉到。bool isvalidEachLine(int *arr,int n,int j){ for (int i=0;i<j;i++) { if
2013-12-29 19:19:15 881
原创 LeetCode N-Queens
还记得第一次遇见这题时,本科刚学数据结构时,有个周末一人拎个笔记本在林大二教调了整整一小天,那是八皇后的问题,只记得第一次调通的兴奋,至于内容全忘了。这题刚开始理解错了,以为皇后只是横竖能走,斜着不能走,所以刚开始的思路是用一个一维数组,下标是皇后所在的行,值是所在的列,这样类似求全排列的方式,应该是有多少种全排列就有多少种布局的方式,当然这是错的,不过思路仍然可以用,只是在放入解的时候多判断一下
2013-12-29 10:31:31 563
原创 LeetCode Pow(x, n)
不得不说我又输了,第一次提交失败了,想的太简单,忘了负数和0 的情况。这题就这一点,一旦知道自己原来错了,这题就没有什么可以拟补失误的机会了。// Pow(x, n).cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include using namespace std;double pow(double x, int ni) { doubl
2013-12-28 19:17:50 533
原创 LeetCode Anagrams
这题关键是明白题意,题意是说去除那些在给定的数组里没有其anagrams的那些值,凡是有anagram的搜输出,甚至顺序都不重要,之后感觉没什么了。vector anagrams(vector &strs) { int lenstr = strs.size(); vector ret; if(lenstr == 0) return ret; vector strtemp(strs)
2013-12-28 19:00:20 588
原创 LeetCode Rotate Image
没什么东西,就是麻烦,容易出错,对于每个点找其旋转后的位置,然后一个一个替换,一层套一层的。// LeetCode_RotateImage.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include using namespace std;void getlastij(int leftup,int rightdown,i
2013-12-27 22:03:25 587
原创 LeetCode Permutations II
没什么说的,剑指offer上也有。bool isSwap(vector &num,int i,int j){ for (int k=i;k<j;k++) { if(num[k]==num[j]) return false; } return true;}void permute_res_unique(vector &num,int i,int n,vector &one
2013-12-26 21:37:10 529
原创 LeetCode Permutations
做过不知道多少遍了,通过了,也就没想太多,有没有不递归的呢?// LeetCode_Permutations.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include using namespace std;void permute_res(vector &num,int i,int n,vector &onevalu
2013-12-26 21:12:58 636
原创 LeetCode Jump Game II
第一次 递归int jump_res(int *arr,int n,int cur){ if (cur>=n-1) return 0; int minv = Inf; for (int i=1;i<=arr[cur];i++) { int tempv = 1+jump_res(arr,n,cur+i); if (minv>tempv) { minv = temp
2013-12-26 20:27:27 1047 2
原创 LeetCode Wildcard Matching
前面有一道http://oj.leetcode.com/problems/regular-expression-matching/ 和这题特别像的。首先我是用回溯法,肯定时间是不够的,不知道有没有错误,leetCode的编译器错误也说是超时,如下:bool isMatch(const char *s, const char *p) { if (*p==0) { return *s=
2013-12-26 16:51:23 653
原创 LeetCode Multiply Strings
面试那时候听说过大整数乘法,当时加法都够呛,要真碰到乘法肯定吓尿了,这里碰到了,想了想,写了写,也不过如此。// LeetCode_MultiplyStrings.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include using namespace std;string add(const st
2013-12-24 19:16:48 587
原创 LeetCode Trapping Rain Water
accept?真是一阵惊喜,没想到这方法能通过,而且反应了有一会儿。先回顾一下前面的相关的题目http://oj.leetcode.com/problems/container-with-most-water/ http://blog.csdn.net/littlestream9527/article/details/17174075 这题当然不一样了,首先每个元素本身都要占据面积的,其
2013-12-20 22:11:47 525
原创 LeetCode First Missing Positive
这题不是自己做的,也没想出来,估计要我自己想得想个一年半载的,也够呛。看了别人的做法真是巧妙呀。不管别的,把能放得放到应该放的位置上,当然之前不能覆盖元素,所以要保存原来的值,然后还要对保存的值进行同样处理,看看它能放到哪,因为元素的值都是正整数,所以可以直接作为索引了,这样才能保证O(n)的时间复杂度。因为每次放到正确的位置的值下次就不用放了,所以不会重复无用的计算,这样既是for里有while
2013-12-20 20:30:45 475
原创 LeetCode Combination Sum II
这个能不能不递归呢?void recursiveSum2(vector &candidates,int target,vector &res,vector > &ret,int n){ if (n==candidates.size()) { return ; } if (candidates[n]==target) { res.push_back(candidates[n
2013-12-20 18:40:32 523
原创 LeetCode Combination Sum
这题以前见过,在哪忘了。也是用的递归,不递归行不行?明天再想想吧// LeetCode_CombinationSum.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include using namespace std;void recursiveSum(vector &candidates,int t
2013-12-19 22:14:44 503
原创 LeetCode Count and Say
string findnext(string str){ int i=0; int lenstr = str.length(); int sumcur = 0; string ret = ""; string curstr = ""; while(i<lenstr) { curstr = ""; curstr += str[i]; sumcur = 0; int j
2013-12-19 18:21:26 572
原创 LeetCode Valid Sudoku
点 相当于任何可能的值,分别判断横竖,还有3*3的小格子。// LeetCode_ValidSudoku.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include using namespace std;bool isValidSudoku(vector > &board) { map mp;
2013-12-17 19:35:51 1136
原创 LeetCode Search Insert Position
这题马虎了,提交了好几次,最简单的二分查找int searchInsert(int A[], int n, int target) { int i=0,j=n-1; int mid; if (target<A[0]) return 0; while(i<=j) { mid = (i+j)>>1; if (A[mid]==target) return mid; e
2013-12-17 18:35:18 476
原创 LeetCode Search for a Range
就是二叉查找。vector searchRange(int A[], int n, int target) { int i=0,j=n-1; vector ret; while(i<=j) { int mid = (i+j)>>1; if (A[mid]>=target) j = mid - 1; else i = mid + 1; } if (A[i]==
2013-12-17 15:40:13 503
原创 LeetCode Search in Rotated Sorted Array
这题和剑指offer上的旋转数组的最小值类似,此题应该比那题还简单,因为没有重复的元素。int bisearch(int *arr,int start,int end,int value){ while(start<=end) { int mid = (start + end)>>1; if (arr[mid]==value) { return mid; } e
2013-12-17 15:16:24 463
原创 LeetCode Longest Valid Parentheses
想了好久一直想用动态规划,还是没规划出来,扫了一眼别人的解法,思路顿开。就是多了另一个栈来存放无法匹配的括号位置,然后找相邻的位置最远的即可,因为要遍历栈所以用了容器。int longestValidParentheses(string s) { vector stkbracket; vector stkindex; int maxsum=0; int cursum=0; stkin
2013-12-17 10:48:49 544
原创 LeetCode Next Permutation
还记得怎么求字符串的排列吗?void Permutation(vector &num,int i){ if (i==num.size()) { for (int k=0;k<num.size();k++) { cout<<num[k]<<" "; } cout<<endl; return ; } for (int j=i;j<num.size();j++)
2013-12-16 16:43:47 601
原创 LeetCode Substring with Concatenation of All Words
一看这道题就知道不是一个简单的题,不想一下子就放弃,就写了写,第一个的想法是找每个字符串出现的位置,然后判断位置是否连续(既包含所有的字符串),但是没有用map,也没有记录每个单词出现的次数,结构之混乱,思维之复杂,弄了好久调通了,时间限制。烂代码贴上也无妨,不要学就是了。vector findSubstring(string S, vector &L) { int lenL = L.siz
2013-12-15 21:59:32 535
原创 LeetCode Divide Two Integers
以前听过这题,也思考过,但到自己做时问题才暴漏出来,最终还是提交别人的通过的,哎,后悔呀,在思考一点是不是我也可以想到呢(我有无数次这样的悔恨,放弃太早了)?下面的是我的方法,也是用了移位运算,时间复杂度也是lg(n)还是没通过,可见leetCode这道题还是挺严的,思路是这样的:先判断结果集所在的区间,然后二分查找,直到找到结果。如下:int divide(int dividend, i
2013-12-13 10:25:22 574
原创 LeetCode Implement strStr()
这题就是KMP算法,看了几遍还是没能想起来,又拿起书看了看,写了程序,bug不少,调了好久,通了。char *strStr(char *haystack, char *needle) { if (haystack==NULL||needle==NULL) { return NULL; } int lenneedle=0; char *p=needle; while(*p) {
2013-12-12 21:10:00 550
原创 LeetCode Remove Element
int removeElement(int A[], int n, int elem) { int i=0,j=0; while(j<n&&A[j]!=elem) { i++; j++; } if (j==n) return n; while(j<n) { while(j<n&&A[j]==elem) j++; if (j<n) { A[i++]=A[j
2013-12-11 21:53:48 546
原创 LeetCode Remove Duplicates from Sorted Array
简单的一道小题,考虑不全提交了好几次。int removeDuplicates(int A[], int n) { int i=0,j=0; if (n==0) return 0; while(i+1<n&&A[i]!=A[i+1]) { i++; j++; } if (i==n-1) return n; //i++; j++; while(j<n) {
2013-12-11 21:42:24 518
程序员面试题精选100题.doc
2013-01-11
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人