- 博客(177)
- 收藏
- 关注
原创 LeetCode - 108 Convert Sorted Array to Binary Search Tree
class Solution {public: TreeNode *fun(vector &num,int i,int j) { if(i>j){ return NULL; } else{ int mid=(i+j)/2; TreeNode *t=new Tre
2015-09-21 15:35:19
255
原创 LeetCode - 98 Validate Binary Search Tree
class Solution {public: bool isValidBST(TreeNode* root) { if(root==NULL){ return true; } vector res=inOrderTraverse(root); for(auto it=res.begin();it<r
2015-09-17 19:06:12
217
原创 LeetCode - 110 Balanced Binary Tree
class Solution {public: int depth(TreeNode *p){ if(p==NULL){ return 0; } int a=depth(p->left); int b=depth(p->right); if(a==-1||b==-1||fabs(a-b
2015-08-17 23:56:01
237
原创 LeetCode - 257 Binary Tree Paths
class Solution {public: string make_path(stack st){ string s,temp; TreeNode *pre=st.top()->left; while(!st.empty()){ TreeNode *cur=st.top(); if(cur
2015-08-16 19:23:30
411
原创 LeetCode - 219 Contains Duplicate II
class Solution {public: bool containsNearbyDuplicate(vector& nums, int k) { map> f; for(int i=0;i<nums.size();i++){ f[nums[i]].push_back(i); } for(auto
2015-08-16 18:01:07
198
原创 LeetCode - 236 Lowest Common Ancestor of a Binary Tree
class Solution {public: stack postOrderSearch(TreeNode *root,TreeNode *p){ stack res; stacks; if(root==NULL){ return res; } TreeNode *pre=root;
2015-08-10 16:32:11
207
原创 LeetCode - 242 Valid Anagram
class Solution {public: bool isAnagram(string s, string t) { int a[26]{},b[26]{},i; if(s.size()!=t.size()){ return false; } for(i=0;i<s.size();i++){
2015-08-10 10:00:00
212
原创 LeetCode - 118 Pascal's Triangle
class Solution {public: vector> generate(int numRows) { vector> res; for(int i=0;i<numRows;i++){ vector cur{1}; if(!i){ res.push_back(cur);
2015-07-20 16:12:13
204
原创 LeetCode - 226 Invert Binary Tree
class Solution {public: TreeNode* invertTree(TreeNode* root) { stack s; s.push(root); while(!s.empty()){ TreeNode *cur=s.top(); s.pop();
2015-07-20 16:03:33
167
原创 LeetCode - 231 Power of Two
class Solution {public: bool isPowerOfTwo(int n) { return (n>0)&&(!(n&(n-1))); }};
2015-07-20 04:19:07
241
原创 LeetCode - 228 Summary Ranges
class Solution {public: vector summaryRanges(vector& nums) { vector res; if(nums.empty()){ return res; } int start=nums[0],end=start; for(int i
2015-07-20 04:10:21
210
原创 LeetCode - 234 Palindrome Linked List
class Solution {public: bool isPalindrome(ListNode* head) { int count=0; ListNode *p=head; while(p!=NULL){ count++; p=p->next; } in
2015-07-20 03:58:54
234
原创 LeetCode - 36 Valid Sudoku
class Solution {public: bool isValidSudoku(vector>& board) { int i,j,m,n; for(i=0;i<9;i++){ unordered_set s; for(j=0;j<9;j++){ if(board[i][
2015-07-20 03:22:05
210
原创 LeetCode - 38 Count and Say
class Solution {public: string countAndSay(int n) { string res="1"; while(--n){ res=say(res); } return res; } string say(string s){ str
2015-07-20 01:55:52
232
原创 LeetCode - 223 Rectangle Area
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int x=0,y=0; if(A<=E){ x=min(C-E,G-E); } else{
2015-07-19 23:37:37
359
原创 LeetCode - 235 Lowest Common Ancestor of a Binary Search Tree
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { TreeNode *cur=root; while(1){ if(p==cur||q==cur){ re
2015-07-19 22:50:39
210
原创 LeetCode - 55 Jump Game
class Solution {public: bool canJump(vector& nums) { int farest=0; for(int i=0;i<nums.size();i++){ if(i+nums[i]>=nums.size()-1){ return true;
2015-07-19 22:24:20
232
原创 LeetCode - 232 Implement Queue using Stacks
class Queue {public: // Push element x to the back of queue. void push(int x) { s1.push(x); reverse(s1,s2); } // Removes the element from in front of queue. void
2015-07-19 21:40:26
216
原创 LeetCode - 155 Min Stack
class MinStack {public: void push(int x) { if(s.empty()){ MinS.push(x); } else{ MinS.push(min(MinS.top(),x)); } s.push(x); }
2015-07-19 21:15:03
209
原创 LeetCode - 232 Implement Queue using Stacks
class Stack {public: // Push element x onto stack. void push(int x) { q1.push(x); } // Removes the element on top of the stack. void pop() { if(!q1.empty()){
2015-07-19 20:11:25
155
原创 LeetCode - 238 Product of Array Except Self
class Solution {public: vector productExceptSelf(vector& nums) { vector res; int prd=1; for(int i=0;i<nums.size();i++){ prd*=nums[i]; res.push_bac
2015-07-18 23:07:44
210
原创 LeetCode - 237 Delete Node in a Linked List
class Solution {public: void deleteNode(ListNode* node) { while(node->next!=NULL){ node->val=node->next->val; if(node->next->next==NULL){ node->nex
2015-07-15 12:44:40
199
原创 LeetCode - 96 Unique Binary Search Trees
class Solution {public: int numTrees(int n) { int *a=new int[n+1](); a[0]=1,a[1]=1; for(int i=2;i<n+1;i++){ int sum=0; for(int j=1;j<=i;j++){
2015-06-05 19:48:54
203
原创 LeetCode - 198 House Robber
class Solution {public: int rob(vector &num) { if(num.empty()){ return 0; } int a[2]; a[0]=num[0]; if(num.size()==1){ return a[0];
2015-06-05 15:56:33
203
原创 LeetCode - 203 Remove Linked List Elements
class Solution {public: ListNode* removeElements(ListNode* head, int val) { while(head!=NULL&&head->val==val){ head=head->next; } ListNode* cur=head; w
2015-05-28 15:38:47
184
原创 LeetCode - 34 Search for a Range
class Solution {public: int binary_search_begin(vector& nums,int b,int e,int p){ if(b==e){ if(nums[b]==p){ return b; } else{
2015-05-28 15:23:06
286
原创 LeetCode - 56 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 int
2015-05-28 14:11:21
235
原创 LeetCode - 33 Search in Rotated Sorted Array
class Solution {public: int binary_search_bigger_than_next(vector& nums,int b,int e){ if(b==e&&nums[b]>nums[b+1]){ return b; } else{ int mid=(b+e)/
2015-05-27 00:55:00
218
原创 LeetCode - 74 Search a 2D Matrix
class Solution {public: int binary_search_1(vector>& matrix,int b,int e,int p){ if(b==e){ if(p>=matrix[b][0]){ return b; } else{
2015-05-26 20:44:35
265
原创 LeetCode - 35 Search Insert Position
class Solution {public: int binary_search(vector& nums,int b,int e,int p){ if(b==e){ if(nums[b]<p){ return b+1; } else{
2015-05-26 20:00:32
190
原创 LeetCode - 217 Contains Duplicate
class Solution {public: bool containsDuplicate(vector& nums) { set s; for(auto it=nums.begin();it!=nums.end();it++){ s.insert(*it); } return s.size()==
2015-05-26 00:17:41
175
原创 LeetCode - 205 Isomorphic Strings
class Solution {public: bool isIsomorphic(string s, string t) { unordered_map m1; unordered_map m2; for(int i=0;i<s.size();i++){ auto p=m1.find(s[i]);
2015-05-14 16:56:53
177
原创 LeetCode - 73 Set Matrix Zeroes
class Solution {public: void setZeroes(vector>& matrix) { int m=matrix.size(),n=matrix[0].size(),i,j,k; int MAX=99999; for(i=0;i<m;i++){ for(j=0;j<n;j++){
2015-05-13 17:50:07
148
原创 LeetCode - 122 Best Time to Buy and Sell Stock II
class Solution {public: int maxProfit(vector &prices) { int profit=0; int n=prices.size(); prices.push_back(0); int start=-1; for(int i=0;i<n;i++){
2015-05-13 17:36:04
197
原创 LeetCode - 78 Subsets
class Solution {public: vector> subsets(vector& nums) { sort(nums.rbegin(),nums.rend()); int n=nums.size(); vector>res; for(int i=0;i<pow(2,n);i++){ ve
2015-05-13 16:59:25
168
原创 LeetCode - 92 Reverse Linked List II
class Solution {public: ListNode* reverseBetween(ListNode* head, int m, int n) { int count=1; ListNode *pre=head,*cur=head->next,*insert_point=head; if(m==1){
2015-05-09 07:33:26
152
原创 LeetCode - 206 Reverse Linked List
class Solution {public: ListNode* reverseList(ListNode* head) { if(head==NULL||head->next==NULL){ return head; } ListNode *former=head,*latter=head->next,*temp
2015-05-09 06:04:02
184
原创 LeetCode - 14 Longest Common Prefix
class Solution {public: string longestCommonPrefix(vector& strs) { string res; if(strs.empty()){ return res; } int n=strs[0].size(); for(auto &
2015-05-09 04:56:28
160
原创 LeetCode - 202 Happy Number
class Solution {public: bool isHappy(int n) { set con; while(1){ stringstream ss; ss<<n; string s; ss>>s; int sum=0;
2015-05-09 04:37:07
219
原创 LeetCode - 53 Maximum Subarray
int maxSub(vector &nums,int i,int j){ if(i==j){ return nums[i]; } int res=max(maxSub(nums,i,(i+j)/2),maxSub(nums,(i+j)/2+1,j)); int left_max=nums[(i+j)/2],right_max=0,sum=0;
2015-05-09 03:15:02
171
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人