From dbde6b107df1baa8616d82037d6d4c3a5d321e36 Mon Sep 17 00:00:00 2001 From: VicRen Date: Tue, 5 Jan 2021 09:00:19 +0800 Subject: [PATCH] 20210105 --- 20210105/main.go | 21 +++++++++++++++++++++ 20210105/main_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 20210105/main.go create mode 100644 20210105/main_test.go diff --git a/20210105/main.go b/20210105/main.go new file mode 100644 index 0000000..01247bc --- /dev/null +++ b/20210105/main.go @@ -0,0 +1,21 @@ +package main + +func main() { + +} + +func countBalance(s string) int { + count, rCount, lCount := 0, 0, 0 + for i := 0; i < len(s); i++ { + if s[i] == 'R' { + rCount++ + } else if s[i] == 'L' { + lCount++ + } + if rCount > 0 && rCount == lCount { + lCount, rCount = 0, 0 + count++ + } + } + return count +} diff --git a/20210105/main_test.go b/20210105/main_test.go new file mode 100644 index 0000000..4b2f378 --- /dev/null +++ b/20210105/main_test.go @@ -0,0 +1,29 @@ +package main + +import "testing" + +func Test_countBalance(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want int + }{ + { + "test1", + args{ + "RLRRLLRLRL", + }, + 4, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := countBalance(tt.args.s); got != tt.want { + t.Errorf("countBalance() = %v, want %v", got, tt.want) + } + }) + } +}