From a9132cb7ffa71ae9631a90a3d1ebcc12edd9d62b Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 20 Jun 2021 16:39:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0093.复原IP地址 go版本 --- problems/0093.复原IP地址.md | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index a8b9a215..1c8e4069 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -367,6 +367,47 @@ var restoreIpAddresses = function(s) { } }; ``` +Go: +> 回溯(对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字)) + +```go +func restoreIpAddresses(s string) []string { + var res,path []string + backTracking(s,path,0,&res) + return res +} +func backTracking(s string,path []string,startIndex int,res *[]string){ + //终止条件 + if startIndex==len(s)&&len(path)==4{ + tmpIpString:=path[0]+"."+path[1]+"."+path[2]+"."+path[3] + *res=append(*res,tmpIpString) + } + for i:=startIndex;i1&&s[startIndex]=='0'{//对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字) + return false + } + if checkInt>255{ + return false + } + return true +} + +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)