2025-03-31-01

This commit is contained in:
kaku 2025-03-31 12:58:00 +08:00
parent ffb1da1215
commit 0e25a6e997

View File

@ -370,6 +370,92 @@ func repeatedSubstringPattern(s string) bool {
~~~ ~~~
## 判断IP地址
~~~ go
import "strings"
/**
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
func solve( IP string ) string {
if isIpv4(IP) {
return "IPv4"
}
if isIpv6(IP) {
return "IPv6"
}
return "Neither"
}
func isIpv4(ip string) bool {
//首先判断字符串是不是大于71.1.1.1),首尾不能是 .
if len(ip) < 7 || ip[0] == '.' || ip[len(ip) - 1] == '.' {
return false
}
//分割之后长度不能少于4
strSlice := strings.Split(ip,".")
if len(strSlice) != 4 {
return false
}
for _, str := range strSlice {
k := len(str)
//开头不能是0
// str = []byte(str)
if k == 0 || str[0] == '0' && k > 1 || k > 3 {
return false
}
num := 0
for i:=0;i<len(str);i++{
//必须是数字
c := str[i]
if c < '0' || c > '9' {
return false
}
num = num * 10 + int((c-'0'))
//不能超过255
if num > 255 {
return false
}
}
}
return true
}
func isIpv6(ip string) bool {
//首先判断字符串是不是小于151.1.1.1),首尾不能是 .
if len(ip) < 7 || ip[0] == '.' || ip[len(ip) - 1] == '.' {
return false
}
strSlice := strings.Split(ip,":")
if len(strSlice) != 8 {
return false
}
for _, str := range strSlice {
k := len(str)
//开头不能是0
// str = []byte(str)
if k == 0 || k > 4 {
return false
}
for i:=0;i<len(str);i++{
c := string(str[i])
//必须是数字
if !((c <= "f" && c >= "a") || (c <= "F" && c >= "A") || (c >= "0" && c <= "9")) {
return false
}
}
}
return true
}
~~~
## 快速排序 ## 快速排序
@ -459,43 +545,28 @@ left, right := 0, len(nums)-1
## 字符串相加 ## 字符串相加
```go ```go
func max(a, b int) int { func solve(s string, t string) (ans string) {
if a > b { buf := make([]byte, 0, max(len(s), len(t))+1)
return a
}
return b
}
func addStrings(num1 string, num2 string) string {
buf := make([]byte, max(len(num1), len(num2)) + 1)
length := len(buf) - 1
add := 0 add := 0
for i, j := len(num1)-1, len(num2)-1; j >= 0 || i >= 0; { for i, j := len(s)-1, len(t)-1; i >= 0 || j >= 0; {
num := add
if i >= 0 { if i >= 0 {
num += int(num1[i] - '0') //转Int add += int(s[i] - '0')
i-- i--
} }
if j >= 0 { if j >= 0 {
num += int(num2[j] - '0') add += int(t[j] - '0')
j-- j--
} }
if num >= 10 { buf = append(buf, byte(add%10)+'0')
num %= 10 add /= 10
add = 1
} else {
add = 0
} }
buf[length] = byte(num+'0') if add > 0 {
length-- buf = append(buf, byte(add)+'0')
} }
if add != 0 { for _, v := range buf {
buf[length] = '1' ans = string(v) + ans
} }
if buf[0]==0{ return ans
buf=buf[1:]
}
return string(buf)
} }
``` ```