算法题
文章平均质量分 58
yxsh01
这个作者很懒,什么都没留下…
展开
-
leetcode:Word Break
class Solution { public: bool wordBreak(string s, unordered_set &dict) { int n = s.length(); if(n == 0) return true; bool * canBreak = new bool[n]; mems原创 2014-11-18 21:54:11 · 277 阅读 · 0 评论 -
morris 树的中序遍历(O(1)空间 O(n)时间)
#include using namespace std; struct TreeNode { int val; TreeNode * left; TreeNode * right; TreeNode(int v):val(v),left(NULL),right(NULL){} }; void InorderTraversal(TreeNode * root) { if(ro原创 2014-11-20 22:57:32 · 472 阅读 · 0 评论 -
leetcode:Search for a Range
class Solution { public: vector searchRange(int A[], int n, int target) { vector result; result.push_back(leftBound(A,n,target)); result.push_back(rightBound(A,n,target));原创 2014-11-27 22:24:54 · 414 阅读 · 0 评论 -
leetcode:Search Insert Position
class Solution { public: int searchInsert(int A[], int n, int target) { int ans = n; int begin = 0, end = n-1; while(begin <= end) { int mid = begin + (原创 2014-11-27 22:30:58 · 381 阅读 · 0 评论 -
leedcode:Search in Rotated Sorted Array
class Solution { public: int search(int A[], int n, int target) { int begin = 0, end = n-1; int ans = -1; while(begin <= end) { int mid = begin + (end -原创 2014-11-27 21:22:07 · 370 阅读 · 0 评论 -
leetcode:Valid Sudoku
class Solution { public: bool isValidSudoku(vector > &board) { int n = board.size(); if(n != 9) return false; int m = board[0].size(); if(m != 9)原创 2014-11-27 22:58:29 · 402 阅读 · 0 评论 -
leetcode:Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",原创 2014-11-26 22:36:21 · 395 阅读 · 0 评论 -
leetcode:First Missing Positive
class Solution { public: int firstMissingPositive(int A[], int n) { for(int i =0 ;i < n;i++) { if(A[i] 0) { int temp = A[i];原创 2014-11-29 15:25:51 · 361 阅读 · 0 评论 -
leetcode:Trapping Rain Water
class Solution { public: int trap(int A[], int n) { if(n <= 0) return 0; int * water = new int[n]; int current =0; for(int i=0 ;i<n;i++) {原创 2014-11-29 15:31:34 · 390 阅读 · 0 评论 -
leetcode:Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unk原创 2014-11-03 22:40:24 · 468 阅读 · 0 评论 -
Leetcode:Find Minimum 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). Find the minimum element. You may assume no duplicate exists in原创 2014-11-03 22:16:00 · 474 阅读 · 0 评论 -
leetcode:Linked List Cycle II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *de原创 2014-11-19 21:21:00 · 377 阅读 · 0 评论 -
leetcode:Candy
class Solution { public: int candy(vector &ratings) { int n = ratings.size(); int c = 1; int * num = new int[n]; num[0] = 1; for(int i = 1; i < n; i++)原创 2014-11-18 22:02:39 · 366 阅读 · 0 评论 -
leetcode:Copy List with Random Pointer
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL),原创 2014-11-18 22:32:00 · 402 阅读 · 0 评论 -
leetcode:Min Stack
class MinStack { public: stack ele; stack minStack; void push(int x) { int minVal = x; if(minStack.empty() == false) minVal = min(x, minStack.top());原创 2014-11-18 21:34:26 · 415 阅读 · 0 评论 -
二叉树的最小公共祖先(一次查询)
#include using namespace std; struct Node { int val; Node * left; Node * right; Node(int v):val(v),left(NULL),right(NULL){} }; Node * lca(Node * a, Node * b, Node * root, int & appNum ) { i原创 2014-11-18 21:19:18 · 894 阅读 · 0 评论 -
leetcode:Binary Tree Postorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Soluti原创 2014-11-18 21:44:35 · 401 阅读 · 0 评论 -
leetcode:Reorder List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorder原创 2014-11-18 22:17:39 · 379 阅读 · 0 评论 -
最小公共祖先(LCA)离线算法_Tarjan c++实现
#include #include #include #include using namespace std; struct Node { int val; vector chlidren; Node(int v):val(v){} }; class UnSet { public: UnSet(int n):capacity(n) { father = new int原创 2014-11-18 20:40:57 · 1244 阅读 · 0 评论 -
leetcode:Binary Tree Preorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Soluti原创 2014-11-19 21:25:34 · 404 阅读 · 0 评论 -
leetcode:Single Number II
class Solution { public: int singleNumber(int A[], int n) { int sign = 0; long long ret = 0; for(int i = 0; i < n ;i++) { long long c = A[i];原创 2014-11-19 20:33:54 · 386 阅读 · 0 评论 -
leetcode:Combination Sum II
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 used once in the combina原创 2014-11-29 15:19:47 · 387 阅读 · 0 评论