当前位置:网站首页>模拟卷Leetcode【普通】093. 复原 IP 地址
模拟卷Leetcode【普通】093. 复原 IP 地址
2022-07-29 05:39:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
093. 复原 IP 地址
有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 ‘.’ 分隔。
例如:“0.1.2.201” 和 “192.168.1.1” 是 有效 IP 地址,但是 “0.011.255.245”、“192.168.1.312” 和 “[email protected]” 是 无效 IP 地址。
给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 ‘.’ 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。
示例 1:
输入:s = “25525511135”
输出:[“255.255.11.135”,“255.255.111.35”]
示例 2:
输入:s = “0000”
输出:[“0.0.0.0”]
示例 3:
输入:s = “101023”
输出:[“1.0.10.23”,“1.0.102.3”,“10.1.0.23”,“10.10.2.3”,“101.0.2.3”]
提示:
1 <= s.length <= 20
s 仅由数字组成
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/restore-ip-addresses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
def isLegal(ssss):
if any(s.startswith('0') for s in ssss):return False
return all(0<=int(s)<=255 for s in ssss)
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
l = len(s)
if l<4:return []
res = []
for id1 in range(1,l-2):
for id2 in range(id1+1,l-1):
for id3 in range(id2+1,l):
ssss = [s[:id1],s[id1:id2],s[id2:id3],s[id3:]]
if isLegal(ssss):
res.append('.'.join(*ssss))
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [List2Node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Annotation
- Best example of amortized cost
- Jetpack Compose 中的键盘处理
- 如何画出优秀的架构图
- 5g service interface and reference point
- 【技能积累】presentation实用技巧积累,常用句式
- Biased lock, lightweight lock test tool class level related commands
- 成长为架构师途中的一些思考
- 【经验】通过跳板机远程连接内网服务器的相关配置
- Share some tips for better code, smooth coding and improve efficiency
猜你喜欢

C language memory stack and heap usage

Navicat for Oracle Cannot create oci environment

STP spanning tree principle and example of election rules

C语言数据类型

Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)

CDM—码分复用(简单易懂)

10种常见的软件架构模式

The core of openresty and cosocket

Software definition boundary SDP

CNN-卷积神经网络
随机推荐
Navicat for Oracle Cannot create oci environment
AbstractQueuedSynchronizer(AQS) 之共享锁源码浅读
Thinkphp5 frequently asked questions
The core of openresty and cosocket
JVM之垃圾回收机制(GC)
Condition 条件对象源码浅读
finally 和 return 的执行顺序
Callable 的使用
数据库多表查询 联合查询 增删改查
'function VTable for error: undefined reference to... 'cause and solution of the problem
Understanding of access, hybrid and trunk modes
Shallow reading of condition object source code
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
SDN topology discovery principle
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
OpenResty的核心与cosocket
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
Let the computer run only one program setting
Not so simple singleton mode
猜数字//第一次使用生成随机数