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

边栏推荐
- MySQL multi column index (composite index) features and usage scenarios
- PHP exports millions of data
- Rust versus go (which is my preferred language?)
- Zhilian + AV, AITO asked M7 to do more than ideal one
- Hands on deep learning (IV) -- convolutional neural network CNN
- 贝叶斯定律
- 探索干货篇!Apifox 建设思路
- C语言二叉树与建堆
- [Stanford Jiwang cs144 project] lab4: tcpconnection
- 【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
猜你喜欢

Sign up now | oar hacker marathon phase III, waiting for your challenge

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions

Leetcode 40: combined sum II

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)

Qt学习26 布局管理综合实例

A bit of knowledge - about Apple Certified MFI

Common validation comments

探索Cassandra的去中心化分布式架构
![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)
[UTCTF2020]file header
随机推荐
芯片资料 网站 易特创芯
Téléchargement des données de conception des puces
Ansible
Implementation of replacement function of shell script
Codeforce c.strange test and acwing
Binary tree and heap building in C language
Pytest+allure+jenkins environment -- completion of pit filling
C语言二叉树与建堆
Linux server development, MySQL index principle and optimization
Wechat applet data binding multiple data
php导出百万数据
Installing postgresql11 database under centos7
Linux server development, redis source code storage principle and data model
[SUCTF 2019]Game
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Leetcode 90: subset II
太真实了,原来自己一直没有富裕起来是有原因的
JSON data flattening pd json_ normalize
Linux server development, MySQL stored procedures, functions and triggers
2022焊工(初级)判断题及在线模拟考试