
战胜LeetCode刷题
文章平均质量分 96
坚持就是胜利好吧
战胜.
一个人要想更出色,需付出别人更多的时间,要相信自己,也相信目标一定能达成
展开
-
战胜LeetCode刷题 【数组篇】
数组目录:136. 只出现一次的数字 2022-01-11169. 多数元素 2022-01-1115. 三数之和 2022-01-124. 寻找两个正序数组的中位数 2022-01-1275. 颜色分类 2022-01-13119. 杨辉三角 II 2022-01-1448. 旋转图像 2022-01-1456. 合并区间 2022-01-1559. 螺旋矩阵 II 2022-01-17240. 搜索二维矩阵 II 2022-01-17435. 无重叠区间 2022-01-17704. 二分查找 2022原创 2022-01-11 17:11:17 · 504 阅读 · 0 评论 -
LeetCode每日一题 (61) 49. 字母异位词分组 (哈希+emplace_back)
49. 字母异位词分组class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { map<string,int> position; //纪录同一类别的位置 vector<vector<string>> result; //结果 int len=0;原创 2020-12-14 12:36:26 · 711 阅读 · 0 评论 -
LeetCode每日一题 (60) 204. 计数质数(制表法:素数的判定)
方法一:枚举class Solution {public: bool isPrime(int x) { for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { return false; } } return true; } int countPrimes(int n) { int原创 2020-12-03 17:57:18 · 320 阅读 · 0 评论 -
LeetCode每日一题 (59) 328. 奇偶链表
328. 奇偶链表class Solution {public: ListNode* oddEvenList(ListNode* head) { if(head==nullptr) return head; ListNode *first,*second,*temp,*pre; first=head; second=head->next; temp=head->next; whil原创 2020-11-14 23:04:37 · 270 阅读 · 0 评论 -
LeetCode每日一题 (58)1122. 数组的相对排序
class Solution {public: // 先对arr1数组每个元素进行计数,然后先将arr2里面的元素先放在result,然后将剩下的放入result vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) { vector<int>fg(1001,0); vector<int> res...原创 2020-11-14 23:00:08 · 211 阅读 · 0 评论 -
LeetCode每日一题 (57)922. 按奇偶排序数组 II
class Solution {public: // 使用双指针,一个为奇数 一个为偶数 然后交换两个位置的元素,遍历整个数组就好了 vector<int> sortArrayByParityII(vector<int>& A) { int i=0,j=1,len=A.size(),t; while(i<len && j<len){ while(i<len &am...原创 2020-11-12 22:39:40 · 228 阅读 · 0 评论 -
LeetCode每日一题 (56) 31. 下一个排列(这个排序的下一个大点的排序,两找交换一反转)
具体地,我们这样描述该算法,对于长度为 n 的排列 a:一(找)、首先从后向前查找第一个顺序对 (i,i+1),满足a[i] < a[i+1]这样「较小数」即为 a[i]a[i]。此时 [i+1,n) 必然是下降序列。二(找)、如果找到了顺序对,那么在区间 [i+1,n) 中从后向前查找第一个元素 j 满足 a[i] < a[j]。这样「较大数」即为 a[j]a[j]。三(反转)、交换 a[i] 与 a[j]此时可以证明区间 [i+1,n) 必为降序。我们可以直接使用双指针反转区间 [i.原创 2020-11-10 23:19:04 · 259 阅读 · 0 评论 -
LeetCode每日一题 (55) 127. 单词接龙
127. 单词接龙题解一:class Solution {public: // 求最短转换路径可以使用广度优先搜索 int ladderLength(string beginWord, string endWord, vector<string>& wordList) { vector<int> fg(wordList.size(),0); // 标记每个元素是否已经被访问了 queue<string>原创 2020-11-05 19:08:53 · 847 阅读 · 0 评论 -
LeetCode每日一题 (54) 57. 插入区间
57. 插入区间遍历整个数组,依次合并操作!class Solution {public: vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) { vector<vector<int>> result; int a=newInterval[0原创 2020-11-05 17:35:55 · 230 阅读 · 0 评论 -
LeetCode每日一题 (53) 941. 有效的山脉数组
941. 有效的山脉数组class Solution {public: bool validMountainArray(vector<int>& A) { if(A.size()<3) return false; int fg=A[1]-A[0]; // fg用来纪录前面时刻山脉的趋势 if(fg<=0) return false; for(int i=1;i<A.size();i++)原创 2020-11-03 22:52:06 · 220 阅读 · 0 评论 -
LeetCode每日一题 (52) 349. 两个数组的交集
349. 两个数组的交集方法一:双指针法class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); int length1 = num原创 2020-11-02 12:24:08 · 251 阅读 · 0 评论 -
LeetCode每日一题 (51) 140. 单词拆分 II(记忆化搜索)
140. 单词拆分 II超时:class Solution {public: vector<string> wordBreak(string s, vector<string>& wordDict) { string tempResult=""; vector<string> result; choose(s,0,tempResult,result,wordDict); ret原创 2020-11-01 20:47:55 · 335 阅读 · 0 评论 -
LeetCode每日一题 (50) 463. 岛屿的周长
463. 岛屿的周长class Solution {public: // 遍历整个二维数组,计算1(也就是陆地)的周围为0的的个数之和 int islandPerimeter(vector<vector<int>>& grid) { int Sum=0; for(int i=0;i<grid.size();i++){ for(int j=0;j<grid[i].size();j++原创 2020-10-30 12:04:27 · 221 阅读 · 0 评论 -
LeetCode每日一题 (49)129. 求根到叶子节点数字之和
class Solution {public: int Sum=0; int sumNumbers(TreeNode* root) { search(root,0); return Sum; } void search(TreeNode *root,int tempSum){ if(root==nullptr) return; tempSum=tempSum*10+root->val; ...原创 2020-10-29 22:53:45 · 217 阅读 · 0 评论 -
LeetCode每日一题 (48) 1207. 独一无二的出现次数(map字典)
class Solution {public: bool uniqueOccurrences(vector<int>& arr) { unordered_map<int, int> occur; for (const auto& x: arr) { occur[x]++; } unordered_set<int> times; for (...原创 2020-10-28 23:21:07 · 256 阅读 · 0 评论 -
LeetCode每日一题 (47)144. 二叉树的前序遍历
class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> result; if(root==nullptr) return result; result.push_back(root->val); vector<int> temp; temp=preorderTra..原创 2020-10-27 23:09:49 · 247 阅读 · 0 评论 -
LeetCode每日一题 (46) 1365. 有多少小于当前数字的数字
1365. 有多少小于当前数字的数字class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { vector<int> tempnums=nums; vector<int>count(101,0); sort(tempnums.begin(),tempnums.end());原创 2020-10-26 23:31:38 · 225 阅读 · 0 评论 -
LeetCode每日一题 (45) 1024. 视频拼接(动态规划、贪心算法)
1024. 视频拼接class Solution {public: // 1. 使用动态规划 // dp[i]=min{dp[i],dp[aj]+1} (aj<i≤bj) // dp[i] 表示从0到i的最小片段数 int videoStitching(vector<vector<int>>& clips, int T) { vector<int>dp(T+1,INT_MAX-1);原创 2020-10-24 20:02:42 · 420 阅读 · 0 评论 -
LeetCode每日一题 (44) 844. 比较含退格的字符串
844. 比较含退格的字符串class Solution {public: // 遍历两个字符串,将其都变成最终结果的形式在做比较 bool backspaceCompare(string S, string T) { char s[201],t[201]; int a=0,b=0; for(int i=0;S[i]!='\0';i++){ if(S[i]=='#'){ if(原创 2020-10-19 16:54:22 · 223 阅读 · 0 评论 -
LeetCode每日一题 (43) 19. 删除链表的倒数第N个节点
19. 删除链表的倒数第N个节点class Solution {public: // 1. 可以先计算出链表的总长度s,然后删除第s-n就可以了 ListNode* removeNthFromEnd(ListNode* head, int n) { int s=0; ListNode *p=head,*pre; while(p!=nullptr){ s++; p=p->next;原创 2020-10-18 20:35:31 · 212 阅读 · 0 评论 -
LeetCode每日一题 (42) 51. N 皇后3
52. N皇后 IILeetCode每日一题 (5) 51. N 皇后class Solution {public: int *X; // 一个一维数组,用来纪录每一层选择的位置; int Sum=0; // 纪录n皇后的解的个数 int N; // 表示n*n的棋盘 int totalNQueens(int n) { N=n; Sum=0; X=new int[n+1]; for(int i=0;原创 2020-10-17 22:17:51 · 209 阅读 · 0 评论 -
LeetCode每日一题 (42)116. 填充每个节点的下一个右侧节点指针
116. 填充每个节点的下一个右侧节点指针LeetCode每日一题 (28) 117. 填充每个节点的下一个右侧节点指针IIclass Solution {public: Node* connect(Node* root) { queue<Node*> Q; Node *p,*pre; if(root==NULL) return root; Q.push(root); while(!Q.em原创 2020-10-16 17:50:38 · 235 阅读 · 0 评论 -
LeetCode每日一题 (41) 977. 有序数组的平方
977. 有序数组的平方题解一:class Solution {public: // 可以直接原地将数组元素平方,然后从小到大进行排序 vector<int> sortedSquares(vector<int>& A) { for(int i=0;i<A.size();i++){ A[i]=A[i]*A[i]; } sort(A.begin(),A.end());原创 2020-10-16 17:42:30 · 179 阅读 · 0 评论 -
LeetCode每日一题 (40)1002. 查找常用字符
1002. 查找常用字符题解:result.emplace_back(1,i+'a');emplace_back将字符变成字符串放入result;class Solution {public: vector<string> commonChars(vector<string>& A) { int flag[26]={0}; vector<string> result; // 1.以第一个为基原创 2020-10-14 18:11:35 · 269 阅读 · 0 评论 -
LeetCode每日一题 (39) 24. 两两交换链表中的节点
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 *nex原创 2020-10-13 22:06:27 · 206 阅读 · 0 评论 -
LeetCode每日一题 (38) 530. 二叉搜索树的最小绝对差 (中序遍历)
530. 二叉搜索树的最小绝对差/** * 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: int getMin原创 2020-10-12 22:33:19 · 171 阅读 · 0 评论 -
LeetCode每日一题 (37) 142. 环形链表 II(找入口:双指针法)
142. 环形链表 II双指针法:解题思路:这类链表题目一般都是使用双指针法解决的,例如寻找距离尾部第K个节点、寻找环入口、寻找公共尾部入口等。算法流程:双指针第一次相遇: 设两指针 fast,slow 指向链表头部 head,fast 每轮走 22 步,slow 每轮走 11 步;第一种结果: fast 指针走过链表末端,说明链表无环,直接返回 null;TIPS: 若有环,两指针一定会相遇。因为每走 11 轮,fast 与 slow 的间距 +1+1,fast 终会追上 slow;第二原创 2020-10-10 09:09:50 · 179 阅读 · 0 评论 -
LeetCode每日一题 (36) 141. 环形链表(找循环:快慢指针)
141. 环形链表对访问过的节点标记一下:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) {原创 2020-10-09 23:04:05 · 153 阅读 · 0 评论 -
LeetCode每日一题 (35) 344. 反转字符串(易)
344. 反转字符串class Solution {public: void reverseString(vector<char>& s) { // 直接前后交换就好了,start++前进,end--后退 if(s.size()==0) return; int start=0,end=s.size()-1; char temp; while(start<end){原创 2020-10-08 18:24:13 · 142 阅读 · 0 评论 -
LeetCode每日一题 (34) 18. 四数之和
18. 四数之和class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> result; vector<int> tempresult; sort(nums.begin(),nums.end()); // 排序原创 2020-10-07 20:27:42 · 134 阅读 · 0 评论 -
LeetCode每日一题 (33)75. 颜色分类
75. 颜色分类使用库函数(测试)class Solution {public: void sortColors(vector<int>& nums) { sort(nums.begin(),nums.end()); }};统计个数,再赋值:class Solution {public: void sortColors(vector<int>& nums) { int count0=0,c原创 2020-10-07 20:20:47 · 169 阅读 · 0 评论 -
LeetCode每日一题 (32)1. 两数之和
1. 两数之和方法一:暴力循环class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> result; for(int i=0;i<nums.size();i++){ for(int j=i+1;j<nums.size();j++){原创 2020-10-03 21:53:09 · 252 阅读 · 0 评论 -
LeetCode每日一题 (31) 701. 二叉搜索树中的插入操作
701. 二叉搜索树中的插入操作题解一:递归这里采用了第一种插入方式,插入到最后面作为叶子节点,/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNod原创 2020-09-30 18:03:53 · 187 阅读 · 0 评论 -
LeetCode每日一题 (30) 501. 二叉搜索树中的众数
501. 二叉搜索树中的众数递归:/** * 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: vector<原创 2020-09-29 17:51:13 · 163 阅读 · 0 评论 -
LeetCode每日一题 (29) 145. 二叉树的后序遍历
145. 二叉树的后序遍历方法一:递归/** * 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)原创 2020-09-29 12:41:10 · 153 阅读 · 0 评论 -
LeetCode每日一题 (28) 117. 填充每个节点的下一个右侧节点指针 II
117. 填充每个节点的下一个右侧节点指针 II题解:/*// 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(N原创 2020-09-28 22:57:49 · 160 阅读 · 0 评论 -
LeetCode每日一题 (27) 235. 二叉搜索树的最近公共祖先
235. 二叉搜索树的最近公共祖先这个题目的关键就是二叉搜索树(左小右大),需要利用这一个关键信息,要找最近的公共祖先:就是用root当前节点与p和q比较就好,一共用三种情况(1.两个节点都在root的左边;2.两个节点都在root的右边;3.不然公共节点就是root了)。方法一:递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *原创 2020-09-27 12:42:52 · 131 阅读 · 0 评论 -
LeetCode每日一题 (26) 113. 路径总和 II
113. 路径总和 II题解:/** * 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: vector<ve原创 2020-09-26 12:39:46 · 314 阅读 · 0 评论 -
LeetCode每日一题 (25) 106. 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树题解:/** * 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: Tree原创 2020-09-25 22:17:08 · 132 阅读 · 0 评论 -
LeetCode每日一题 (24) 617. 合并二叉树
617. 合并二叉树题解:/** * 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* merg原创 2020-09-23 18:06:37 · 144 阅读 · 0 评论