当前位置:网站首页>LeetCode简单题之字符串中最大的 3 位相同数字
LeetCode简单题之字符串中最大的 3 位相同数字
2022-07-07 04:47:00 【·星辰大海】
题目
给你一个字符串 num ,表示一个大整数。如果一个整数满足下述所有条件,则认为该整数是一个 优质整数 :
该整数是 num 的一个长度为 3 的 子字符串 。
该整数由唯一一个数字重复 3 次组成。
以字符串形式返回 最大的优质整数 。如果不存在满足要求的整数,则返回一个空字符串 “” 。
注意:
子字符串 是字符串中的一个连续字符序列。
num 或优质整数中可能存在 前导零 。
示例 1:
输入:num = “6777133339”
输出:“777”
解释:num 中存在两个优质整数:“777” 和 “333” 。
“777” 是最大的那个,所以返回 “777” 。
示例 2:
输入:num = “2300019”
输出:“000”
解释:“000” 是唯一一个优质整数。
示例 3:
输入:num = “42352338”
输出:“”
解释:不存在长度为 3 且仅由一个唯一数字组成的整数。因此,不存在优质整数。
提示:
3 <= num.length <= 1000
num 仅由数字(0 - 9)组成
来源:力扣(LeetCode)
解题思路
这个题只需要做一个长度为3的滑动窗口然后遍历整个字符串,找出符合条件的最大的数字。
class Solution:
def largestGoodInteger(self, num: str) -> str:
MIN=''
for i in range(0,len(num)-2):
if num[i]==num[i+1] and num[i+1]==num[i+2]:
if MIN=='':
MIN=num[i]*3
else:
MIN=num[i]*3 if int(num[i]*3)>int(MIN) else MIN
return MIN

边栏推荐
- 这5个摸鱼神器太火了!程序员:知道了快删!
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- 2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
- 解决问题:Unable to connect to Redis
- SQL优化的魅力!从 30248s 到 0.001s
- What are the positions of communication equipment manufacturers?
- [experience sharing] how to expand the cloud service icon for Visio
- Problem solving: unable to connect to redis
- Linux server development, detailed explanation of redis related commands and their principles
- [UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
猜你喜欢

【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO

2022制冷与空调设备运行操作复训题库及答案

Codeforces Global Round 19
![[2022 actf] Web Topic recurrence](/img/e4/ab9a1771489d751ee73a79f151d374.png)
[2022 actf] Web Topic recurrence

Es FAQ summary
![[Stanford Jiwang cs144 project] lab4: tcpconnection](/img/fd/704d19287a12290f779cfc223c71c8.png)
[Stanford Jiwang cs144 project] lab4: tcpconnection

Common validation comments
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio

Niu Mei's mathematical problem --- combinatorial number

Why should we understand the trend of spot gold?
随机推荐
Operation suggestions for today's spot Silver
解决问题:Unable to connect to Redis
追风赶月莫停留,平芜尽处是春山
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
Linux server development, MySQL index principle and optimization
Why should we understand the trend of spot gold?
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Visualization Document Feb 12 16:42
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
[UTCTF2020]file header
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
[OBS] win capture requires winrt
Zhilian + AV, AITO asked M7 to do more than ideal one
What are the positions of communication equipment manufacturers?
探索Cassandra的去中心化分布式架构
[mathematical notes] radian
Linux server development, redis source code storage principle and data model