Leetcode 排序
Leetcode中与排序相关的题目
努利!奋斗!
这个作者很懒,什么都没留下…
展开
-
Leetcode 2274. Maximum Consecutive Floors Without Special Floors
题目解法:排序class Solution {public: int maxConsecutive(int bottom, int top, vector<int>& special) { sort(special.begin(),special.end()); if(bottom != special[0]) special.insert(special.begin(),bottom-1); if(top != speci原创 2022-05-23 09:17:33 · 98 阅读 · 0 评论 -
Leetcode 524. Longest Word in Dictionary through Deleting (python+cpp)
Leetcode 524. Longest Word in Dictionary through Deleting题目解法:灵活运用双指针题目解法:灵活运用双指针总体思路比较简单,遍历d,判断每个d中的target是不是符合条件,并选择长度最长中lexicographical order最小的保存这边主要对比一下python和C++在比较lexicographical order的不同。...原创 2020-04-15 08:15:52 · 433 阅读 · 0 评论 -
【Leetcode 1288. Remove Covered Intervals】
题目解法把interval按照第一个元素生序第二个元素降序进行排序,逐个比较即可class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key = lambda x:(x[0],-x[1])) # print(intervals) prev_end = intervals[0][1]原创 2022-01-11 12:25:57 · 74 阅读 · 0 评论 -
Leetcode 179. Largest Number (python+cpp)
题目解法:自定义排序排序的时候比较两个数字形成的字符串,将两个字符串前后放置形成不同的串之间比较大小,可以直接得出这两个串在最后形成的字符串的相对位置python版本class LargerNumKey(str): def __lt__(x, y): return x+y > y+x class Solution: def largestNumber(self, nums): largest_num = ''.join(sor原创 2021-01-13 09:22:26 · 400 阅读 · 0 评论 -
Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (cpp)
题目解法1:暴力class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { vector<int> ans; int count; for(int i=0;i<nums.size();i++){ count = 0; for(int j=0;j原创 2021-01-03 05:51:45 · 159 阅读 · 0 评论 -
Leetcode 1366. Rank Teams by Votes (python+cpp)
题目这题看起来不难,但实际上有挺多启发点的错误解法:这题有个理解误区,一开始以为是按照分数来排序。比如第一位得五分,第二位的四分这样,把所有的字符分数计算出来然后排序,于是就有了下面的错误解法class Solution {public: string rankTeams(vector<string>& votes) { vector<pair<int,int>> scores(26,{0,0}); int n原创 2021-01-03 05:34:39 · 599 阅读 · 0 评论 -
力扣 16.06. 最小差
题目题目链接:https://leetcode-cn.com/problems/smallest-difference-lcci/解法1:排序+双指针class Solution: def smallestDifference(self, a: List[int], b: List[int]) -> int: a.sort() b.sort() p1 = p2 = 0 ans = float('inf')原创 2020-12-19 02:55:34 · 123 阅读 · 1 评论 -
Leetcode 1424. Diagonal Traverse II (python)
题目解法1:排序这道题关键在于在同一条对角线上的元素行数和列数的和是相同的。所以将每个元素按照(r+c,r,val)储存,然后先按照第一个元素升序排序,其次按照第二个元素降序排序即可这边有个要注意的点是这两个排序是一个升序一个排序,而且要在第一个排序的基础上进行排序,在python里面需要用sorted函数,并且secondary element,再排primary element,详细参见:https://docs.python.org/3/howto/sorting.htmlclass So原创 2020-11-26 05:50:10 · 252 阅读 · 0 评论 -
Leetcode 1451. Rearrange Words in a Sentence (python)
题目解法1:同时sort单词长度和indexclass Solution: def arrangeWords(self, text: str) -> str: text = text.split() len_ind_list = [] for i, word in enumerate(text): len_ind_list.append((len(word),i,word)) le原创 2020-11-20 11:33:46 · 151 阅读 · 0 评论 -
Leetcode 1630. Arithmetic Subarrays (python)
Leetcode 1630. Arithmetic Subarrays题目解法1:sort解法2:利用set题目解法1:sortclass Solution: def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]: def check(array): array.sort() dis = a原创 2020-10-27 09:59:54 · 147 阅读 · 0 评论 -
Leetcode 347. Top K Frequent Elements & 692. Top K Frequent Words (python)
题目解法1:排序 (NlogN)class Solution(object): def topKFrequent(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ count = collections.Counter(nums) candidates = [(-v,key) fo原创 2020-10-14 00:52:44 · 154 阅读 · 0 评论 -
Leetcode 1057. Campus Bikes (python)
Leetcode 1057. Campus Bikes题目解法1:暴力排序解法2:heap代替暴力排序题目解法1:暴力排序首先理解这道题目一定要到位。不是说从bike去找worker,也不是从worker去找bike。如果一开始陷入了这种错误思路,就会想到BFS,把题目搞得很复杂。正确的理解是这样的,找到距离最近的pair,如果距离最近的pair有多个,那么先要选小的worker_id,比如worker1和worker2和bike1距离一样,那么要选worker1.然后如果worker1和bike原创 2020-09-25 14:20:34 · 370 阅读 · 0 评论 -
Leetcode 977. Squares of a Sorted Array (python+cpp)
Leetcode 977. Squares of a Sorted Array题目解法1:two pass with two pointer解法2:improved two pass with two pointers解法3:one pass with two pointers题目解法1:two pass with two pointerclass Solution: def sortedSquares(self, A: List[int]) -> List[int]:原创 2020-09-03 13:07:56 · 245 阅读 · 0 评论 -
Leetcode 75. Sort Colors (python+cpp)
Leetcode 75. Sort Colors题目解法1:two pass解法2:三指针法题目解法1:two passfirst pass统计0,1,2出现的次数,second pass对不同的范围内进行重新赋值,这样需要额外的空间储存三种颜色出现的频率,代码如下:class Solution: def sortColors(self, nums: List[int]) -> None: """ Do not return anything, mod原创 2020-08-03 11:52:51 · 148 阅读 · 0 评论 -
Leetcode 215. Kth Largest Element in an Array (python+cpp)
Leetcode 215. Kth Largest Element in an Array题目解法1:brutal force解法2:heap,最小堆解法3:快速选择题目解法1:brutal force每次取出一个最大的,直到取到第K个为止。时间复杂度n*nclass Solution: def findKthLargest(self, nums: List[int], k: int) -> int: for i in range(k-1):原创 2020-08-02 08:50:03 · 343 阅读 · 0 评论