LeetCode知识点总结 - 1985
LeetCode 1985. Find the Kth Largest Integer in the Array
考点 | 难度 |
---|---|
Sorting | Medium |
题目
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.
思路
custom sort: 写一个用来比较strings的function。先比较长短,然后从最左位character开始比较。cmp_to_key( )
用来call custom sort的function。
答案
from functools import cmp_to_key
class Solution:def kthLargestNumber(self, nums: List[str], k: int) -> str:def sorter(x, y):n, m = len(x), len(y)if n < m: # if you want to keep x in left of y, return -1, here if len(x) is shorter, so it is smaller, so left # ['33', '4'], 4 to leftreturn -1elif n > m: # +1, if to keep in right of yreturn 1else:for i in range(n):if x[i] < y[i]: # if x[i] smaller, then leftreturn -1elif x[i] > y[i]: # else in right return 1else:continuereturn 0 # if both same, x==ykey = cmp_to_key(sorter)nums.sort(key=key, reverse=True)# print(nums)return nums[k-1]
LeetCode知识点总结 - 1985相关推荐
- LeetCode知识点总结 - 884
LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...
- LeetCode知识点总结 - 868
LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...
- LeetCode知识点总结 - 1413
LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...
- LeetCode知识点总结 - 2073
LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...
- LeetCode知识点总结 - 347
LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...
- LeetCode知识点总结 - 997
LeetCode 997. Find the Town Judge 考点 难度 Hash Table Easy 题目 In a town, there are n people labeled fro ...
- LeetCode知识点总结 - 606
LeetCode 606. Construct String from Binary Tree 考点 难度 String Easy 题目 Given the root of a binary tree ...
- LeetCode知识点总结 - 844
LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...
- LeetCode知识点总结 - 977
LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...
最新文章
- Lucene系列-facet--转
- 为什么很多人说 Java 不适合编写桌面应用?
- 【算法】一个简单的支持向量机(SVM)原理
- RTC实时时钟实验(低功耗、纽扣电池供电)
- 前端模板引擎artTemplate---高性能JavaScript模板引擎
- python 除数总是提示为0_Python错误的处理方法
- Mac版正式上线剪映,适配用M1
- netscreen MIP 问题
- OpenSSL密码库算法笔记——第6.1章 密钥
- win7系统下文件夹重命名提示找不到该项目:的解决!
- MySQL - 唯一索引
- 人工智能数学基础---定积分2:定积分的性质
- ORACLE账户提示EXPIRED(GRACE)问题解决
- 如何解决“Cannot be opened because the developer cannot be verified”
- zabbix短信告警oracle,zabbix自定义脚本实现短信报警提醒
- 兔子繁衍问题—c语言
- type、dtype和astype
- JVM进阶(十二)——JAVA 可视化分析工具
- 【无标题】VisualC++逆序存放
- [ 物联网篇 ] 11 - NXP i.MX8M Mini 集成Mender OTA解决方案