
数据结构与算法
三块桌布
这个作者很懒,什么都没留下…
展开
-
lc 973.middle 最接近原点的 K 个点(***自定义sort比较函数)
评论https://leetcode-cn.com/problems/k-closest-points-to-origin/comments/659297自定义compare函数https://leetcode-cn.com/problems/k-closest-points-to-origin/solution/sortzi-ding-yi-pai-xu-by-heroding/bool compare(vector<int>& a, vector<int> ..原创 2020-11-17 00:14:12 · 214 阅读 · 0 评论 -
lc 122.easy买卖股票的最佳时机II(***贪心算法)
评论https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/comments/87652class Solution {public: int maxProfit(vector<int>& prices) { int len=prices.size(), sum=0; if(len<=1) return 0; for(int i=1;i..原创 2020-11-17 00:09:32 · 181 阅读 · 0 评论 -
lc 1122.easy 数组的相对排序
自己写的class Solution {public: vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) { vector<int> ans; int len1=arr1.size(); int added[len1]; memset(added,0,sizeof(added)..原创 2020-11-17 00:04:58 · 137 阅读 · 0 评论 -
lc 406.middle根据身高重建队列(****数学问题,***自定义sort比较函数)
评论区高能!!!https://leetcode-cn.com/problems/queue-reconstruction-by-height/comments/62983官方题解(这tm是阅读理解吧!!)https://leetcode-cn.com/problems/queue-reconstruction-by-height/solution/gen-ju-shen-gao-zhong-jian-dui-lie-by-leetcode-sol/class Solution {pub..原创 2020-11-17 00:01:07 · 167 阅读 · 0 评论 -
力扣周赛215.2 middle 确定两个字符串是否接近(字符串)
看了别人的思路自己写的,思路懂了就不难https://leetcode-cn.com/problems/determine-if-two-strings-are-close/solution/hen-jian-dan-de-ban-fa-by-handsomeluoyang/class Solution {public: bool closeStrings(string word1, string word2) { if(word1.size() != word2..原创 2020-11-15 12:47:53 · 211 阅读 · 0 评论 -
lc 922. easy按奇偶排序数组 II【***双指针】
自己写的超时了class Solution {public: vector<int> sortArrayByParityII(vector<int>& A) { for(int i=0;i<A.size();++i){ if(i%2==0 && A[i]%2==1){ for(int j=i+1;j<A.size();++j) ..原创 2020-11-12 23:41:53 · 129 阅读 · 0 评论 -
lc 31.middle下一个排列【****数学问题】
题解https://leetcode-cn.com/problems/next-permutation/solution/xia-yi-ge-pai-lie-suan-fa-xiang-jie-si-lu-tui-dao-/class Solution {public: void nextPermutation(vector<int>& nums) { int len=nums.size(), i=len-1; while(i&g..原创 2020-11-10 23:31:26 · 140 阅读 · 0 评论 -
lc 327.hard区间和的个数【*****递归】
327. 区间和的个数难度 困难给定一个整数数组nums,返回区间和在[lower, upper]之间的个数,包含lower和upper。区间和S(i, j)表示在nums中,位置从i到j的元素之和,包含i和j(i≤j)。说明:最直观的算法复杂度是O(n2) ,请在此基础上优化你的算法。示例:输入: nums = [-2,5,-1], lower = -2, upper = 2,输出: 3 解释: 3个区间分别是: [0,0], [2...原创 2020-11-08 00:17:40 · 169 阅读 · 0 评论 -
lc 1356. easy根据数字二进制下 1 的数目排序【****位运算,***排序】
1356. 根据数字二进制下 1 的数目排序难度 简单给你一个整数数组arr。请你将数组中的元素按照其二进制表示中数字1的数目升序排序。如果存在多个数字二进制中1的数目相同,则必须将它们按照数值大小升序排列。请你返回排序后的数组。示例 1:输入:arr = [0,1,2,3,4,5,6,7,8]输出:[0,1,2,4,8,3,5,6,7]解释:[0] 是唯一一个有 0 个 1 的数。[1,2,4,8] 都有 1 个 1 。[3,5,6] 有 2 个 1 。[7...原创 2020-11-07 19:53:42 · 169 阅读 · 0 评论 -
lc57.hard 插入区间(区间重叠的判断,扩大待插入区间的范围,***找到插入位置)
class Solution {public: vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) { int len=intervals.size(); int start=newInterval[0], end=newInterval[1]; vec...原创 2020-11-04 23:25:13 · 150 阅读 · 0 评论 -
lc 941.easy有效的山脉数组(cmp lc845数组中的最长山脉)
又是模拟爬山,不过要简单很多,一定是先上再下。class Solution {public: bool validMountainArray(vector<int>& A) { int len=A.size(), i=1; if(len<3) return false; while(i<len && A[i]>A[i-1]) ++i; if(i==1 || i==len..原创 2020-11-03 23:17:52 · 99 阅读 · 0 评论 -
lc 349.easy两个数组的交集(数组,我用的unordered_set哈希表)
官方题解https://leetcode-cn.com/problems/intersection-of-two-arrays/solution/liang-ge-shu-zu-de-jiao-ji-by-leetcode-solution/我:class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { ..原创 2020-11-02 22:19:22 · 171 阅读 · 0 评论 -
lc 140.hard单词拆分II【①动态规划->lc139.单词拆分;②*****枚举 - 回溯法】
官方题解https://leetcode-cn.com/problems/word-break-ii/solution/dan-ci-chai-fen-ii-by-leetcode-solution/抄答案:class Solution {public: unordered_set<string> wordDictSet; unordered_map<int, vector<string>> sen; //记录每个下标对应的字符 以它开头..原创 2020-11-01 23:20:02 · 197 阅读 · 0 评论 -
lc 139.middle单词拆分【****动态规划】
官方题解【动态规划】https://leetcode-cn.com/problems/word-break/solution/dan-ci-chai-fen-by-leetcode-solution/class Solution {public: bool wordBreak(string s, vector<string>& wordDict) { unordered_set<string> wordDictSet; fo..原创 2020-11-01 23:14:25 · 268 阅读 · 0 评论 -
lc 463.easy 岛屿的周长(二维数组,数学问题)
我看的代码https://leetcode-cn.com/problems/island-perimeter/comments/43681官方题解https://leetcode-cn.com/problems/island-perimeter/solution/dao-yu-de-zhou-chang-by-leetcode-solution/class Solution {public: int islandPerimeter(vector<vector<int&...原创 2020-10-30 22:28:30 · 146 阅读 · 0 评论 -
lc 19.删除链表的倒数第N个节点(链表,***快慢指针)
官方题解https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/shan-chu-lian-biao-de-dao-shu-di-nge-jie-dian-b-61/我看的代码https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/comments/105722class Solution {public: Li..原创 2020-10-29 20:35:17 · 241 阅读 · 0 评论 -
lc 129.求根到叶子节点数字之和【***递归】
官方题解https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/qiu-gen-dao-xie-zi-jie-dian-shu-zi-zhi-he-by-leetc//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ..原创 2020-10-29 19:42:49 · 140 阅读 · 0 评论 -
lc 1207.独一无二的出现次数(数组)
自己写的嘻嘻嘻class Solution {public: bool uniqueOccurrences(vector<int>& arr) { sort(arr.begin(), arr.end()); int len=arr.size(), cnt=0; if(len<=1) return true; vector<int> re; for(int i=1;i<..原创 2020-10-28 22:32:56 · 112 阅读 · 0 评论 -
lc 1365.[easy]有多少小于当前数字的数字【***计数排序】
官方题解https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/solution/you-duo-shao-xiao-yu-dang-qian-shu-zi-de-shu-zi--2/最朴素的方法:二重循环class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int&g..原创 2020-10-26 23:34:46 · 99 阅读 · 0 评论 -
lc 845.middle数组中的最长山脉
欧买噶,第一次自己做出了middle,鸡冻!!官方题解https://leetcode-cn.com/problems/longest-mountain-in-array/solution/shu-zu-zhong-de-zui-chang-shan-mai-by-leetcode-sol/我是强行模拟爬山过程,用类似贪心的思想,每次记下最长山脉。代码看起来很长,其实思路很单纯的!然后我发现像这种模拟其实挺麻烦的,需要各种数据来检错,覆盖所有情况。。class Solution {pub..原创 2020-10-25 11:22:36 · 127 阅读 · 0 评论 -
lc 234.[easy]回文链表(快慢指针,翻转链表,比较两个链表)->lc143
欧买噶,鸡冻,第一次自己写出通过了,而且就是空间O(1)。用了前面学的快慢指针找中间结点,翻转链表,比较两个链表。注意找的中间结点是n/2向下取整,然后翻转n/2向上取整处的链表。n为奇数时,l1会多一个结点,此时整个链表也是回文,返回true。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : v..原创 2020-10-23 22:22:25 · 125 阅读 · 0 评论 -
lc 763.划分字母区间(字符串,双指针)【***贪心算法】
官方题解https://leetcode-cn.com/problems/partition-labels/solution/hua-fen-zi-mu-qu-jian-by-leetcode-solution/class Solution {public: vector<int> partitionLabels(string S) { vector<int> re; int endc[26], len=S.size(); ...原创 2020-10-22 23:52:43 · 234 阅读 · 0 评论 -
lc 925.长按键入(字符串,双指针)
官方题解https://leetcode-cn.com/problems/long-pressed-name/solution/chang-an-jian-ru-by-leetcode-solution/class Solution {public: bool isLongPressedName(string name, string typed) { int i=0, j=0, len1=name.size(), len2=typed.size(); w..原创 2020-10-21 22:16:18 · 191 阅读 · 0 评论 -
lc 143.重排链表【****①找中间结点;②翻转中间结点之后的链表,形成两个链表;③合并两个链表】
官方题解https://leetcode-cn.com/problems/reorder-list/solution/zhong-pai-lian-biao-by-leetcode-solution//** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ..原创 2020-10-20 23:43:08 · 141 阅读 · 0 评论 -
lc 206.反转链表
迭代法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseList(ListNode* head) { if(!head ||..原创 2020-10-20 22:45:47 · 108 阅读 · 0 评论 -
lc 876.链表的中间结点【***链表 - 快慢指针】
官方题解https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut//** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)..原创 2020-10-20 22:28:22 · 105 阅读 · 0 评论 -
lc 844.比较含退格的字符串【①重构字符串,然后比较;②***逆序遍历字符串】
官方题解https://leetcode-cn.com/problems/backspace-string-compare/solution/bi-jiao-han-tui-ge-de-zi-fu-chuan-by-leetcode-solu/①重构字符串,然后比较class Solution {public: bool backspaceCompare(string S, string T) { string s,t; int i; ...原创 2020-10-19 23:29:12 · 106 阅读 · 0 评论 -
lc1002.查找常用字符(字符串数组)
class Solution {public: vector<string> commonChars(vector<string>& A) { int minfre[26], fre[26]; vector<string> re; if(!A.size()) return re; memset(minfre,100,26*sizeof(int)); for(int i...原创 2020-10-15 15:13:17 · 96 阅读 · 0 评论 -
lc 24.两两交换链表中的节点(链表,交换顺序)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(ne...原创 2020-10-13 23:35:22 · 173 阅读 · 0 评论 -
lc 142.环形链表II【返回入环结点,①***用哈希表;②*****快慢指针,如何利用相遇点 找到入环点?】
官方题解https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/huan-xing-lian-biao-ii-by-leetcode-solution/①***用哈希表/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val..原创 2020-10-10 14:46:59 · 111 阅读 · 0 评论 -
lc 141.环形链表【****神奇的快慢指针】【*****双指针的用途】
官方题解https://leetcode-cn.com/problems/linked-list-cycle/solution/huan-xing-lian-biao-by-leetcode-solution/【*****链表双指针用途】https://leetcode-cn.com/problems/linked-list-cycle/solution/yi-wen-gao-ding-chang-jian-de-lian-biao-wen-ti-h-2//** * Definition f..原创 2020-10-09 23:16:41 · 139 阅读 · 0 评论 -
lc 462.最少移动次数 使数组元素相等II【数学问题:所有数字与m之差 的绝对值 之和 最小】
结论是m为中位数,简单证明:https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements-ii/comments/97616class Solution {public: int minMoves2(vector<int>& nums) { sort(nums.begin(),nums.end()); int n=nums.size(); i..原创 2020-10-08 20:01:51 · 188 阅读 · 0 评论 -
lc 75.颜色分类【***荷兰国旗问题】
题目中的方法:class Solution {public: void sortColors(vector<int>& nums) { int cnt0,cnt1,cnt2; cnt0=cnt1=cnt2=0; for(int i=0;i<nums.size();++i){ if(nums[i]==0) cnt0++; else if(nums[i]==1) cnt1..原创 2020-10-07 23:53:36 · 113 阅读 · 0 评论 -
lc 834.树中距离之和【*****树形DP】
看了这个题解看懂了https://leetcode-cn.com/problems/sum-of-distances-in-tree/solution/c-liang-ci-dfsde-dao-da-an-by-congwang357-2/核心就在这里:※求出递推式;※先使用后序遍历求出ans[root]和所有cnt[i],然后使用前序遍历用ans[node]求出ans[node的孩子];※使用用数组存储邻接点的方式存储图。class Solution {public: ..原创 2020-10-07 23:19:54 · 130 阅读 · 0 评论 -
lc15.三数之和【*****枚举+剪枝+双指针】
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { int n=nums.size(); sort(nums.begin(),nums.end()); vector<vector<int>> re; for(int a=0;a<n;++a){ ...原创 2020-10-06 19:37:20 · 117 阅读 · 0 评论 -
lc18.四数之和(枚举+剪枝+双指针)->lc15
官方题解https://leetcode-cn.com/problems/4sum/solution/si-shu-zhi-he-by-leetcode-solution/class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> re; i..原创 2020-10-06 19:35:32 · 151 阅读 · 0 评论 -
lc 2.两数相加(链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(ne...原创 2020-10-04 22:31:31 · 172 阅读 · 0 评论 -
lc701.二叉搜索树插入
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}...原创 2020-09-30 19:12:44 · 106 阅读 · 0 评论 -
lc117.填充每个节点的下一个右侧节点指针II【①->lc102层序遍历,记录每层结点数;②*****在第n层为第n+1层建立next,在n+1层通过本层next为第n+2层建立next】
①->lc102层序遍历,记录每层结点数/*// Definition for a Node.class Node {public: int val; Node* left; Node* right; Node* next; Node() : val(0), left(NULL), right(NULL), next(NULL) {} Node(int _val) : val(_val), left(NULL), right(NULL..原创 2020-09-28 20:02:53 · 125 阅读 · 0 评论 -
lc235.二叉搜索树的最近公共祖先【①分别得到祖先序列,然后比较;②***同时查找,找出分岔结点】
①分别得到祖先序列,然后比较/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* l..原创 2020-09-27 17:29:15 · 571 阅读 · 0 评论