diff --git a/question/q002.md b/question/q002.md index f6a9fcd..e03e2c2 100644 --- a/question/q002.md +++ b/question/q002.md @@ -55,10 +55,46 @@ func isUniqueString2(s string) bool { } ``` +通过位运算判断 + +``` +func isUniqString3(s string) bool { + if len(s) == 0 || len(s) > 3000 { + return false + } + // 256 个字符 256 = 64 + 64 + 64 + 64 + var mark1, mark2, mark3, mark4 uint64 + var mark *uint64 + for _, r := range s { + n := uint64(r) + if n < 64 { + mark = &mark1 + } else if n < 128 { + mark = &mark2 + n -= 64 + } else if n < 192 { + mark = &mark3 + n -= 128 + } else { + mark = &mark4 + n -= 192 + } + if (*mark)&(1<