当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Difference between CNAME record and a record
- LDAP brief description and unified authentication description
- The difference between pairs and ipairs
- API for using the new date class of instant
- 5G控制面协议之N2接口
- 10 frequently asked JVM questions in interviews
- 崔雪婷老师最优化理论与方法课程笔记 00 写在前面
- Invalid access control
- 王树尧老师运筹学课程笔记 02 高等数学基础
- Enterprise manager cannot connect to the database instance in Oracle10g solution
猜你喜欢

【经验】通过跳板机远程连接内网服务器的相关配置

Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers

C语言数据类型

Share some tips for better code, smooth coding and improve efficiency

10 frequently asked JVM questions in interviews

ping 原理

Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)

vscode通过remotessh结合xdebug远程调试php解决方案

MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear

矩阵分解与梯度下降
随机推荐
Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
Software definition boundary SDP
API for using the new date class of instant
吴恩达老师机器学习课程笔记 05 Octave教程
损失函数——交叉熵损失函数
王树尧老师运筹学课程笔记 01 导学与绪论
10道面试常问JVM题
Talk about tcp/ip protocol? And the role of each layer?
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
JMM memory model concept
王树尧老师运筹学课程笔记 00 写在前面
网络工具中的“瑞士军刀”-nc
Base64与File之间的相互转化
Shallow reading of shared lock source code of abstractqueuedsynchronizer (AQS)
Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
多线程并发下的指令重排问题
吴恩达老师机器学习课程笔记 00 写在前面
Teacher wangshuyao's notes on operations research 01 guidance and introduction
王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
Teacher Cui Xueting's course notes on optimization theory and methods 00 are written in the front