- 博客(103)
- 收藏
- 关注
原创 Longest Valid Parentheses
class Solution { public: int longestValidParentheses(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function stack stk; vector v(s
2013-07-31 20:00:36 348
原创 Merge k 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
2013-07-31 15:50:04 357
原创 Generate Parentheses
考虑的几个不符合要求的case)))(((())(()在每次加入新的括号前,保证当前的是合法的。每次加入新括号前检查已经使用的左括号大于等于已经使用的右括号数量class Solution { public: vector generateParenthesis(int n) { // Start typing your C/C++ solution below
2013-07-31 15:04:35 367
原创 Trapping Rain Water
O(n) solution. for each bar, find the max height bar on the left and right. then for this bar it can hold min(max_left, max_right) - height class Solution { public: int trap(int A[], int n) {
2013-07-31 14:47:21 350
原创 Container With Most Water
class Solution { public: int maxArea(vector &height) { // Start typing your C/C++ solution below // DO NOT write int main() function int left=0,right=height.size()-1,maxA=0
2013-07-31 14:03:41 389
原创 Remove Nth Node From End of List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *re
2013-07-31 13:10:15 291
原创 Valid Parentheses
class Solution { public: bool isValid(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function stack stk; for(int i=0;i<s.length();
2013-07-31 12:29:31 309
原创 Combination Sum
class Solution { public: vector > combinationSum(vector &candidates, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function vector vec;
2013-07-30 22:22:53 357
原创 Combination Sum II
class Solution { public: vector > combinationSum2(vector &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function vector vec;
2013-07-30 22:00:00 332
原创 First Missing Positive
注意的一点是在交换A[i],A[A[i]-1]后,记得--i。 不执行--i的反例 3 4 -1 1 -1 4 3 1 i=0 -1 1 3 4 i=1 -1 1 3 4 i=2 -1 1 3 4 i=3 class Solution { public: int firstMissingPositive(int A[], int n) {
2013-07-30 17:56:53 258
原创 Count and Say
class Solution { public: string countAndSay(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function string s="1",tmp; while(--n){
2013-07-30 15:12:01 444
原创 Search Insert Position
class Solution { public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function int pos=BinarySearchLe
2013-07-30 13:18:14 302
原创 Search for a Range
class Solution { public: vector searchRange(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function int left=BinarySearc
2013-07-30 12:15:28 313
原创 Letter Combinations of a Phone Number
class Solution { vector board; vector ret; void DFS(string &d,int cur,string str){ if(cur==d.length()){ ret.push_back(str); return; } string
2013-07-29 21:56:09 370
原创 4Sum
需要注意的几个case [0,0,0,0,0,0] 0 [-1,-1,0,0,1,1,2,2] class Solution { public: vector > fourSum(vector &num, int target) { // Start typing your C/C++ solution below // DO NOT w
2013-07-29 18:53:53 327
原创 3Sum Closest
class Solution { public: int threeSumClosest(vector &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(num.begin(),num.e
2013-07-29 18:14:31 322
原创 Longest Common Prefix
class Solution { public: string longestCommonPrefix(vector &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function int LCP=0,i,j; str
2013-07-29 12:27:54 310
原创 Palindrome Number
class Solution { public: bool isPalindrome(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function int ret=0,y=x; while(x){
2013-07-29 10:43:24 318
原创 Reverse Integer
class Solution { public: int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function int ret=0; while(x){
2013-07-29 09:55:37 344
原创 Jump Game II
class Solution { public: int jump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector vec(n,INT_MAX); int rmax=0;
2013-07-29 09:38:05 422
原创 Next Permutation
class Solution { public: void nextPermutation(vector &num) { // Start typing your C/C++ solution below // DO NOT write int main() function int len=num.size(),i,j; i
2013-07-28 20:07:12 286
原创 Anagrams
class Solution { public: vector anagrams(vector &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function vector ans; map > dict;
2013-07-28 19:57:59 326
原创 Rotate Image
算法: 1.由外至内旋转90° 例如 将1用21取代,21用25取代,25用5取代,5用1取代 ... class Solution { public: void rotate(vector > &matrix) { // Start typing your C/C++ solution below // DO NOT write int
2013-07-28 19:30:25 376
原创 Permutations
class Solution { public: vector > permute(vector &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector > vec; int len=num.siz
2013-07-28 09:14:39 377
原创 Permutations II
(1) 字典序法 对给定的字符集中的字符规定了一个先后关系,在此基础上按照顺序依次产生每个排列。 [例]字符集{1,2,3},较小的数字较先,这样按字典序生成的全排列是:123,132,213,231,312,321。 生成给定全排列的下一个排列 所谓一个的下一个就是这一个与下一个之间没有字典顺序中相邻的字符串。这就要求这一个与下一个有尽可能长的共同前缀,也即变化限制在尽可能短的
2013-07-28 09:12:53 324
原创 Pow(x, n)
几个trick 1. n为负数的时候 2.int类型 负数转化为正数时,越界,需采用long long 3.O(n)算法大数据TLE,需用O(log(n))算法。 class Solution { public: double pow(double x, int n) { // Start typing your C/C++ solution below
2013-07-27 23:47:55 295
原创 N-Queens II
class Solution { int upper; int ret; public: int totalNQueens(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function upper=(1<<n)-1
2013-07-25 21:31:08 324
原创 N-Queens
http://www.matrix67.com/blog/archives/266 class Solution { public: int upper; vector > ret; vector > solveNQueens(int n) { // Start typing your C/C++ solution below /
2013-07-25 21:28:05 343
原创 Maximum Subarray
class Solution { public: int maxSubArray(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int ans=0,sum=0; if(n==0)re
2013-07-25 13:30:38 275
原创 Spiral Matrix
class Solution { public: vector spiralOrder(vector > &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function vector vec; int row=ma
2013-07-25 11:36:14 330
原创 Jump Game
class Solution { public: bool canJump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int last=0; if(n>1&&A[0]==0)re
2013-07-25 11:29:25 351
原创 Merge Intervals
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ bool cm
2013-07-25 11:13:12 264
原创 Insert Interval
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ class So
2013-07-25 10:20:39 343
原创 Length of Last Word
class Solution { public: int lengthOfLastWord(const char *s) { // Start typing your C/C++ solution below // DO NOT write int main() function int len=strlen(s),p=len-1,cnt=0
2013-07-24 18:54:02 302
原创 Spiral Matrix II
class Solution { public: vector > generateMatrix(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector col(n,0); ve
2013-07-24 15:59:42 276
原创 Permutation Sequence
康拓展开实例 {1,2,3,4,...,n}表示1,2,3,...,n的排列如 {1,2,3} 按从小到大排列一共6个。123 132 213 231 312 321 。 代表的数字 1 2 3 4 5 6 也就是把10进制数与一个排列对应起来。 他们间的对应关系可由康托展开来找到。 如我想知道321是{1,2,3}中第几个大的数可以这样考虑 : 第一位是3,当第一位的数小于3时,那排列
2013-07-24 15:41:33 310
原创 Rotate List
class Solution { public: ListNode *rotateRight(ListNode *head, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function ListNode *h=new ListNo
2013-07-24 14:53:56 304
原创 Unique Paths
class Solution { public: int uniquePaths(int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector col(n+1,0); vector> Gr
2013-07-24 10:55:07 456
原创 Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector > &obstacleGrid) { // Start typing your C/C++ solution below // DO NOT write int main() function int row=obstac
2013-07-24 10:34:13 321
原创 Minimum Path Sum
class Solution { public: int minPathSum(vector > &grid) { // Start typing your C/C++ solution below // DO NOT write int main() function int row=grid.size(); if(row=
2013-07-24 10:09:10 357
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人