- 博客(56)
- 收藏
- 关注
原创 leetcode 求众数
https://leetcode-cn.com/problems/majority-element/class Solution {public: int majorityElement(vector<int>& nums) { int times = 0; int ans = 0; for (in...
2019-10-27 14:42:06 111
原创 leetcode 220. 存在重复元素 III
https://leetcode-cn.com/problems/contains-duplicate-iii/方法1class Solution {public: bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { if (t<0 || k ==0...
2019-10-26 20:11:51 110
原创 leetcode 173. 二叉搜索树迭代器
https://leetcode-cn.com/problems/binary-search-tree-iterator/两个栈class BSTIterator {public: BSTIterator(TreeNode* root) { inorder(root); dump_stack(); } /** @retu...
2019-10-25 22:03:27 124
原创 leetcode 162. 寻找峰值
https://leetcode-cn.com/problems/find-peak-element/官方解答遍历class Solution {public: int findPeakElement(vector<int>& nums) { for (int i = 0; i < nums.size() - 1; ++i) ...
2019-10-25 11:58:17 138
原创 leetcode 二叉树非递归遍历
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/(前序遍历)class Solution {public: vector<int> preorderTraversal(TreeNode* root) { if (root == NULL) return {}; ...
2019-10-22 20:42:38 133
原创 leetcode 300. 最长上升子序列 334. 递增的三元子序列 354. 俄罗斯套娃信封问题
讲解:https://mp.weixin.qq.com/s/EWLi6sP3l4xsuc6GDpQAjwhttps://mp.weixin.qq.com/s/wK3zCwMtT04DdvTo-FzQ1Qhttps://leetcode-cn.com/problems/longest-increasing-subsequence/(LIS)O(N^2)解法class So...
2019-10-14 21:42:28 241
原创 leetcode 138. 复制带随机指针的链表
https://leetcode-cn.com/problems/copy-list-with-random-pointer/map法:class Solution {public: Node* copyRandomList(Node* head) { if (head == NULL) return NULL; map<...
2019-10-12 17:51:39 92
原创 leetcode 反转链表
https://leetcode-cn.com/problems/reverse-linked-list/(整体反转)迭代法class Solution {public: ListNode* reverseList(ListNode* head) { ListNode *prev = NULL; ListNode *cur = head; ...
2019-10-12 10:19:08 69
原创 leetcode 120. 三角形最小路径和
https://leetcode-cn.com/problems/triangle/二维dpclass Solution {public: int minimumTotal(vector<vector<int>>& triangle) { if (triangle.size() == 0) return 0; ...
2019-10-09 20:46:15 74
原创 leetcode 填充每个节点的下一个右侧节点指针
https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/(通用解)class Solution {public: Node* connect(Node* root) { Node *cur = root; while(cur) {...
2019-10-09 20:26:30 78
原创 leetcode 二叉树的层次遍历
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/(上到下)class Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>...
2019-10-09 19:55:20 78
原创 leetcode 103. 二叉树的锯齿形层次遍历
https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/剑指offer原题class Solution {public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { vector&l...
2019-10-09 15:13:32 81
原创 leetcode 排列
https://leetcode-cn.com/problems/next-permutation/(下一个排列)class Solution {public: void nextPermutation(vector<int> &nums) { int i = (int)nums.size() - 2; while (i >...
2019-10-08 23:06:39 99
原创 leetcode 组合&组合总和
https://leetcode-cn.com/problems/combination-sum/(无重复)class Solution {private: vector<int> candidates; vector<vector<int>> res; vector<int> path;public: vo...
2019-10-08 23:04:53 138
原创 leetcode 子集
https://leetcode-cn.com/problems/subsets/(无重复元素)class Solution {public: vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> ans; ...
2019-10-08 23:01:40 98
原创 leetcode 删除排序链表中的重复元素
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/重复元素保留一个class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ListNode *cur = head; while ...
2019-10-08 20:11:17 66
原创 leetcode 旋转排序数组
https://leetcode-cn.com/problems/search-in-rotated-sorted-array/查找特定数字,无重复数字class Solution {public: int search(vector<int>& nums, int target) { int left = 0; int ri...
2019-10-08 19:48:14 81
原创 leetcode 74. 搜索二维矩阵
https://leetcode-cn.com/problems/search-a-2d-matrix/class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); if (m ==...
2019-10-08 19:20:20 88
原创 leetcode 不同路径
https://leetcode-cn.com/problems/unique-paths/class Solution {public: int uniquePaths(int m, int n) { int dp[m][n]; for (int i = 0; i < m; ++i) dp[i][0] = 1; ...
2019-10-07 23:43:06 94
原创 leetcode 61. 旋转链表
https://leetcode-cn.com/problems/rotate-list/方法1(旋转数组法)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(...
2019-10-07 23:28:11 93
原创 leetcode 115. 不同的子序列
https://leetcode-cn.com/problems/distinct-subsequences/https://leetcode.wang/leetcode-115-Distinct-Subsequences.html
2019-10-05 00:12:08 104
原创 leetcode 4. 寻找两个有序数组的中位数
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/看这个解答https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-w-2/class ...
2019-10-03 19:56:25 61
原创 leetcode 19. 删除链表的倒数第N个节点
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (head == NULL) return NULL; Li...
2019-10-03 16:23:43 64
原创 leetcode 34. 在排序数组中查找元素的第一个和最后一个位置
https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/两次二分查找class Solution { // returns leftmost (or rightmost) index at which `target` should be // ins...
2019-10-03 14:57:21 77
原创 leetcode 416. 分割等和子集
https://leetcode-cn.com/problems/partition-equal-subset-sum/0-1背包问题变形class Solution {public: bool canPartition(vector<int>& nums) { int sum = 0; for (auto n : nums)...
2019-10-02 20:25:24 115
原创 leetcode 207. 课程表
https://leetcode-cn.com/problems/course-schedule/拓扑排序,判断有向无环图问题class Solution {public: class Graph{ public: Graph(int v) { this->v = v; adj.res...
2019-10-01 20:23:32 63
原创 leetcode 279. 完全平方数
https://leetcode-cn.com/problems/perfect-squares/预先算好平方数class Solution {public: int numSquares(int n) { vector<int> cand; for (int i = 1; i <= sqrt(n); ++i) ...
2019-10-01 00:38:04 85
原创 leetcode 337. 打家劫舍 III
https://leetcode-cn.com/problems/house-robber-iii/暴力递归(最后一个用例超时)class Solution {public: int rob(TreeNode* root) { return max(rob(root,1), rob(root,0)); } int rob(TreeNod...
2019-09-30 23:07:29 228
原创 leetcode 739. 每日温度
https://leetcode-cn.com/problems/daily-temperatures/https://mp.weixin.qq.com/s/X0kf4rB88oS-VOcI7OR6OA
2019-09-30 21:30:17 147
原创 leetcode 236. 二叉树的最近公共祖先
https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/思路参考 剑指offer52题class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { ...
2019-09-30 20:26:32 75
原创 剑指offer 试题52 两个链表的第一个公共节点
https://github.com/zhulintao/CodingInterviewChinese2/blob/master/52_FirstCommonNodesInLists/FirstCommonNodesInLists.cpp
2019-09-30 19:58:58 76
原创 leetcode 406. 根据身高重建队列
https://leetcode-cn.com/problems/queue-reconstruction-by-height/submissions/ [c++] 排序,然后插入。 假设候选队列为 A,已经站好队的队列为 B. 从 A 里挑身高最高的人 x 出来,插入到 B. 因为 B 中每个人的身高都比 x 要高,因此 x 插入的位置,就是看 x 前面应该有多少人...
2019-09-30 14:18:43 295
原创 leetcode 64. 最小路径和
https://leetcode-cn.com/problems/minimum-path-sum/submissions/动态规划class Solution {public: int minPathSum(vector<vector<int>>& grid) { int M = grid.size(); int...
2019-09-29 23:52:53 86
原创 leetcode 238. 除自身以外数组的乘积
https://leetcode-cn.com/problems/product-of-array-except-self/submissions/https://leetcode-cn.com/problems/product-of-array-except-self/solution/yi-ci-bian-li-qiao-miao-cun-chu-he-ji-suan-zuo-ji-/
2019-09-29 23:41:17 70
原创 leetcode 48. 旋转图像
https://leetcode-cn.com/problems/rotate-image/submissions/没少可说的,就是要细心class Solution {public: void rotate(vector<vector<int>>& matrix) { for (int i = 0; i < matrix....
2019-09-29 23:12:25 57
原创 leetcode 114. 二叉树展开为链表
https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/submissions/递归class Solution {public: void flatten(TreeNode* root) { TreeNode *last_node; helper(root, las...
2019-09-29 22:22:51 86
原创 leetcode 94. 二叉树的中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/submissions/递归法class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; ...
2019-09-29 21:37:08 76
原创 leetcode 1128. 等价多米诺骨牌对的数量
https://leetcode-cn.com/problems/number-of-equivalent-domino-pairs/submissions/这道题主要是边界条件多:bool cmp(const vector<int> &a, vector<int> &b){ //不能只考虑第一个元素 if (a[0] == b[...
2019-09-29 12:31:14 94
原创 leetcode 796. 旋转字符串
https://leetcode-cn.com/problems/rotate-string/submissions/思路:首先确认两个字符串的长度要相等。其次只要保证A+A 的字符串中包含B就可以了。因为A+A已经包含了所有可移动的方案。class Solution {public: bool rotateString(string A, string B) { ...
2019-09-29 11:28:33 84
原创 leetcode 686. 重复叠加字符串匹配
https://leetcode-cn.com/problems/repeated-string-match/submissions/方法一:这个问题可以概括为“最小的 k 是什么,B 是 A*k 的子串?”我们可以试试每一个 k。算法:假设我们写了 S = A+A+A+...,如果 B 是 S 的子串,我们只需要检查一些 S[0:], S[1:], ..., S[len(A) - ...
2019-09-28 11:04:32 192
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人