leetcode
文章平均质量分 79
xjwowangran
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
sort list
题目如下: Sort a linked list in O(n log n) time using constant space complexity. nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。以下是用归并排序的代码: #include "stdafx.h" #include using nam转载 2014-04-25 12:08:17 · 287 阅读 · 0 评论 -
Combination, subset
Subset class Solution { public: vector > subsets(vector &S) { vector > result; if(S.size() == 0) return result; sort(S.begin(), S.end()); int n = S.size(); vector each; resul转载 2014-08-20 16:02:00 · 386 阅读 · 0 评论 -
Add two numbers-list,string
Add Two Numbers 主要考虑两个链表相等位数的和,shengxia /* 提交多次才成功,主要是考虑,两个链表不一样长度 1. 12 7990,有数有进位 2. 1 99,无数有进位 5 5, */ class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2转载 2014-08-13 19:48:50 · 361 阅读 · 0 评论 -
sum 3- 4
3 Sum vector > threeSum(vector &num) { vector > result; if(num.size() < 3) return result; sort(num.begin(),num.end()); const int target = 0; auto last = num.end(); for(auto a = num.begin();转载 2014-08-13 16:15:48 · 380 阅读 · 0 评论 -
Pascal's Triangle , Pascal's Triangle II
Pascal's Triangle class Solution { public: vector > generate(int numRows) { vector > result; if(numRows <= 0) return result; vector row1(1,1); vector row2(2,1); if(numRows >= 1) re原创 2014-09-09 17:21:26 · 360 阅读 · 0 评论 -
Plus One
class Solution { public: vector plusOne(vector &digits) { if(digits.size() == 0) return digits; int carry = 0; for(int i = digits.size()-1; i >=0 ; -- i) //如果使用的是size_t就是无符号的,如果减一了,那么就是一个大数原创 2014-09-09 20:59:50 · 302 阅读 · 0 评论 -
Minimum Path Sum
Minimum Path Sum class Solution { public: vector > f; //备忘录法,把每次计算的值保存下来 int getOrupdate(vector > &grid, int m, int n) { if(m < 0 || n < 0) retu转载 2014-09-11 15:56:29 · 391 阅读 · 0 评论 -
BT 3 validate, unique, recover
Validate Binary Search Tree 浪费太多时间思考这个问题,结果知道了左孩子的右孩子必须比爷爷小,右孩子的左孩子必须比爷爷大,可是这么递归是超时的,实际上是归纳成先序遍历,递归判断,递归时传入两个参数,一个是左界,一个是右界,节点的值必须在两个界的中间,同时在判断做子树和右子树时更新左右界。 class Solution { public: bool Va转载 2014-07-28 15:09:08 · 340 阅读 · 0 评论 -
Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of li转载 2014-09-11 19:32:23 · 289 阅读 · 0 评论 -
Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to ri转载 2014-09-11 20:27:16 · 286 阅读 · 0 评论 -
BT search 2 level order, Construct Binary Tree from Preorder and Inorder Traversal
Binary Tree Level Order Traversal class Solution { public: vector > levelOrder(TreeNode *root) { vector > result; vector temp; queue myQueue; TreeNode* p转载 2014-07-22 21:00:18 · 532 阅读 · 0 评论 -
二叉查找树的个数,BT search 1, 二叉树遍历的非递归形式,Flatten Binary Tree to Linked List
主要是卡特兰特s class Solution { public: int numTrees(int n) { if(n == 0) return 0; int* num = new int[n+1]; num[0] = 1; num[1] = 1; for(转载 2014-07-06 14:32:56 · 506 阅读 · 0 评论 -
BST——depth, same,symmetric,path sum
bool check(TreeNode* left, TreeNode* right) { if(left == NULL && right == NULL) return true; if(left == NULL ||right == NULL) return false; if(left->val == right->val) return check(left->left转载 2014-07-22 20:56:11 · 427 阅读 · 0 评论 -
same tree & depth in the tree
1.same tree /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };转载 2014-03-28 19:14:54 · 546 阅读 · 0 评论 -
Permutation, next Permutation, Permutation sequence
class Solution { public: void nextPermutation(vector &num) { if(num.empty()) return; size_t j = num.size() - 1; size_t i = j - 1; while(i >= 0 && j > 0 && num[j] <= num[i]) { -- j; -- i;转载 2014-08-04 15:54:53 · 327 阅读 · 0 评论 -
insertion sort list
if(head == NULL || head->next == NULL) return head; else { ListNode* cur = head->next; ListNode* cprior = head; for(;cur != NULL; cprior = cur, cur = cur->next) { ListNode* index = he转载 2014-04-25 17:18:03 · 270 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL)转载 2014-07-17 22:02:16 · 344 阅读 · 0 评论 -
N queens
class Solution { public: bool isValid(vector& board, int n,int x, int y) { int i =0, j =0; for(i=0; i < x; ++ i) { if(board[i][y] == 'Q')//只可能是x之前的行,在同一列有相同的元素 return false; } for(i = 0; i转载 2014-07-20 17:00:09 · 311 阅读 · 0 评论 -
Remove
Remove Duplicates from Sorted Array class Solution { public: int removeDuplicates(int A[], int n) { if(n<=0) return 0; if(n==1) return 1; int i=0,j=1; //遍历一边又相同的就用后面的元素覆盖 whil转载 2014-07-21 18:14:45 · 355 阅读 · 0 评论 -
cycle,reverse,rotate linked list
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycl转载 2014-07-17 21:37:30 · 291 阅读 · 0 评论 -
BST : sorted list (array) to BST
Convert Sorted Array to Binary Search Tree转载 2014-07-23 10:29:12 · 349 阅读 · 0 评论 -
array 2 map
Longest Consecutive Sequence转载 2014-07-31 16:02:04 · 315 阅读 · 0 评论 -
KMP算法
// KMP.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include using namespace std; //输入:src, slen主串 //输入:pstr, plen模式串 //输入:next KMP算法中的next函数值数组 //修正后的求next数组各值的函数代码 void GetNext(转载 2014-03-19 21:08:20 · 278 阅读 · 0 评论 -
string 1
class Solution { public: bool isPalindrome(string s) { if(s.empty() ) return true; size_t i = 0; size_t j = 0; string s1 = ""; for(; i < s.size(); ++ i) { if('A'= s[i] ) { s1 +=转载 2014-08-04 21:28:22 · 322 阅读 · 0 评论 -
array 二分查找
Search in Rotated Sorted Array转载 2014-07-31 16:03:07 · 416 阅读 · 0 评论 -
1 Longest Palindromic Substring
wewr转载 2014-03-16 14:46:55 · 251 阅读 · 0 评论 -
Generate Parentheses,Rotate Image
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()转载 2014-09-09 21:14:50 · 325 阅读 · 0 评论
分享