当前位置:网站首页>LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
2022-08-02 14:11:00 【超级码力奥】
给你一个下标从 0 开始的字符串数组 nums ,其中每个字符串 长度相等 且只包含数字。
再给你一个下标从 0 开始的二维整数数组 queries ,其中 queries[i] = [ki, trimi] 。对于每个 queries[i] ,你需要:
将 nums 中每个数字 裁剪 到剩下 最右边 trimi 个数位。
在裁剪过后的数字中,找到 nums 中第 ki 小数字对应的 下标 。如果两个裁剪后数字一样大,那么下标 更小 的数字视为更小的数字。
将 nums 中每个数字恢复到原本字符串。
请你返回一个长度与 queries 相等的数组 answer,其中 answer[i]是第 i 次查询的结果。
提示:
裁剪到剩下 x 个数位的意思是不断删除最左边的数位,直到剩下 x 个数位。
nums 中的字符串可能会有前导 0 。
示例 1:
输入:nums = [“102”,“473”,“251”,“814”], queries = [[1,1],[2,3],[4,2],[1,2]]
输出:[2,2,1,0]
解释:
- 裁剪到只剩 1 个数位后,nums = [“2”,“3”,“1”,“4”] 。最小的数字是 1 ,下标为 2 。
- 裁剪到剩 3 个数位后,nums 没有变化。第 2 小的数字是 251 ,下标为 2 。
- 裁剪到剩 2 个数位后,nums = [“02”,“73”,“51”,“14”] 。第 4 小的数字是 73 ,下标为 1 。
- 裁剪到剩 2 个数位后,最小数字是 2 ,下标为 0 。
注意,裁剪后数字 “02” 值为 2 。
示例 2:
输入:nums = [“24”,“37”,“96”,“04”], queries = [[2,1],[2,2]]
输出:[3,0]
解释:
- 裁剪到剩 1 个数位,nums = [“4”,“7”,“6”,“4”] 。第 2 小的数字是 4 ,下标为 3 。
有两个 4 ,下标为 0 的 4 视为小于下标为 3 的 4 。 - 裁剪到剩 2 个数位,nums 不变。第二小的数字是 24 ,下标为 0 。
提示:
1 <= nums.length <= 100
1 <= nums[i].length <= 100
nums[i] 只包含数字。
所有 nums[i].length 的长度 相同 。
1 <= queries.length <= 100
queries[i].length == 2
1 <= ki <= nums.length
1 <= trimi <= nums[0].length
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/query-kth-smallest-trimmed-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
仔细分析完题意之后,由于数据量不大,
时间复杂度:O(qmnlogn),其中 q是数组 queries 的长度,n 是数组 nums 的长度,m 是每个 nums[i] 的长度。每次询问,都需要对一个长为 n 的数组排序,排序共发生 O(nlogn) 次比较,每次比较的耗时为O(m),故总的时间复杂度为 O(qmnlogn)。
空间复杂度:O(n)。返回值不计入空间复杂度。
这里可以看一下如何在排序中保留原始的下标信息
# 方法一:直接排序
ans = []
for k, trim in queries:
# 因为排序时,下标跟着变,如何在排序中保留原始的下表信息?
# 把字符串和对应的下标组合成一个pair, 然后对pair排序
pairs = sorted(zip([num[-trim:] for num in nums], range(len(nums))))
ans.append(pairs[k - 1][1])
return ans
这里还有一个就是,你以为在Python中写成一行很难?其实上面的摇身一变,就变成一行了,但是读起来更困难了,请看
class Solution:
def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
return [sorted(zip([num[-trim:] for num in nums], range(len(nums))))[k - 1][1] for k, trim in queries]
C++实现
class Solution {
public:
vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& qs) {
// 由于要和下标一块排序,所以考验的是C++语法
int n = nums.size(), m = qs.size();
vector<pair<string, int>> strs(n);
for(int i = 0; i < n; i ++) strs[i] = {
nums[i], i};
vector<int> res;
for(auto& q: qs)
{
int k = q[0], trim = q[1];
// 排序函数里边不加引用过不了,会变慢
sort(strs.begin(), strs.end(), [&](pair<string, int>& a, pair<string, int>& b){
// 优先比较数字大小
for(int i = a.first.size() - trim; i < a.first.size(); i ++){
if(a.first[i] < b.first[i])
return true;
else if(a.first[i] > b.first[i])
return false;
}
// 其次比较下标
return a.second < b.second;
});
res.push_back(strs[k-1].second);
}
return res;
}
};
边栏推荐
- Redis的线程模型
- Masters and Masters
- 用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
- Based on the least squares linear regression equation coefficient estimation
- What are IPV4 and IPV6?
- Configure clangd for vscode
- casbin模型
- cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
- STM32LL library - USART interrupt to receive variable length information
- Introduction to MATLAB drawing functions ezplot explanation
猜你喜欢

Win10安装了固态硬盘还是有明显卡顿怎么办?

How to solve Win11 without local users and groups

pygame绘制弧线

Redis常见面试题

基于最小二乘法的线性回归分析方程中系数的估计

Configure clangd for vscode

What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer

二叉排序树与 set、map

Detailed explanation of MATLAB drawing function fplot

flink+sklearn——使用jpmml实现flink上的机器学习模型部署
随机推荐
MATLAB drawing command fimplicit detailed introduction to drawing implicit function graphics
2.登录退出,登录状态检查,验证码
Mapreduce环境详细搭建和案例实现
第二十五章:一文掌握while循环
Win10无法连接打印机怎么办?不能使用打印机的解决方法
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
Yolov5 official code reading - prior to transmission
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
pygame image rotate continuously
利用plot_surface命令绘制复杂曲面入门详解
Codeforces Round #605 (Div. 3)
Spark及相关生态组件安装配置——快速回忆
Publish module to NPM should be how to operate?Solutions to problems and mistake
推开机电的大门《电路》(二):功率计算与判断
Please make sure you have the correct access rights and the repository exists. Problem solved
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
STM32LL库——USART中断接收不定长信息
Redis的线程模型