加载中...
1985-找出数组中的第 K 大整数(Find the Kth Largest Integer in the Array)
发表于:2021-12-03 | 分类: 中等
字数统计: 787 | 阅读时长: 3分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/find-the-kth-largest-integer-in-the-array

英文原文

You are given an array of strings nums and an integer k. Each string in nums represents an integer without leading zeros.

Return the string that represents the kth largest integer in nums.

Note: Duplicate numbers should be counted distinctly. For example, if nums is ["1","2","2"], "2" is the first largest integer, "2" is the second-largest integer, and "1" is the third-largest integer.

 

Example 1:

Input: nums = ["3","6","7","10"], k = 4
Output: "3"
Explanation:
The numbers in nums sorted in non-decreasing order are ["3","6","7","10"].
The 4th largest integer in nums is "3".

Example 2:

Input: nums = ["2","21","12","1"], k = 3
Output: "2"
Explanation:
The numbers in nums sorted in non-decreasing order are ["1","2","12","21"].
The 3rd largest integer in nums is "2".

Example 3:

Input: nums = ["0","0"], k = 2
Output: "0"
Explanation:
The numbers in nums sorted in non-decreasing order are ["0","0"].
The 2nd largest integer in nums is "0".

 

Constraints:

  • 1 <= k <= nums.length <= 104
  • 1 <= nums[i].length <= 100
  • nums[i] consists of only digits.
  • nums[i] will not have any leading zeros.

中文题目

给你一个字符串数组 nums 和一个整数 knums 中的每个字符串都表示一个不含前导零的整数。

返回 nums 中表示第 k 大整数的字符串。

注意:重复的数字在统计时会视为不同元素考虑。例如,如果 nums["1","2","2"],那么 "2" 是最大的整数,"2" 是第二大的整数,"1" 是第三大的整数。

 

示例 1:

输入:nums = ["3","6","7","10"], k = 4
输出:"3"
解释:
nums 中的数字按非递减顺序排列为 ["3","6","7","10"]
其中第 4 大整数是 "3"

示例 2:

输入:nums = ["2","21","12","1"], k = 3
输出:"2"
解释:
nums 中的数字按非递减顺序排列为 ["1","2","12","21"]
其中第 3 大整数是 "2"

示例 3:

输入:nums = ["0","0"], k = 2
输出:"0"
解释:
nums 中的数字按非递减顺序排列为 ["0","0"]
其中第 2 大整数是 "0"

 

提示:

  • 1 <= k <= nums.length <= 104
  • 1 <= nums[i].length <= 100
  • nums[i] 仅由数字组成
  • nums[i] 不含任何前导零

通过代码

高赞题解

找出数组中的第 K 大整数

​ 第二道题。

​ 第一想到的是堆排序和手撕快排,又想了想,第二题好像不会整这种活,并且力扣里的这类题直接快排也能过那就写一个指定排序方法的快排就ok了。

模拟

class Solution {
public:
    string kthLargestNumber(vector<string>& nums, int k) {
        sort(nums.begin(), nums.end(), 
            [](string s1, string s2)->bool{   //用lambda表达式写一个临时函数用来比对大小
        		if(s1.size() != s2.size()) return s1.size() > s2.size(); //先比字符串的长度
        		else return s1 > s2;  //再比字符串的大小
        });
        return nums[k - 1]; //返回第k个大的
    }
};

统计信息

通过次数 提交次数 AC比率
6229 15069 41.3%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1984-学生分数的最小差值(Minimum Difference Between Highest and Lowest of K Scores)
下一篇:
1987-不同的好子序列数目(Number of Unique Good Subsequences)
本文目录
本文目录