![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode刷题
文章平均质量分 71
刷刷题
仓鼠船船船船长
这个作者很懒,什么都没留下…
展开
-
LeetCode刷题——215. 数组中的第K个最大元素(中等)——快速查找/堆排序/挖坑法
我的解法:暴力查表class Solution {public: int findKthLargest(vector<int>& nums, int k) { multimap<int,int> contact; int i=-1; for(auto a:nums){ contact.insert({a,++i}); } int size=contact..原创 2022-04-14 15:49:29 · 563 阅读 · 0 评论 -
LeetCode刷题——1672. 最富有客户的资产总量(简单)
我的解法:直接遍历class Solution {public: int maximumWealth(vector<vector<int>>& accounts) { int m=accounts.size(); int n=accounts[0].size(); int max=0; for(int i=0;i<m;i++){ int sum=0; ..原创 2022-04-13 20:24:02 · 417 阅读 · 0 评论 -
LeetCode刷题——1342. 将数字变成 0 的操作次数(简单)
笑死嘞,1.29的题,属实想让大家好好跨年我的解法:class Solution {public: int numberOfSteps(int num) { int count=0; while(num){ if(num%2){ num-=1; }else{ num/=2; } count++;..原创 2022-04-13 19:45:46 · 457 阅读 · 0 评论 -
LeetCode——1337. 矩阵中战斗力最弱的 K 行(中等)——二分查找 + 堆/快速选择
我的解法:有点疲惫,就恩写,来回查表class Solution {public: vector<int> kWeakestRows(vector<vector<int>>& mat, int k) { vector<int> rank;//建立战斗力表 for(auto a:mat){ rank.push_back(count(a.cbegin(),a.cend(),1));..原创 2022-04-13 16:00:37 · 1327 阅读 · 0 评论 -
LeetCode刷题——876. 链表的中间结点(简单)——快慢指针
我的解法:直接快慢指针/** * 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) : va..原创 2022-04-13 13:11:00 · 1009 阅读 · 0 评论 -
LeetCode刷题——383. 赎金信(简单)——字符统计
我的写法:建立map查表class Solution {public: bool canConstruct(string ransomNote, string magazine) { map<char,int> content; for(char a:magazine){ content[a]++; } for(auto begin=ransomNote.cbegin();begin&..原创 2022-04-12 17:23:06 · 992 阅读 · 0 评论 -
LeetCode刷题——234. 回文链表(简单)——快慢指针
我的解法:暴力解法,头尾依次对比/** * 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 *n...原创 2022-04-08 15:17:08 · 177 阅读 · 0 评论 -
LeetCode刷题——3.无重复字符的最长子串(中等)——滑动窗口
我的解法:和解法1一样classSolution{public:intlengthOfLongestSubstring(strings){intstart(0),end(0),length(0),result(0);intsSize=s.size();while(end<sSize){chartemp=s[end];for(intp...原创 2022-04-05 15:05:49 · 122 阅读 · 0 评论 -
LeetCode刷题——2.两数相加(中等)——单链表
熟悉单链表操作:运用单链表指针遍历,注意不取空指针内值我的解法:缺点:不应该直接用l1,l2遍历,因为无法回到原链表起始点,应该令创建指针p1,p2用其遍历链表。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * L...原创 2022-04-05 14:43:11 · 1170 阅读 · 0 评论 -
LeetCode刷题——412. Fizz Buzz(简单)
我的解法:没啥写头,不过我以为必须用数组呢结果不是class Solution {public: vector<string> fizzBuzz(int n) { string answer[n+1]; for(int i=1;i<n+1;i++){ if(!(i%3)&&!(i%5)){ answer[i]="FizzBuzz"; }else..原创 2022-04-12 18:12:35 · 503 阅读 · 1 评论 -
LeetCode刷题——206. 反转链表(简单)——迭代(双指针)
我的解法:双指针迭代/** * 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) : v...原创 2022-04-11 19:13:06 · 96 阅读 · 0 评论 -
LeetCode刷题——13. 罗马数字转整数(简单)——查表
按顺序做被第4题二分法绊咯半天,做个新手题单压压惊我的解法:分析对应书写规则(当前数与其右边数对比,若左>=右,则用作加法,否则用作减法),然后查表但内存消耗偏高,可借鉴下其他写法如用switch替代mapclass Solution {public: int romanToInt(string s) { map<char,int> excle={{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',5原创 2022-04-06 15:37:15 · 306 阅读 · 0 评论