From 21a2b9cd69307b16c3b5faeecc36f84a6a05dc2e Mon Sep 17 00:00:00 2001 From: VicRen Date: Thu, 26 Nov 2020 08:36:38 +0800 Subject: [PATCH] 20201126 --- 20201126/main.go | 74 ++++++++++++++++++++++++++ 20201126/main_test.go | 121 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 20201126/main.go create mode 100644 20201126/main_test.go diff --git a/20201126/main.go b/20201126/main.go new file mode 100644 index 0000000..279a949 --- /dev/null +++ b/20201126/main.go @@ -0,0 +1,74 @@ +package main + +func main() { + +} + +func findMaxSubLen(src string) int { + l := 0 + r := 0 + max := 0 + sm := make(map[uint8]struct{}) + for { + if l == len(src) { + return max + } + for { + if r == len(src) { + return max + } + c := src[r] + if _, found := sm[c]; found { + sm = make(map[uint8]struct{}) + l++ + r = l + break + } else { + sm[c] = struct{}{} + if len(sm) > max { + max = len(sm) + } + r++ + } + } + } +} + +func findMaxSubLen2(src string) int { + l := 0 + r := 0 + max := 0 + var ss []uint8 + for { + if l == len(src) { + return max + } + for { + if r == len(src) { + return max + } + c := src[r] + if i := indexOfValue(c, ss); i != -1 { + ss = ss[i:] + l += i + 1 + r++ + break + } else { + ss = append(ss, c) + if len(ss) > max { + max = len(ss) + } + r++ + } + } + } +} + +func indexOfValue(n uint8, src []uint8) int { + for i := len(src) - 1; i >= 0; i-- { + if src[i] == n { + return i + } + } + return -1 +} diff --git a/20201126/main_test.go b/20201126/main_test.go new file mode 100644 index 0000000..553a3bc --- /dev/null +++ b/20201126/main_test.go @@ -0,0 +1,121 @@ +package main + +import "testing" + +func Test_findMaxSubLen(t *testing.T) { + type args struct { + src string + } + tests := []struct { + name string + args args + want int + }{ + { + "example_1", + args{"abccefg"}, + 4, + }, + { + "example_2", + args{"bbbbb"}, + 1, + }, + { + "example_3", + args{"pwwkew"}, + 3, + }, + { + "example_4", + args{""}, + 0, + }, + { + "example_5", + args{"2"}, + 1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := findMaxSubLen(tt.args.src); got != tt.want { + t.Errorf("findMaxSubLen() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_findMaxSubLen2(t *testing.T) { + type args struct { + src string + } + tests := []struct { + name string + args args + want int + }{ + { + "example_1", + args{"abccefg"}, + 4, + }, + { + "example_2", + args{"bbbbb"}, + 1, + }, + { + "example_3", + args{"pwwkew"}, + 3, + }, + { + "example_4", + args{""}, + 0, + }, + { + "example_5", + args{"2"}, + 1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := findMaxSubLen2(tt.args.src); got != tt.want { + t.Errorf("findMaxSubLen2() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_hasValue(t *testing.T) { + type args struct { + n uint8 + src []uint8 + } + tests := []struct { + name string + args args + want int + }{ + { + "test1", + args{1, []uint8{1, 2}}, + 0, + }, + { + "test2", + args{1, []uint8{3, 2}}, + -1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := indexOfValue(tt.args.n, tt.args.src); got != tt.want { + t.Errorf("indexOfValue() = %v, want %v", got, tt.want) + } + }) + } +}