LeetCode周赛#235

2021-04-04 做题

传送门 T1:截断句子 思路 签到题,用python会比cpp简单很多。直接按空格split然后分片就行。 代码 ```python class Solution(object): def truncateSentence(self, s, k): """ :type s: str :type k: int :rtype: str """ ss = s.split(" ") if k == len(ss): return s ans = " ".join(ss[:k]) return ans ``` 赛后感觉写的太冗长了,可以精简成一句: ```python class Solution(object): def truncateSentence(self, …

阅读全文 →

LeetCode周赛#225

2021-01-26 做题

传送门 T1替换隐藏数字得到的最晚时间 思路 签到题,注意一些细节就行。 代码 ```cpp class Solution { public: string maximumTime(string time) { if (time[0] == '?' && time[1] >= '4' && time[1] != '?') time[0] = '1'; else if (time[0] == '?') time[0] = '2'; if (time[1] == '?' && time[0] == '2') …

阅读全文 →

LeetCode周赛#224

2021-01-17 做题

传送门 T1 可以形成最大正方形的矩形数目 思路 签到题,半分钟直接来。 代码 ```cpp class Solution { public: int countGoodRectangles(vector>& rectangles) { int ans = 0; int maxLen = -1; for(auto el : rectangles) { maxLen = max(maxLen, min(el[0], el[1])); } for(auto el : rectangles) { if(min(el[0], el[1]) == maxLen) { …

阅读全文 →

LeetCode周赛#220

2020-12-21 做题

传送门 T1重新格式化电话号码 思路 周赛经典的字符串处理题。难倒是不难,但是处理起来感觉比往期的签到题麻烦不少啊... 主要思路就是replace掉'-'与空格,然后每三个一组分片,塞进列表,最后再对单出来的元素特殊处理。 代码 ```python class Solution: def reformatNumber(self, number: str) -> str: number = number.replace('-', '').replace(' ', '') ans, N = [], len(number) for i in range(0, N, 3): ans.append(number[i:i+3]) if len(ans[-1]) == 1: last = ans.pop() sec_last = ans.pop() ans.append(sec_last[:2]) …

阅读全文 →

LeetCode周赛#218

2020-12-06 做题

传送门 好水的周赛啊,建议改名为Python赛 T1-设计 Goal 解析器 简单的字符串模拟题,别像我一样看到括号就栈栈栈qaq。 我是用Python replace搞的,这样会简洁不少,贴一个lc评论区C++简单模拟的。 Python ```python class Solution(object): def interpret(self, command): command = command.replace('()', 'o') command = command.replace('(al)', 'al') return command ``` C++ ```cpp class Solution { public: string interpret(string command) { string ans = ""; int i = 0; int …

阅读全文 →

LeetCode周赛#216

2020-11-24 做题

传送门 好久没写博客了啊...考试周真是烦人。216周赛可以说是最简单的一次周赛了吧,贪就vans了。 T1 检查两个字符串数组是否相等 签到打卡。 ```cpp class Solution { public: bool arrayStringsAreEqual(vector& word1, vector& word2) { string str1 = "", str2 = ""; for(auto el : word1) { str1 += el; } for(auto el : word2) { str2 += el; } return str1 == …

阅读全文 →

LeetCode周赛#203

2020-08-25 做题

传送门 t1 圆形赛道上经过次数最多的扇区 有一个圆形区域,被分成n块扇区,给出一系列起点与终点,问跑到最后经过次数最多的扇区是哪些。 赛时居然没过这题... 当时写了一个模拟,因为没处理好边界问题,死活过不去。赛后下来看了发现确实想复杂了。 我们要发现一个事实,因为赛道是圆形的,所以除了结尾的那一圈“不完整”的赛程,之前若干圈赛程都是没有意义的,因为每个扇区都同时累加了经过次数,“同时”在本题中是没有讨论意义的。所以我们的答案只用关注最后那不完整的赛程经过了哪些扇区。 值得注意的是,起点不一定是第一块扇区,所以要处理越界问题。同时,观察样例不难发现答案是有序的,所以还要对最终答案数组做一个排序。 ```cpp class Solution { public: vector mostVisited(int n, vector& rounds) { vectorans; int l = rounds[0]; int r = rounds[rounds.size()-1]; while(l != r) { if(l == n+1) { l %= n; continue; } ans.push_back(l++); } ans.push_back(r); …

阅读全文 →

LeetCode周赛#202

2020-08-16 做题

传送门 t1 存在连续三个奇数的数组 签到题,直接O(n²)遍历就能过。 ```cpp class Solution { public: bool threeConsecutiveOdds(vector& arr) { const int n = arr.size(); if(n < 3) return false; for(int i = 2; i < n; i++) { int cnt = 0; for(int j = i-2; j <= i; j++) …

阅读全文 →

LeetCode周赛#194

2020-06-22 做题

传送门 T1数组异或操作 签到题,直接翻译 ```cpp class Solution { public: int xorOperation(int n, int start) { vector nums; int ans; for(int i = 0; i < n; i++) nums.push_back(start + 2*i); ans = nums[0]; for(int i = 1; i < n; i++) ans ^= nums[i]; return ans; …

阅读全文 →

LeetCode周赛#193

2020-06-16 做题

传送门 T1一位数组的动态和 维护一个nums数组的前缀和,直接写。 ```cpp class Solution { public: vector runningSum(vector& nums) { vector pre; int n = nums.size(); pre.push_back(nums[0]); for(int i = 1; i < n; i++) { int tmp = pre[i-1] + nums[i]; pre.push_back(tmp); } return pre; } }; ``` T2不同整数的最小数目 题意很直观:给出一组整数arr与一个整数k,要求删除k个元素并使剩下的元素尽可能地少。 思路也很清晰,其实就是一个贪心策略。统计每个元素出现的次数,优先从出现次数少的开始删,最后剩下的元素个数就是答案。 考虑到数组中的元素范围是[0,1e9],所以用hash表来离散处理。将统计用的hash表以出现次数为关键字做排序然后从头开始删就可以了。 …

阅读全文 →

LeetCode周赛#192

2020-06-07 做题

传送门→ T1重新排列数组 签到题,按题意模拟就行 ```cpp class Solution { public: vector shuffle(vector& nums, int n) { vector ans; for(int i = 0; i < n; i++) { ans.push_back(nums[i]); ans.push_back(nums[n+i]); } return ans; } }; ``` T2 数组中的 k 个最强值 就是根据题目要求重新定义排序规则,在排序后取前k项即是答案 ```cpp int m; bool cmp(int a, …

阅读全文 →

LeetCode周赛#191

2020-06-04 做题

传送门→ T1 数组中两元素的最大乘积 签到题,两个for。 ```cpp class Solution { public: int maxProduct(vector& nums) { int n = nums.size(); int maxn = -1; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(i != j) maxn = max((nums[i]-1) * (nums[j]-1), …

阅读全文 →