当前位置:网站首页>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

边栏推荐
- 即刻报名|飞桨黑客马拉松第三期等你挑战
- 解决问题:Unable to connect to Redis
- 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
- 探索Cassandra的去中心化分布式架构
- What are the positions of communication equipment manufacturers?
- Es FAQ summary
- Linux server development, detailed explanation of redis related commands and their principles
- 【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
- Leetcode 40: combined sum II
- Zsh shell adds automatic completion and syntax highlighting
猜你喜欢

Padavan manually installs PHP

Linux server development, MySQL transaction principle analysis

Who has docker to install MySQL locally?

Linux server development, SQL statements, indexes, views, stored procedures, triggers

Numbers that appear only once
![[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)](/img/39/cac2b5492d374da393569e2ab467a4.png)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)

Use and analysis of dot function in numpy

2022 tea master (intermediate) examination questions and mock examination
![[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替

即刻报名|飞桨黑客马拉松第三期等你挑战
随机推荐
Implementation of replacement function of shell script
[P2P] local packet capturing
Thinkcmf6.0 installation tutorial
探索干货篇!Apifox 建设思路
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
C语言二叉树与建堆
[webrtc] M98 screen and window acquisition
[unity] several ideas about circular motion of objects
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
Shell 脚本的替换功能实现
Es FAQ summary
[Stanford Jiwang cs144 project] lab3: tcpsender
3D reconstruction - stereo correction
Use and analysis of dot function in numpy
【VHDL 并行语句执行】
Pytorch(六) —— 模型调优tricks
Zsh shell adds automatic completion and syntax highlighting
Visualization Document Feb 12 16:42
Pytest+allure+jenkins environment -- completion of pit filling
Info | webrtc M97 update