![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
zhoujun651115
这个作者很懒,什么都没留下…
展开
-
110 Balanced Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:32:25 · 222 阅读 · 0 评论 -
232 Implement Queue using Stacks
class Queue {public: // Push element x to the back of queue. void push(int x) { s1.push(x); } // Removes the element from in front of queue. void pop(void) { if(s原创 2015-11-14 20:31:19 · 180 阅读 · 0 评论 -
70 Climbing Stairs
class Solution {public: int climbStairs(int n) { int *res = new int[n+1]; for(int i=0;i<n+1;++i) res[i] = 0; res[1] = 1; res[2] = 2; for(int i=原创 2015-11-14 20:29:06 · 165 阅读 · 0 评论 -
206 Reverse Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* re原创 2015-11-14 20:27:22 · 210 阅读 · 0 评论 -
217 Contains Duplicate
class Solution {public: bool containsDuplicate(vector& nums) { set result; for(int i=0;i<nums.size();++i) { if(result.find(nums[i])!=result.end())原创 2015-11-14 20:18:43 · 125 阅读 · 0 评论 -
237 Delete Node in a Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: void deleteN原创 2015-11-14 20:13:53 · 160 阅读 · 0 评论 -
111 Minimum Depth of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:42:00 · 136 阅读 · 0 评论 -
9 Palindrome Number
class Solution {public: bool isPalindrome(int x) { if(x<0) return false; int xx = x; int count = 0; while(xx!=0) { ++count原创 2015-11-14 20:40:58 · 222 阅读 · 0 评论 -
119 Pascal's Triangle II
class Solution {public: vector getRow(int rowIndex) { vector res; vector tmp; int i=0; while(i<=rowIndex) { tmp.clear(); tmp.push_b原创 2015-11-14 20:40:20 · 167 阅读 · 0 评论 -
112 Path Sum
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:39:01 · 188 阅读 · 0 评论 -
66 Plus One
class Solution {public: vector plusOne(vector& digits) { vector res; int size = digits.size(); int add = 1; for(int i=size-1;i>=0;--i) { int tm原创 2015-11-14 20:36:50 · 160 阅读 · 0 评论 -
102 Binary Tree Level Order Traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:39:46 · 179 阅读 · 0 评论 -
58 Length of Last Word
class Solution {public: int lengthOfLastWord(string s) { int size = s.size(); int j = size-1; while(j>=0&&s[j]==' ') --j; if(j==-1) return原创 2015-11-14 20:44:46 · 209 阅读 · 0 评论 -
190 Reverse Bits
class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t res = 0; int i = 0; while(i<32) { res<<=1; res |= (n&1); n>原创 2015-11-15 10:30:16 · 197 阅读 · 0 评论 -
36 Valid Sudoku
class Solution {public: bool isValidSudoku(vector>& board) { //每一行 for(int i=0;i<9;++i) { bool s[9]; for(int j=0;j<9;++j) s[j] = fa原创 2015-11-15 10:29:53 · 176 阅读 · 0 评论 -
219 Contains Duplicate II
class Solution {public: bool containsNearbyDuplicate(vector& nums, int k) { map res; int size = nums.size(); for(int i=0;i<size;++i) { if(res.find(nums原创 2015-11-14 20:47:53 · 122 阅读 · 0 评论 -
*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 res = (C-A)*(D-B)+(G-E)*(H-F); A = max(A,E);原创 2015-11-14 20:48:38 · 152 阅读 · 0 评论 -
88 Merge Sorted Array
class Solution {public: void merge(vector& nums1, int m, vector& nums2, int n) { int i = m-1; int j = n-1; while(i>=0&&j>=0) { //这个if语句只是为了提高一点效率// if原创 2015-11-15 10:30:42 · 185 阅读 · 0 评论 -
19 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原创 2015-11-15 10:29:23 · 217 阅读 · 0 评论 -
20 Valid Parentheses
class Solution {public: bool isValid(string s) { int size = s.size(); stack res; for(int i=0;i<size;++i) { if(s[i]=='('||s[i]=='['||s[i]=='{')原创 2015-11-14 20:49:18 · 142 阅读 · 0 评论 -
160 Intersection of Two Linked Lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *ge原创 2015-11-14 20:41:33 · 128 阅读 · 0 评论 -
225 Implement Stack using Queues
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() { while(q1.size()!=1)原创 2015-11-14 20:38:28 · 181 阅读 · 0 评论 -
172 Factorial Trailing Zeroes
class Solution {public: int trailingZeroes(int n) { int res = 0; while(n/5) { res += n/5; n/=5; } return res; }};原创 2015-11-14 20:37:47 · 198 阅读 · 0 评论 -
118 Pascal's Triangle
class Solution {public: vector> generate(int numRows) { vector> res; vector tmp; vector tmp1; int i=1; while(i<=numRows) { tmp1.clear()原创 2015-11-14 20:37:15 · 193 阅读 · 0 评论 -
242 Valid Anagram
class Solution {public: bool isAnagram(string s, string t) { int res[256]; if(s.size()!=t.size()) return false; int size = s.size(); for(int i=0;i<256;原创 2015-11-14 20:20:11 · 166 阅读 · 0 评论 -
171 Excel Sheet Column Number
class Solution {public: int titleToNumber(string s) { int res = 0; for(int i=0;i<s.size();++i) { res = res*26 + s[i]-'A'+1; } return res; }原创 2015-11-14 20:19:27 · 161 阅读 · 0 评论 -
226 Invert Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:18:01 · 182 阅读 · 0 评论 -
283 Move Zeroes
class Solution {public: void moveZeroes(vector& nums) { int size = nums.size(); int i = 0; int j = 0; while(i<size&&j<size) { while(i<size&&num原创 2015-11-14 20:16:44 · 214 阅读 · 0 评论 -
100 Same Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:14:47 · 174 阅读 · 0 评论 -
*27 Remove Element
做出来了,不过花的时间比较多,不熟练class Solution {public: int removeElement(vector& nums, int val) { int i = 0; int size = nums.size(); int j = size-1; int count = 0; wh原创 2015-11-14 20:11:30 · 181 阅读 · 0 评论 -
*13 Roman to Integer
没明白罗马数字的规律,只有可能左边一class Solution {public: int romanToInt(string s) { int res = 0; for(int i=0;i<s.size();++i) { int a = getNum(s[i]); int b = 0;原创 2015-11-14 20:07:33 · 217 阅读 · 0 评论 -
104 Maximum Depth of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:02:36 · 265 阅读 · 0 评论 -
258 Add Digits
class Solution {public: int addDigits(int num) { if(num<10) return num; int res = 0; while(num>=10) { res += num%10; n原创 2015-11-14 20:01:40 · 118 阅读 · 0 评论 -
235 Lowest Common Ancestor of a Binary Search Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:24:11 · 264 阅读 · 0 评论 -
191 Number of 1 Bits
class Solution {public: int hammingWeight(uint32_t n) { int res = 0; while(n) { res++; n = n&(n-1); } return res; }};原创 2015-11-14 20:24:48 · 176 阅读 · 0 评论 -
198 House Robber
class Solution {public: int rob(vector& nums) { int size = nums.size(); if(size==0) return 0; if(size==1) return nums[0]; int *res = new in原创 2015-11-14 20:36:18 · 241 阅读 · 0 评论 -
26 Remove Duplicates from Sorted Array
class Solution {public: int removeDuplicates(vector& nums) { int size = nums.size(); if(size<=1) return size; int i=0; int j=1; while(原创 2015-11-14 20:35:39 · 165 阅读 · 0 评论 -
107 Binary Tree Level Order Traversal II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:35:08 · 164 阅读 · 0 评论 -
101 Symmetric Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2015-11-14 20:33:39 · 172 阅读 · 0 评论 -
21 Merge Two 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原创 2015-11-14 20:31:53 · 171 阅读 · 0 评论