LeetCode-- Unique Binary Search Trees II
摘要:递归 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode...
阅读全文
LeetCode--Next Permutation
摘要:(1)从后往前,找到a[i] &num) { 4 int end = num.size() - 1; 5 int povit = end; 6 while(povit > 0){ 7 if(num[povit] > num[po...
阅读全文
LeetCode--Wrod Break
摘要:---恢复内容开始--- 1 class Solution { 2 public: 3 bool wordBreak(string s, unordered_set &dict) { 4 int nsize=s.size(); 5 int i=0,j=0; 6...
阅读全文
LeetCode--Largest Rectangle in Histogram
摘要:---恢复内容开始--- 1 class Solution { 2 public: 3 int largestRectangleArea(vector &height) { 4 int n = height.size(); 5 if(n == 0) 6 ...
阅读全文
LeetCode--Binary Tree Maximum Path Sum
摘要:递归,dfs这里返回值是一个pair,first表示两个子树的不经过当前root的最大pathsum;second表示两个子树的,以root->left和root->right为路径起始节点的路径的最大值 1 /** 2 * Definition for binary tree 3 * stru...
阅读全文
LeetCode--Reorder List
摘要:(1)双指针(快慢):找到中节点(2)反转第一或者第二个链表(反转第一个后面合并完了还得再反转),所以建议反转第二个链表代码:暂时不贴了https://oj.leetcode.com/problems/reorder-list/
阅读全文
LeetCode--Combination Sum
摘要:由于需要就地保存清零信息,所以把信息保存在第一行和第一列 1 class Solution { 2 public: 3 void setZeroes(vector > &matrix) { 4 const int ROW = matrix.size(); 5 ...
阅读全文
LeetCode--Binary Tree Level Order Traversal
摘要:用两个队列实现,交叉使用,达到判断层的目的 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right...
阅读全文
LeetCode--Plus One
摘要:考查,最高位有进位 1 class Solution { 2 public: 3 vector plusOne(vector &digits) { 4 // IMPORTANT: Please reset any member data you declared, as 5 ...
阅读全文
LeetCode--Combination Sum
摘要:一开始的思路是:中序遍历+判断遍历后的数组,时间空间都不是最优果然超时了 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * ...
阅读全文
LeetCode--Balanced Binary Tree
摘要:递归 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode...
阅读全文
LeetCode--N-Queens II
摘要:dfs 1 class Solution { 2 public: 3 int res; 4 int totalNQueens(int n) { 5 res = 0; 6 vector arr(n); 7 dfs(arr,0,n); 8 ...
阅读全文
LeetCode--Merge Two Sorted Lists
摘要:重新写了下,代码看着清爽多了 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x...
阅读全文
LeetCode--Restore IP Addresses
摘要:dfs即可注意边界情况:(1)s长度不在4 和 12之间(2)"010"这种形式是错误的 1 class Solution { 2 public: 3 vector restoreIpAddresses(string s) { 4 vector res; 5 ...
阅读全文
LeetCode--3Sum
摘要:类似于2sum,先排序,然后从左开始遍历,计算a[i]后面的等于-a[i]的两个元素,注意去除重复元素 1 class Solution { 2 public: 3 vector > threeSum(vector &num) { 4 vector > res; 5 ...
阅读全文
LeetCode--Insert Interval
摘要:其实可以O(n)的思路,如下 1 class Solution { 2 public: 3 vector insert(vector &intervals, Interval newInterval) { 4 // Start typing your C/C++ soluti...
阅读全文
LeetCode--Surrounded Regions
摘要:1 class Solution { 2 public: 3 void solve(vector> &board) { 4 int m = board.size(); 5 if(m == 0) 6 return; 7 ...
阅读全文
LeetCode--Container With Most Water
摘要:双指针 1 class Solution { 2 public: 3 int maxArea(vector &height) { 4 if(height.size() == 0 || height.size() == 1) 5 return 0; 6 ...
阅读全文
LeetCode--Permutation Sequence
摘要:1 class Solution { 2 public: 3 string getPermutation(int n, int k) { 4 int fac[10]; 5 bool vis[10]; 6 memset(vis, 0, size...
阅读全文
LeetCode--Regular Expression Matching
摘要:1 class Solution { 2 public: 3 bool isMatch(const char *s, const char *p) { 4 // Start typing your C/C++ solution below 5 // DO N...
阅读全文