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相关推荐

  1. LeetCode知识点总结 - 884

    LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...

  2. LeetCode知识点总结 - 868

    LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...

  3. LeetCode知识点总结 - 1413

    LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...

  4. LeetCode知识点总结 - 2073

    LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...

  5. LeetCode知识点总结 - 347

    LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...

  6. LeetCode知识点总结 - 997

    LeetCode 997. Find the Town Judge 考点 难度 Hash Table Easy 题目 In a town, there are n people labeled fro ...

  7. LeetCode知识点总结 - 606

    LeetCode 606. Construct String from Binary Tree 考点 难度 String Easy 题目 Given the root of a binary tree ...

  8. LeetCode知识点总结 - 844

    LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...

  9. LeetCode知识点总结 - 977

    LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...

最新文章

  1. Lucene系列-facet--转
  2. 为什么很多人说 Java 不适合编写桌面应用?
  3. 【算法】一个简单的支持向量机(SVM)原理
  4. RTC实时时钟实验(低功耗、纽扣电池供电)
  5. 前端模板引擎artTemplate---高性能JavaScript模板引擎
  6. python 除数总是提示为0_Python错误的处理方法
  7. Mac版正式上线剪映,适配用M1
  8. netscreen MIP 问题
  9. OpenSSL密码库算法笔记——第6.1章 密钥
  10. win7系统下文件夹重命名提示找不到该项目:的解决!
  11. MySQL - 唯一索引
  12. 人工智能数学基础---定积分2:定积分的性质
  13. ORACLE账户提示EXPIRED(GRACE)问题解决
  14. 如何解决“Cannot be opened because the developer cannot be verified”
  15. zabbix短信告警oracle,zabbix自定义脚本实现短信报警提醒
  16. 兔子繁衍问题—c语言
  17. type、dtype和astype
  18. JVM进阶(十二)——JAVA 可视化分析工具
  19. 【无标题】VisualC++逆序存放
  20. [ 物联网篇 ] 11 - NXP i.MX8M Mini 集成Mender OTA解决方案

热门文章

  1. python找与7相关的数_查找连续有七个“ 7”的所有10位质数-Python
  2. Android开发:标准体重计算器应用的…
  3. 还有10天自考就开始了,我应该怎么高效的复习?
  4. 堆料突破天花板 vivo X70 Pro+体验:定义高端影像旗舰
  5. 绝!2D素材3D效果!
  6. 复习-数据结构之线性表
  7. OpenHarmony轻量系统开发【1】初始OpenHarmony
  8. 河南工业大学计算机考研资料汇总
  9. IE8及以下IE浏览器支持canvas效果
  10. 1.0电机的分类及永磁同步电机的控制技术介绍