From e0d85d59311d4423ce75d387591bdf3fc1ccf745 Mon Sep 17 00:00:00 2001 From: guowenfeng Date: Fri, 28 May 2021 10:56:07 +0800 Subject: [PATCH] init --- .idea/do_some_fxxking_test.iml | 9 + .idea/leetcode/editor.xml | 257 ++++++++++++++++++ .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 37 +++ ...\345\200\274\347\273\231 []interface{}.md" | 51 ++++ README.md | 1 + go.mod | 9 + go.sum | 43 +++ ...44\346\225\260\344\271\213\345\222\214.go" | 83 ++++++ .../main.go" | 36 +++ .../main.go" | 36 +++ .../main.go" | 81 ++++++ .../main.go" | 26 ++ .../main.go" | 15 + ...5.\346\234\200\345\260\217\346\240\210.go" | 59 ++++ .../main.go" | 50 ++++ .../main.go" | 17 ++ .../main.go" | 39 +++ ...44\346\225\260\347\233\270\345\212\240.go" | 134 +++++++++ .../mian.go" | 50 ++++ ...77\347\232\204\346\225\260\351\207\217.go" | 39 +++ .../main.go" | 48 ++++ .../main.go" | 30 ++ .../main.go" | 37 +++ .../main.go" | 50 ++++ .../main.go" | 56 ++++ .../main.go" | 17 ++ .../main.go" | 14 + ...50\345\271\263\346\226\271\346\225\260.go" | 55 ++++ .../main.go" | 19 ++ .../main.go" | 39 +++ .../mian.go" | 30 ++ .../main.go" | 41 +++ .../main.go" | 21 ++ .../main.go" | 21 ++ .../main.go" | 39 +++ .../main.go" | 21 ++ .../main.go" | 36 +++ .../main.go" | 15 + .../main.go" | 53 ++++ .../main.go" | 66 +++++ ...00\350\275\254\347\233\230\351\224\201.go" | 43 +++ .../main.go" | 32 +++ ...52\347\216\257\351\230\237\345\210\227.go" | 94 +++++++ main.go | 39 +++ practise/testByteBuffer/main.go | 19 ++ practise/test_01/main.go | 23 ++ practise/test_rate/main.go | 116 ++++++++ practise/test_strings/main.go | 34 +++ the_way_to_go/chapter_10/main.go | 52 ++++ the_way_to_go/chapter_11/chapter_11.go | 43 +++ the_way_to_go/chapter_12/main.go | 21 ++ the_way_to_go/chapter_13/main.go | 29 ++ the_way_to_go/chapter_14/main.go | 52 ++++ the_way_to_go/chapter_5/string_conversion2.go | 25 ++ the_way_to_go/chapter_5/switch.go | 26 ++ the_way_to_go/chapter_6/func.go | 16 ++ the_way_to_go/chapter_7/practice_7_9.go | 80 ++++++ the_way_to_go/chapter_9/main.go | 24 ++ 60 files changed, 2562 insertions(+) create mode 100644 .idea/do_some_fxxking_test.iml create mode 100644 .idea/leetcode/editor.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 "Doc/# Golang \344\270\272\344\273\200\344\271\210\344\270\215\350\203\275\347\233\264\346\216\245\345\260\206\344\273\273\346\204\217\347\261\273\345\236\213\346\225\260\347\273\204\350\265\213\345\200\274\347\273\231 []interface{}.md" create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 "leet_code/1.\344\270\244\346\225\260\344\271\213\345\222\214.go" create mode 100644 "leet_code/14.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/main.go" create mode 100644 "leet_code/141.\347\216\257\345\275\242\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/142.\347\216\257\345\275\242\351\223\276\350\241\250II/main.go" create mode 100644 "leet_code/151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/main.go" create mode 100644 "leet_code/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\345\200\274/main.go" create mode 100644 "leet_code/155.\346\234\200\345\260\217\346\240\210/155.\346\234\200\345\260\217\346\240\210.go" create mode 100644 "leet_code/160.\347\233\270\344\272\244\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/167.\344\270\244\346\225\260\344\271\213\345\222\214/main.go" create mode 100644 "leet_code/19.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\257\274\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271/main.go" create mode 100644 "leet_code/2.\344\270\244\346\225\260\347\233\270\345\212\240.go" create mode 100644 "leet_code/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/mian.go" create mode 100644 "leet_code/200.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217.go" create mode 100644 "leet_code/203.\347\247\273\345\207\272\351\223\276\350\241\250\345\205\203\347\264\240/main.go" create mode 100644 "leet_code/206.\345\217\215\350\275\254\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/main.go" create mode 100644 "leet_code/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/26.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271/main.go" create mode 100644 "leet_code/27.\347\247\273\351\231\244\345\205\203\347\264\240/main.go" create mode 100644 "leet_code/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.go" create mode 100644 "leet_code/283.\347\247\273\345\212\250\351\233\266/main.go" create mode 100644 "leet_code/328.\345\245\207\345\201\266\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/35.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/mian.go" create mode 100644 "leet_code/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.go" create mode 100644 "leet_code/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/main.go" create mode 100644 "leet_code/485.\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260/main.go" create mode 100644 "leet_code/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/main.go" create mode 100644 "leet_code/557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/main.go" create mode 100644 "leet_code/56.\345\220\210\345\271\266\345\214\272\351\227\264/main.go" create mode 100644 "leet_code/561.\346\225\260\347\273\204\346\213\206\345\210\206/main.go" create mode 100644 "leet_code/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.go" create mode 100644 "leet_code/724.\345\257\273\346\211\276\346\225\260\347\273\204\344\270\255\345\277\203\344\270\213\346\240\207/main.go" create mode 100644 "leet_code/753.\346\211\223\345\274\200\350\275\254\347\233\230\351\224\201.go" create mode 100644 "leet_code/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/main.go" create mode 100644 "leet_code/\350\256\276\350\256\241\345\276\252\347\216\257\351\230\237\345\210\227.go" create mode 100644 main.go create mode 100644 practise/testByteBuffer/main.go create mode 100644 practise/test_01/main.go create mode 100644 practise/test_rate/main.go create mode 100644 practise/test_strings/main.go create mode 100644 the_way_to_go/chapter_10/main.go create mode 100644 the_way_to_go/chapter_11/chapter_11.go create mode 100644 the_way_to_go/chapter_12/main.go create mode 100644 the_way_to_go/chapter_13/main.go create mode 100644 the_way_to_go/chapter_14/main.go create mode 100644 the_way_to_go/chapter_5/string_conversion2.go create mode 100644 the_way_to_go/chapter_5/switch.go create mode 100644 the_way_to_go/chapter_6/func.go create mode 100644 the_way_to_go/chapter_7/practice_7_9.go create mode 100644 the_way_to_go/chapter_9/main.go diff --git a/.idea/do_some_fxxking_test.iml b/.idea/do_some_fxxking_test.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/do_some_fxxking_test.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/leetcode/editor.xml b/.idea/leetcode/editor.xml new file mode 100644 index 0000000..2f18cdb --- /dev/null +++ b/.idea/leetcode/editor.xml @@ -0,0 +1,257 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5ee219b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..e733e3f --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/Doc/# Golang \344\270\272\344\273\200\344\271\210\344\270\215\350\203\275\347\233\264\346\216\245\345\260\206\344\273\273\346\204\217\347\261\273\345\236\213\346\225\260\347\273\204\350\265\213\345\200\274\347\273\231 []interface{}.md" "b/Doc/# Golang \344\270\272\344\273\200\344\271\210\344\270\215\350\203\275\347\233\264\346\216\245\345\260\206\344\273\273\346\204\217\347\261\273\345\236\213\346\225\260\347\273\204\350\265\213\345\200\274\347\273\231 []interface{}.md" new file mode 100644 index 0000000..1eba211 --- /dev/null +++ "b/Doc/# Golang \344\270\272\344\273\200\344\271\210\344\270\215\350\203\275\347\233\264\346\216\245\345\260\206\344\273\273\346\204\217\347\261\273\345\236\213\346\225\260\347\273\204\350\265\213\345\200\274\347\273\231 []interface{}.md" @@ -0,0 +1,51 @@ +# Golang 为什么不能直接将任意类型数组赋值给 []interface{} + +> 本篇参考: https://github.com/golang/go/wiki/InterfaceSlice + +当我在写 go 代码时,想用 []interface{} 类型来接受一个任意类型的数组,从而进行泛型操作时,发现直接赋值会发生错误,导致 panic。 + +```go + dataSlice := []int{1, 2, 3, 4, 5, 6} + var interfaceSlice []interface{} = dataSlice +``` + +> Cannot use 'dataSlice' (type []int) as type []interface{} + +IDE 会报以上错误。 + +原本我以为 interface{} 可以接收任何类型,[]interface{} 数组就应该可以接收任何类型的数组才对。 + +## Why? + +先说结论:**[]interface{} 不是 interface{},它是一个元素类型为 interface{} 的切片。** + +主要是因为 []interface{} 有特殊的内存布局。先看一下 interface{} 的内存布局: + +![image-20210428153736553](/Users/guowenfeng/Library/Application Support/typora-user-images/image-20210428153736553.png) + +如果是 []interface{} 就是 N 个上面的布局。可是普通类型的切片内存布局是: + +![image-20210428155048225](/Users/guowenfeng/Library/Application Support/typora-user-images/image-20210428155048225.png) + +所以,根本原因就是两个东西的内存布局不同,所以无法直接赋值。 + +## What can I do instead? + +两种情况: + +1. 你只是需要用一个容器去接收任意类型的切片,然后在进行索引之前就会将它转成原来的类型你只需要: + + ```go + var tmp interface{} = dataArr + ``` + +2. 你的确需要将任意类型的数组转成 []interface{} ,然后去索引 []interface{} ,你只能遍历赋值: + + ```go + for index, data := range dataArr { + interfaceSlice = append(interfaceSlice, data) + // 这里可以直接在声明时就指定 []interface{} 的长度 + // 用 interfaceSlice[index] = data 来代替会减少系统创建内存时消耗的时间 + } + ``` + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a49eacf --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# golang_daily_test diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d7cffc4 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module do_some_fxxking_test + +go 1.14 + +require ( + github.com/hashicorp/go-uuid v1.0.2 // indirect + golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect + gopkg.in/eapache/queue.v1 v1.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..89b95e7 --- /dev/null +++ b/go.sum @@ -0,0 +1,43 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= +github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= +github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE= +golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/eapache/queue.v1 v1.1.0 h1:EldqoJEGtXYiVCMRo2C9mePO2UUGnYn2+qLmlQSqPdc= +gopkg.in/eapache/queue.v1 v1.1.0/go.mod h1:wNtmx1/O7kZSR9zNT1TTOJ7GLpm3Vn7srzlfylFbQwU= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git "a/leet_code/1.\344\270\244\346\225\260\344\271\213\345\222\214.go" "b/leet_code/1.\344\270\244\346\225\260\344\271\213\345\222\214.go" new file mode 100644 index 0000000..25d041c --- /dev/null +++ "b/leet_code/1.\344\270\244\346\225\260\344\271\213\345\222\214.go" @@ -0,0 +1,83 @@ +package leet_code +//给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。 +// +// 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 +// +// 你可以按任意顺序返回答案。 +// +// +// +// 示例 1: +// +// +//输入:nums = [2,7,11,15], target = 9 +//输出:[0,1] +//解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 +// +// +// 示例 2: +// +// +//输入:nums = [3,2,4], target = 6 +//输出:[1,2] +// +// +// 示例 3: +// +// +//输入:nums = [3,3], target = 6 +//输出:[0,1] +// +// +// +// +// 提示: +// +// +// 2 <= nums.length <= 103 +// -109 <= nums[i] <= 109 +// -109 <= target <= 109 +// 只会存在一个有效答案 +// +// Related Topics 数组 哈希表 +// 👍 10692 👎 0 + + + + + +// 使用暴力解法,时间复杂度 n^2 +func twoSum(nums []int, target int) []int { + res := make([]int, 2) + for i := 0; i < len(nums); i++ { + for j := i + 1; j < len(nums); j++ { + if nums[i] + nums[j] == target { + res[0] = i + res[1] = j + } + } + } + return res +} + +// 使用哈希方法解决,时间复杂度 n*logn +// 初始化一个 map,遍历 nums +// 每次使用 target - nums[i],得到一个值在 map 中找这个 key +// 如果没找到就把 nums[i]-i 放入 map +// 如果找到就返回 [i, map[target - nums[i]]] +func TwoSumHash(nums []int, target int) []int { + hashMap := make(map[int]int, 0) + res := make([]int, 2) + + for i := 0; i < len(nums); i++ { + if index, ok := hashMap[target - nums[i]]; ok { + res[0] = index + res[1] = i + break + } else { + hashMap[nums[i]] = i + } + } + + return res +} \ No newline at end of file diff --git "a/leet_code/14.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/main.go" "b/leet_code/14.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/main.go" new file mode 100644 index 0000000..a09f8da --- /dev/null +++ "b/leet_code/14.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/main.go" @@ -0,0 +1,36 @@ +package main + +func longestCommonPrefix(strs []string) string { + if len(strs) == 0 { + return "" + } + + res := "" + for i := 0; i < len(strs[0]); i++ { + ch := strs[0][i] + + isSame := true + jumpOut := false + for j := 1; j < len(strs); j++ { + if i >= len(strs[j]) { + jumpOut = true + break + } + if strs[j][i] != ch { + isSame = false + } + } + + if jumpOut { + break + } + + if isSame { + res += string(ch) + } else { + break + } + } + + return res +} diff --git "a/leet_code/141.\347\216\257\345\275\242\351\223\276\350\241\250/main.go" "b/leet_code/141.\347\216\257\345\275\242\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..73d4f5c --- /dev/null +++ "b/leet_code/141.\347\216\257\345\275\242\351\223\276\350\241\250/main.go" @@ -0,0 +1,36 @@ +package main + +//Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func hasCycle(head *ListNode) bool { + if head == nil { + return false + } + + slow := head + fast := head + + for { + slow = (*slow).Next + if slow == nil { + return false + } + fast = (*fast).Next + if fast == nil { + return false + } + fast = (*fast).Next + if fast == nil { + return false + } + + if slow == fast { + return true + } + + } +} diff --git "a/leet_code/142.\347\216\257\345\275\242\351\223\276\350\241\250II/main.go" "b/leet_code/142.\347\216\257\345\275\242\351\223\276\350\241\250II/main.go" new file mode 100644 index 0000000..a2d6aea --- /dev/null +++ "b/leet_code/142.\347\216\257\345\275\242\351\223\276\350\241\250II/main.go" @@ -0,0 +1,81 @@ +package main + +// Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + + + +func detectCycle(head *ListNode) *ListNode { + node, ok := hasCycle(head) + if !ok { + return nil + } + + left := head + right := node.Next + node.Next = nil + + leftLen := getListLength(left) + rightLen := getListLength(right) + + if leftLen > rightLen { + stepNum := leftLen - rightLen + for step := 0; step < stepNum; step++ { + left = left.Next + } + } else { + stepNum := rightLen - leftLen + for step := 0; step < stepNum; step++ { + right = right.Next + } + } + + for left != right { + left = left.Next + right = right.Next + } + + return left +} + +func getListLength(head *ListNode) int { + length := 0 + for head != nil { + length++ + head = head.Next + } + + return length +} + +func hasCycle(head *ListNode) (*ListNode, bool) { + if head == nil { + return nil, false + } + + slow := head + fast := head + + for { + slow = (*slow).Next + if slow == nil { + return nil, false + } + fast = (*fast).Next + if fast == nil { + return nil, false + } + fast = (*fast).Next + if fast == nil { + return nil, false + } + + if slow == fast { + return slow, true + } + + } +} \ No newline at end of file diff --git "a/leet_code/151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/main.go" "b/leet_code/151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/main.go" new file mode 100644 index 0000000..ffe1bf4 --- /dev/null +++ "b/leet_code/151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/main.go" @@ -0,0 +1,26 @@ +package main + +import "strings" + +func reverseWords(s string) string { + s = strings.TrimSpace(s) + words := strings.Split(s, " ") + + res := "" + + for i := len(words) - 1; i >= 0; i-- { + if words[i] != "" { + res += strings.TrimSpace(words[i]) + " " + } + } + + if len(res) > 1 { + res = res[:len(res) - 1] + } + + return res +} + +func main() { + reverseWords("a good example") +} \ No newline at end of file diff --git "a/leet_code/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\345\200\274/main.go" "b/leet_code/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\345\200\274/main.go" new file mode 100644 index 0000000..0f63582 --- /dev/null +++ "b/leet_code/153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\345\200\274/main.go" @@ -0,0 +1,15 @@ +package main + +import "math" + +func findMin(nums []int) int { + res := math.MinInt32 + + for _, num := range nums { + if res > num { + res = num + } + } + + return res +} diff --git "a/leet_code/155.\346\234\200\345\260\217\346\240\210/155.\346\234\200\345\260\217\346\240\210.go" "b/leet_code/155.\346\234\200\345\260\217\346\240\210/155.\346\234\200\345\260\217\346\240\210.go" new file mode 100644 index 0000000..2e1e060 --- /dev/null +++ "b/leet_code/155.\346\234\200\345\260\217\346\240\210/155.\346\234\200\345\260\217\346\240\210.go" @@ -0,0 +1,59 @@ +package main + +import "fmt" + +type MinStack struct { + min []int + stack []int +} + +/** initialize your data structure here. */ +func Constructor() MinStack { + return MinStack{ + min: make([]int, 0), + stack: make([]int, 0), + } +} + +func (this *MinStack) Push(val int) { + if len(this.min) == 0 || this.min[len(this.min) - 1] >= val { + this.min = append(this.min, val) + } + this.stack = append(this.stack, val) +} + +func (this *MinStack) Pop() { + if this.min[len(this.min) - 1] == this.stack[len(this.stack) - 1] { + this.min = this.min[:len(this.min) - 1] + } + + this.stack = this.stack[:len(this.stack)-1] +} + +func (this *MinStack) Top() int { + return this.stack[len(this.stack)-1] +} + +func (this *MinStack) GetMin() int { + return this.min[len(this.min) - 1] +} + +/** + * Your MinStack object will be instantiated and called as such: + * obj := Constructor(); + * obj.Push(val); + * obj.Pop(); + * param_3 := obj.Top(); + * param_4 := obj.GetMin(); + */ + +func main() { + stack := Constructor() + stack.Push(-2) + stack.Push(0) + stack.Push(-3) + fmt.Println( stack.GetMin()) + stack.Pop() + fmt.Println(stack.Top()) + fmt.Println(stack.GetMin()) +} diff --git "a/leet_code/160.\347\233\270\344\272\244\351\223\276\350\241\250/main.go" "b/leet_code/160.\347\233\270\344\272\244\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..7728701 --- /dev/null +++ "b/leet_code/160.\347\233\270\344\272\244\351\223\276\350\241\250/main.go" @@ -0,0 +1,50 @@ +package main + +type ListNode struct { + Val int + Next *ListNode +} + +func getIntersectionNode(headA, headB *ListNode) *ListNode { + if headA == nil || headB == nil { + return nil + } + + left := headA + right := headB + + leftLen := getListLength(left) + rightLen := getListLength(right) + + if leftLen > rightLen { + stepNum := leftLen - rightLen + for step := 0; step < stepNum; step++ { + left = left.Next + } + } else { + stepNum := rightLen - leftLen + for step := 0; step < stepNum; step++ { + right = right.Next + } + } + + for left != right { + if left == nil || right == nil { + return nil + } + left = left.Next + right = right.Next + } + + return left +} + +func getListLength(head *ListNode) int { + length := 0 + for head != nil { + length++ + head = head.Next + } + + return length +} \ No newline at end of file diff --git "a/leet_code/167.\344\270\244\346\225\260\344\271\213\345\222\214/main.go" "b/leet_code/167.\344\270\244\346\225\260\344\271\213\345\222\214/main.go" new file mode 100644 index 0000000..741567b --- /dev/null +++ "b/leet_code/167.\344\270\244\346\225\260\344\271\213\345\222\214/main.go" @@ -0,0 +1,17 @@ +package main + +func twoSum(numbers []int, target int) []int { + valueSlot := make(map[int]int, len(numbers)) + + for index, num := range numbers { + valueSlot[num] = index + } + + for leftIndex, num := range numbers { + if index, ok := valueSlot[target - num]; ok { + return []int{leftIndex, index} + } + } + + return []int{} +} diff --git "a/leet_code/19.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\257\274\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271/main.go" "b/leet_code/19.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\257\274\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271/main.go" new file mode 100644 index 0000000..780c40d --- /dev/null +++ "b/leet_code/19.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\257\274\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271/main.go" @@ -0,0 +1,39 @@ +package main + +//Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func removeNthFromEnd(head *ListNode, n int) *ListNode { + if head == nil { + return nil + } + + listLen := getListLength(head) + + newHead := &ListNode{ + Val: 0, + Next: head, + } + cur := newHead + + for step := 0; step < listLen - n; step++ { + cur = cur.Next + } + + cur.Next = cur.Next.Next + + return newHead.Next +} + +func getListLength(head *ListNode) int { + length := 0 + for head != nil { + length++ + head = head.Next + } + + return length +} \ No newline at end of file diff --git "a/leet_code/2.\344\270\244\346\225\260\347\233\270\345\212\240.go" "b/leet_code/2.\344\270\244\346\225\260\347\233\270\345\212\240.go" new file mode 100644 index 0000000..f9dc1de --- /dev/null +++ "b/leet_code/2.\344\270\244\346\225\260\347\233\270\345\212\240.go" @@ -0,0 +1,134 @@ +package leet_code + +//给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。 +// +// 请你将两个数相加,并以相同形式返回一个表示和的链表。 +// +// 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。 +// +// +// +// 示例 1: +// +// +//输入:l1 = [2,4,3], l2 = [5,6,4] +//输出:[7,0,8] +//解释:342 + 465 = 807. +// +// +// 示例 2: +// +// +//输入:l1 = [0], l2 = [0] +//输出:[0] +// +// +// 示例 3: +// +// +//输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] +//输出:[8,9,9,9,0,0,0,1] +// +// +// +// +// 提示: +// +// +// 每个链表中的节点数在范围 [1, 100] 内 +// 0 <= Node.val <= 9 +// 题目数据保证列表表示的数字不含前导零 +// +// Related Topics 递归 链表 数学 +// 👍 5942 👎 0 + +// Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +// 新建一个 List 用来存每一位上之和的结果 +// 一个数字表示进位 +// 遍历长的 List +// 解答失败: 测试用例:[9,9,9,9,9,9,9] [9,9,9,9] 测试结果:[8,9,9,9,0,0,0] 期望结果:[8,9,9,9,0,0,0,1] stdout: +func AddTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { + if l1 == nil { + return l2 + } else if l2 == nil { + return l1 + } + + l1Len := getListNodeLen(l1) + l2Len := getListNodeLen(l2) + + longerList := l1 + shorterList := l2 + if l1Len < l2Len { + longerList = l2 + shorterList = l1 + } + + res := ListNode{ + -1, nil, + } + list := &res + carry := 0 + for ; shorterList != nil && longerList != nil; { + tmpSum := longerList.Val + shorterList.Val + carry + carry = 0 + if tmpSum > 9 { + carry = tmpSum / 10 + tmpSum %= 10 + } + + if list.Val == -1 { + list.Val = tmpSum + } else { + list.Next = new(ListNode) + list = list.Next + list.Val = tmpSum + } + + longerList = (*longerList).Next + shorterList = (*shorterList).Next + } + + if longerList != nil && carry != 0 { + for ; longerList != nil; longerList = (*longerList).Next { + if carry == 0 { + list.Next = longerList + break + } + + tmpSum := longerList.Val + carry + carry = 0 + if tmpSum > 9 { + carry = tmpSum / 10 + tmpSum %= 10 + } + + list.Next = new(ListNode) + list = list.Next + list.Val = tmpSum + } + } else if longerList != nil && carry == 0 { + list.Next = longerList + } + if carry != 0 { + list.Next = new(ListNode) + list = list.Next + list.Val = carry + } + + return &res +} + +func getListNodeLen(list *ListNode) int { + count := 0 + for node := list; node != nil; node = (*node).Next { + count++ + } + + return count +} \ No newline at end of file diff --git "a/leet_code/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/mian.go" "b/leet_code/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/mian.go" new file mode 100644 index 0000000..666b129 --- /dev/null +++ "b/leet_code/20.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/mian.go" @@ -0,0 +1,50 @@ +package main + +func getTopValue(stack []byte) byte{ + if len(stack) == 0 { + return 0 + } + + return stack[len(stack) - 1] +} + +func isValid(s string) bool { + stack := make([]byte, 0) + + for _, v := range s { + switch v { + case '{', '[', '(': + stack = append(stack, byte(v)) + case '}': + if topValue := getTopValue(stack); topValue == '{' { + stack = stack[:len(stack) - 1] + } else { + return false + } + case ']': + if topValue := getTopValue(stack); topValue == '[' { + stack = stack[:len(stack) - 1] + } else { + return false + } + case ')': + if topValue := getTopValue(stack); topValue == '(' { + stack = stack[:len(stack) - 1] + } else { + return false + } + default: + return false + } + } + + if len(stack) == 0 { + return true + } + + return false +} + +func main() { + +} diff --git "a/leet_code/200.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217.go" "b/leet_code/200.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217.go" new file mode 100644 index 0000000..b797099 --- /dev/null +++ "b/leet_code/200.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217.go" @@ -0,0 +1,39 @@ +package leet_code + +func numIslands(grid [][]byte) int { + if grid == nil || len(grid) == 0 { + return -1 + } + + m := len(grid) + n := len(grid[0]) + islandCount := 0 + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] == '1' { + islandCount++ + + dfsIsland(i, j, &grid) + } + } + } + + return islandCount +} + + +func dfsIsland(i, j int, grid *[][]byte) { + if i >= len(*grid) || j >= len((*grid)[0]) || + i < 0 || j < 0 || + (*grid)[i][j] == '0' { + return + } + + (*grid)[i][j] = '0' + + dfsIsland(i + 1, j, grid) + dfsIsland(i - 1, j, grid) + dfsIsland(i, j + 1, grid) + dfsIsland(i, j - 1, grid) +} \ No newline at end of file diff --git "a/leet_code/203.\347\247\273\345\207\272\351\223\276\350\241\250\345\205\203\347\264\240/main.go" "b/leet_code/203.\347\247\273\345\207\272\351\223\276\350\241\250\345\205\203\347\264\240/main.go" new file mode 100644 index 0000000..c7c58bb --- /dev/null +++ "b/leet_code/203.\347\247\273\345\207\272\351\223\276\350\241\250\345\205\203\347\264\240/main.go" @@ -0,0 +1,48 @@ +package main + +import "fmt" + +//Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func removeElements(head *ListNode, val int) *ListNode { + if head == nil { + return nil + } + + prevHead := &ListNode{ + Val: 0, + Next: head, + } + prev := prevHead + + for cur := head; cur != nil;{ + if cur.Val == val { + prev.Next = cur.Next + } else { + prev = prev.Next + } + + cur = cur.Next + } + + return prevHead.Next +} + +func main() { + head := &ListNode{ + Val: 1, + Next: &ListNode{ + Val: 2, + Next: &ListNode{ + Val: 3, + Next: nil, + }, + }, + } + + fmt.Println(removeElements(head, 3)) +} \ No newline at end of file diff --git "a/leet_code/206.\345\217\215\350\275\254\351\223\276\350\241\250/main.go" "b/leet_code/206.\345\217\215\350\275\254\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..69735e0 --- /dev/null +++ "b/leet_code/206.\345\217\215\350\275\254\351\223\276\350\241\250/main.go" @@ -0,0 +1,30 @@ +package main + +// Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func reverseList(head *ListNode) *ListNode { + if head == nil { + return nil + } + + resHead := &ListNode{ + Val: 0, + Next: nil, + } + + for head != nil { + cur := &ListNode{ + Val: head.Val, + Next: resHead.Next, + } + resHead.Next = cur + + head = head.Next + } + + return resHead +} diff --git "a/leet_code/209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/main.go" "b/leet_code/209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/main.go" new file mode 100644 index 0000000..357786c --- /dev/null +++ "b/leet_code/209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204/main.go" @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "math" +) + +func minSubArrayLen(target int, nums []int) int { + if len(nums) == 0 { + return 0 + } + minLenth := len(nums) + 1 + sum := 0 + + + for i := 0; i < len(nums); i++ { + for j := i; j < len(nums); j++ { + sum += nums[j] + + if sum >= target { + minLenth = int(math.Min(float64(j - i + 1), float64(minLenth))) + sum = 0 + break + } + } + sum = 0 + } + + if minLenth == len(nums) + 1 { + return 0 + } + return minLenth +} + +func main() { + fmt.Println(minSubArrayLen(11, []int{1,1,1,1,1,1,1,1})) +} \ No newline at end of file diff --git "a/leet_code/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/main.go" "b/leet_code/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..4479dc1 --- /dev/null +++ "b/leet_code/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/main.go" @@ -0,0 +1,50 @@ +package main + +//* Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { + if l1 == nil { + return l2 + } else if l2 == nil { + return l1 + } + + res := new(ListNode) + + cur1 := l1 + cur2 := l2 + cur3 := res + + + for cur1 != nil && cur2 != nil { + if cur1.Val < cur2.Val { + tmp := ListNode{ + Val: cur1.Val, + Next: nil, + } + cur3.Next = &tmp + cur1 = cur1.Next + } else { + tmp := ListNode{ + Val: cur2.Val, + Next: nil, + } + cur3.Next = &tmp + cur2 = cur2.Next + } + + cur3 = cur3.Next + } + + if cur1 == nil { + cur3.Next = cur2 + } else if cur2 == nil { + cur3.Next = cur1 + } + + return res.Next +} diff --git "a/leet_code/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.go" "b/leet_code/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..918888f --- /dev/null +++ "b/leet_code/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.go" @@ -0,0 +1,56 @@ +package main + +// Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func isPalindrome(head *ListNode) bool { + if head == nil { + return true + } + + stack := make([]int, 0) + length := getListLen(head) + if length == 1 { + return true + } + + cur := head + + for i := 0; i < length / 2; i++ { + stack = append(stack, cur.Val) + cur = cur.Next + } + + if length % 2 != 0 { + cur = cur.Next + } + + for cur != nil { + if stack[len(stack) - 1] != cur.Val { + return false + } + + stack = stack[:len(stack) - 1] + cur = cur.Next + } + + if len(stack) == 0 { + return true + } + + return false +} + +func getListLen(head *ListNode) int { + length := 0 + + for head != nil { + length++ + head = head.Next + } + + return length +} diff --git "a/leet_code/26.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271/main.go" "b/leet_code/26.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271/main.go" new file mode 100644 index 0000000..2955e85 --- /dev/null +++ "b/leet_code/26.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271/main.go" @@ -0,0 +1,17 @@ +package main + +func removeDuplicates(nums []int) int { + slowIndex := 0 + fastIndex := 0 + + for fastIndex < len(nums) { + if nums[slowIndex] == nums[fastIndex] { + fastIndex++ + } else { + slowIndex++ + nums[slowIndex] = nums[fastIndex] + } + } + + return slowIndex + 1 +} diff --git "a/leet_code/27.\347\247\273\351\231\244\345\205\203\347\264\240/main.go" "b/leet_code/27.\347\247\273\351\231\244\345\205\203\347\264\240/main.go" new file mode 100644 index 0000000..7279e55 --- /dev/null +++ "b/leet_code/27.\347\247\273\351\231\244\345\205\203\347\264\240/main.go" @@ -0,0 +1,14 @@ +package main + +func removeElement(nums []int, val int) int { + slow := 0 + + for fast := 0; fast < len(nums); fast++ { + if nums[fast] != val { + nums[slow] = nums[fast] + slow++ + } + } + + return slow +} diff --git "a/leet_code/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.go" "b/leet_code/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.go" new file mode 100644 index 0000000..be75e01 --- /dev/null +++ "b/leet_code/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.go" @@ -0,0 +1,55 @@ +package leet_code + +import "math" + +func numSquares(n int) int { + queue := make([]int, 0) + visited := make(map[int]byte, 0) + + queue = append(queue, 0) + visited[0] = '0' + + level := 0 + + for len(queue) != 0 { + size := len(queue) + level++ + + for i := 0; i < size; i++ { + digit := queue[0] + queue = queue[1:] + + for j := 1; j <= n; j++ { + nodeValue := digit + j * j + + if nodeValue == n { + return level + } else if nodeValue > n { + break + } + + if _, ok := visited[nodeValue]; !ok { + queue = append(queue, nodeValue) + visited[nodeValue] = '0' + } + } + } + } + + return level +} + +func numSquaresDp(n int) int { + dp := make([]int, n + 1) + dp[0] = 0 + + for i := 1; i < n; i++ { + dp[i] = i + + for j := 1; j * j <= i; j++ { + dp[i] = int(math.Min(float64(dp[i]), float64(dp[i - j * j] + 1))) + } + } + + return dp[n] +} \ No newline at end of file diff --git "a/leet_code/283.\347\247\273\345\212\250\351\233\266/main.go" "b/leet_code/283.\347\247\273\345\212\250\351\233\266/main.go" new file mode 100644 index 0000000..c321bd8 --- /dev/null +++ "b/leet_code/283.\347\247\273\345\212\250\351\233\266/main.go" @@ -0,0 +1,19 @@ +package main + +func moveZeroes(nums []int) { + slowIndex := 0 + fastIndex := 0 + + for fastIndex < len(nums) { + if nums[fastIndex] != 0 { + nums[slowIndex] = nums[fastIndex] + slowIndex++ + } + fastIndex++ + } + + for slowIndex < len(nums) { + nums[slowIndex] = 0 + slowIndex++ + } +} diff --git "a/leet_code/328.\345\245\207\345\201\266\351\223\276\350\241\250/main.go" "b/leet_code/328.\345\245\207\345\201\266\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..ce9fd05 --- /dev/null +++ "b/leet_code/328.\345\245\207\345\201\266\351\223\276\350\241\250/main.go" @@ -0,0 +1,39 @@ +package main + +//Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func oddEvenList(head *ListNode) *ListNode { + if head == nil { + return nil + } + + oddHead := &ListNode{} + oddCur := oddHead + evenHead := &ListNode{} + evenCur := evenHead + + index := 1 + + for cur := head; cur != nil; { + if index % 2 != 0 { + oddCur.Next = cur + oddCur = oddCur.Next + cur = cur.Next + oddCur.Next = nil + } else { + evenCur.Next = cur + evenCur = evenCur.Next + cur = cur.Next + evenCur.Next = nil + } + index++ + } + + oddCur.Next = evenHead.Next + + return oddHead.Next +} diff --git "a/leet_code/35.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/mian.go" "b/leet_code/35.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/mian.go" new file mode 100644 index 0000000..0f2cc70 --- /dev/null +++ "b/leet_code/35.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/mian.go" @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func searchInsert(nums []int, target int) int { + if len(nums) == 0 { + return 0 + } + + left := 0 + right := len(nums) + + for right > left { + mid := (right + left) / 2 + if nums[mid] == target { + return mid + } else if nums[mid] > target { + right = mid + + } else { + left = mid + 1 + + } + } + return left +} + +func main() { + fmt.Println(searchInsert([]int{1, 3, 5, 6}, 4)) +} diff --git "a/leet_code/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.go" "b/leet_code/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.go" new file mode 100644 index 0000000..07c2e54 --- /dev/null +++ "b/leet_code/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.go" @@ -0,0 +1,41 @@ +package main + +import "fmt" + +func decodeString(s string) string { + nums := make([]int, 0) + strs := make([]string, 0) + + num := 0 + str := "" + + for _, curByte := range s { + // 如果 curByte 为数字,将num * 10 再加 curByte + if curByte >= '0' && curByte <= '9' { + num = num * 10 + int(curByte - '0') + } else if (curByte >= 'a' && curByte <= 'z') || (curByte >= 'A' && curByte <= 'Z') { + str += string(curByte) + } else if curByte == '[' { + nums = append(nums, num) + num = 0 + strs = append(strs, str) + str = "" + } else { + times := nums[len(nums) - 1] + nums = nums[:len(nums) - 1] + + for i := 0; i < times; i++ { + strs[len(strs) - 1] += str + } + + str = strs[len(strs) - 1] + strs = strs[:len(strs) - 1] + } + } + + return str +} + +func main() { + fmt.Println(decodeString("2[a3[c]]")) +} \ No newline at end of file diff --git "a/leet_code/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/main.go" "b/leet_code/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/main.go" new file mode 100644 index 0000000..f345381 --- /dev/null +++ "b/leet_code/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/main.go" @@ -0,0 +1,21 @@ +package main + +func longestPalindrome(s string) int { + charCount := make(map[byte]int) + + for _, ch := range s { + charCount[byte(ch)]++ + } + + res := 0 + + for _, v := range charCount { + if res & 1 != 0 && v & 1 != 0 { + res += v - 1 + } else { + res += v + } + } + + return res +} \ No newline at end of file diff --git "a/leet_code/485.\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260/main.go" "b/leet_code/485.\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260/main.go" new file mode 100644 index 0000000..a4866f5 --- /dev/null +++ "b/leet_code/485.\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260/main.go" @@ -0,0 +1,21 @@ +package main + +import "math" + +func findMaxConsecutiveOnes(nums []int) int { + count := 0 + res := 0 + + for _, num := range nums { + if num != 0 { + count ++ + } else { + res = int(math.Max(float64(res), float64(count))) + count = 0 + } + } + + res = int(math.Max(float64(res), float64(count))) + + return res +} \ No newline at end of file diff --git "a/leet_code/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/main.go" "b/leet_code/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/main.go" new file mode 100644 index 0000000..7b1d7da --- /dev/null +++ "b/leet_code/5.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/main.go" @@ -0,0 +1,39 @@ +package main + +import "fmt" + +func longestPalindrome(s string) string { + if len(s) < 2 { + return s + } + + dp := make([][]bool, len(s)) + for index := range dp { + dp[index] = make([]bool, len(s)) + } + dp[0][0] = true + + ans := s[:1] + maxLen := 1 + + for right := 1; right < len(s); right++ { + for left := 0; left < right; left++ { + // 如果左右相等 --> 如果两个字符之间相隔 0 或 1 个字符,那么就是回文 + // 如果两个字符中间夹着的字符串是回文,那么他们也是回文 + if (s[left] == s[right]) && (right - left <= 2 || dp[right - 1][left + 1]) { + dp[right][left] = true + + palindromeStrLen := right - left + 1 + if palindromeStrLen > maxLen { + maxLen = palindromeStrLen + ans = s[left:right + 1] + } + } + } + } + return ans +} + +func main() { + fmt.Println(longestPalindrome("babad")) +} \ No newline at end of file diff --git "a/leet_code/557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/main.go" "b/leet_code/557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/main.go" new file mode 100644 index 0000000..bde288b --- /dev/null +++ "b/leet_code/557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/main.go" @@ -0,0 +1,21 @@ +package main + +import "strings" + +func reverse(s string) string { + res := "" + for i := len(s) - 1; i >= 0; i-- { + res += string(s[i]) + } + return res +} + +func reverseWords(s string) string { + words := strings.Split(s, " ") + + for index := range words { + words[index] = reverse(words[index]) + } + + return strings.Join(words, " ") +} diff --git "a/leet_code/56.\345\220\210\345\271\266\345\214\272\351\227\264/main.go" "b/leet_code/56.\345\220\210\345\271\266\345\214\272\351\227\264/main.go" new file mode 100644 index 0000000..d57d108 --- /dev/null +++ "b/leet_code/56.\345\220\210\345\271\266\345\214\272\351\227\264/main.go" @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "math" + "sort" +) + +func merge(intervals [][]int) [][]int { + if len(intervals) == 1 { + return intervals + } + + sort.Slice(intervals, func(i, j int) bool { + return intervals[i][0] < intervals[j][0] + }) + + + res := make([][]int, 0) + res = append(res, intervals[0]) + j := 0 + for i := j + 1; i < len(intervals); i++ { + if intervals[i][0] <= res[j][1] { + res[j][1] = int(math.Max(float64(res[j][1]), float64(intervals[i][1]))) + } else { + res = append(res, intervals[i]) + j++ + } + } + + return res +} + +func main() { + fmt.Println(merge([][]int{{8, 10}, {2, 6}, {1, 3}, {15, 18}})) +} diff --git "a/leet_code/561.\346\225\260\347\273\204\346\213\206\345\210\206/main.go" "b/leet_code/561.\346\225\260\347\273\204\346\213\206\345\210\206/main.go" new file mode 100644 index 0000000..696d68b --- /dev/null +++ "b/leet_code/561.\346\225\260\347\273\204\346\213\206\345\210\206/main.go" @@ -0,0 +1,15 @@ +package main + +import "sort" + +func arrayPairSum(nums []int) int { + sort.Ints(nums) + + res := 0 + + for i := 0; i < len(nums); i += 2 { + res += nums[i] + } + + return res +} \ No newline at end of file diff --git "a/leet_code/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.go" "b/leet_code/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.go" new file mode 100644 index 0000000..dcc21fe --- /dev/null +++ "b/leet_code/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.go" @@ -0,0 +1,53 @@ +package main + +// Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +func rotateRight(head *ListNode, k int) *ListNode { + if head == nil { + return head + } + + listLen := getListLen(head) + k = k % listLen + + + for i := 0; i < k; i++ { + head = lastNodeToFirst(head) + } + + return head +} + +func lastNodeToFirst(head *ListNode) *ListNode { + preHead := new(ListNode) + preHead.Next = head + + preLastNode := preHead + lastNode := head + + for lastNode.Next != nil { + preLastNode = lastNode + lastNode = lastNode.Next + } + + preLastNode.Next = nil + lastNode.Next = head + head = lastNode + + return head +} + +func getListLen(head *ListNode) int { + length := 0 + + for head != nil { + length++ + head = head.Next + } + + return length +} diff --git "a/leet_code/724.\345\257\273\346\211\276\346\225\260\347\273\204\344\270\255\345\277\203\344\270\213\346\240\207/main.go" "b/leet_code/724.\345\257\273\346\211\276\346\225\260\347\273\204\344\270\255\345\277\203\344\270\213\346\240\207/main.go" new file mode 100644 index 0000000..58c6e67 --- /dev/null +++ "b/leet_code/724.\345\257\273\346\211\276\346\225\260\347\273\204\344\270\255\345\277\203\344\270\213\346\240\207/main.go" @@ -0,0 +1,66 @@ +package main + +import "fmt" + +// 1. 暴力解法 +func pivotIndex(nums []int) int { + if len(nums) == 0 { + return 0 + } + + for index := 0; index < len(nums); index++ { + left := getArraySum(nums, 0, index) + right := getArraySum(nums, index + 1, len(nums)) + + if left - right == 0 { + return index + } + } + + return -1 +} + +func getArraySum(nums []int, left, right int) int { + if left == right || left > right { + return 0 + } + + res := 0 + for index := left; index < right; index++ { + res += nums[index] + } + + return res +} + +// 该方法可以试着画一画 +func pivotIndexBetter(nums []int) int { + sum := 0 + for _, num := range nums { + sum += num + } + + if sum - nums[0] == 0 { + return 0 + } + + curSum := 0 + leftWithoutIndex := 0 + for index := 0; index < len(nums); index++ { + curSum += nums[index] + + if index != 0 { + leftWithoutIndex = curSum - nums[index] + } + + if leftWithoutIndex == sum - curSum { + return index + } + } + + return -1 +} + +func main() { + fmt.Println(pivotIndexBetter([]int{1, 7, 3, 6, 5, 6})) +} \ No newline at end of file diff --git "a/leet_code/753.\346\211\223\345\274\200\350\275\254\347\233\230\351\224\201.go" "b/leet_code/753.\346\211\223\345\274\200\350\275\254\347\233\230\351\224\201.go" new file mode 100644 index 0000000..da49f6c --- /dev/null +++ "b/leet_code/753.\346\211\223\345\274\200\350\275\254\347\233\230\351\224\201.go" @@ -0,0 +1,43 @@ +package leet_code + +func OpenLock(deadends []string, target string) int { + deadMap := make(map[string]byte, 0) + visitedMap := make(map[string]byte, 0) + for _, deadNum := range deadends { + deadMap[deadNum] = '0' + } + if _, ok := deadMap["0000"]; ok { + return -1 + } + + visitSlice := make([]string, 0) + visitSlice = append(visitSlice, "0000") + visitedMap["0000"] = '0' + step := 0 + + for len(visitSlice) != 0 { + size := len(visitSlice) + for i := 0; i < size; i++ { + cur := visitSlice[0] + visitSlice = visitSlice[1:] + if cur == target { + return step + } + + for j := 0; j < 4; j++ { + for k := -1; k <= 1; k += 2 { + tmp := cur[0:j] + string((cur[j]-'0'+uint8(10+k))%10+'0') + cur[j+1:] + if _, ok := deadMap[tmp]; !ok { + if _, ok := visitedMap[tmp]; ok { + visitedMap[tmp] = '0' + visitSlice = append(visitSlice, tmp) + } + } + } + } + } + step++ + } + + return -1 +} diff --git "a/leet_code/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/main.go" "b/leet_code/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/main.go" new file mode 100644 index 0000000..78bf15b --- /dev/null +++ "b/leet_code/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/main.go" @@ -0,0 +1,32 @@ +package main + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func inorderTraversal(root *TreeNode) []int { + res := make([]int, 0) + stack := make([]*TreeNode, 0) + + for root != nil || len(stack) != 0 { + // 先走到最左边 + for root != nil { + stack = append(stack, root) + root = root.Left + } + + top := stack[len(stack) - 1] + if len(stack) != 0 { + stack = stack[:len(stack) - 1] + } + + res = append(res, top.Val) + + root = top.Right + + } + + return res +} diff --git "a/leet_code/\350\256\276\350\256\241\345\276\252\347\216\257\351\230\237\345\210\227.go" "b/leet_code/\350\256\276\350\256\241\345\276\252\347\216\257\351\230\237\345\210\227.go" new file mode 100644 index 0000000..328d6c9 --- /dev/null +++ "b/leet_code/\350\256\276\350\256\241\345\276\252\347\216\257\351\230\237\345\210\227.go" @@ -0,0 +1,94 @@ +package leet_code + +import ( + "fmt" +) + +type MyCircularQueue struct { + head int + tail int + data []int +} + + +func Constructor(k int) MyCircularQueue { + return MyCircularQueue{ + head: 0, + tail: 0, + data: make([]int, k + 1), + } +} + +func (this *MyCircularQueue) EnQueue(value int) bool { + // 判断是否还有可用空间 + if this.IsFull() { + return false + } + + this.tail = (this.tail + 1) % len(this.data) + this.data[this.tail] = value + + return true +} + + +func (this *MyCircularQueue) DeQueue() bool { + if this.IsEmpty() { + return false + } + + this.head = (this.head + 1) % len(this.data) + return true +} + + +func (this *MyCircularQueue) Front() int { + if this.IsEmpty() { + return -1 + } + return this.data[(this.head + 1) % len(this.data)] +} + + +func (this *MyCircularQueue) Rear() int { + if this.IsEmpty() { + return -1 + } + return this.data[this.tail] +} + + +func (this *MyCircularQueue) IsEmpty() bool { + return this.head == this.tail +} + + +func (this *MyCircularQueue) IsFull() bool { + return (this.tail + 1) % len(this.data) == this.head +} + + +/** + * Your MyCircularQueue object will be instantiated and called as such: + * obj := Constructor(k); + * param_1 := obj.EnQueue(value); + * param_2 := obj.DeQueue(); + * param_3 := obj.Front(); + * param_4 := obj.Rear(); + * param_5 := obj.IsEmpty(); + * param_6 := obj.IsFull(); + */ + +func main() { + queue := Constructor(3) + fmt.Println(queue.EnQueue(1)) + fmt.Println(queue.EnQueue(2)) + fmt.Println(queue.EnQueue(3)) + fmt.Println(queue.EnQueue(4)) + + fmt.Println(queue.Rear()) + fmt.Println(queue.IsFull()) + fmt.Println(queue.DeQueue()) + fmt.Println(queue.EnQueue(4)) + fmt.Println(queue.Rear()) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..c710c73 --- /dev/null +++ b/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "time" +) + +func f(arr []int) { + go func() { + for{ + fmt.Println(time.Now().Unix()) + time.Sleep(time.Second) + } + }() + + //time.Sleep(time.Second*100) +} + +func main() { + //t := time.NewTicker(time.Second / 3) + //ch := t.C + // + //go func() { + // for range ch { + // fmt.Println("123----------" + strconv.FormatInt(time.Now().Unix(), 10)) + //}}() + // + //go func() { + // for range ch { + // fmt.Println("456----------" + strconv.FormatInt(time.Now().Unix(), 10)) + // } + //}() + // + //time.Sleep(time.Second * 10) + //t.Stop() + + + fmt.Println(4 % 5) +} diff --git a/practise/testByteBuffer/main.go b/practise/testByteBuffer/main.go new file mode 100644 index 0000000..7aa40a7 --- /dev/null +++ b/practise/testByteBuffer/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +type utest struct { + A float64 `json:"a"` +} +func main() { + test := "{\"a\": 11.11}" + a := new(utest) + + json.Unmarshal([]byte(test), a) + + fmt.Printf("%+v", a) + +} diff --git a/practise/test_01/main.go b/practise/test_01/main.go new file mode 100644 index 0000000..e2ee5df --- /dev/null +++ b/practise/test_01/main.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func main() { + //body, _ := uuid.GenerateUUID() + //fmt.Println(strings.ToUpper(body)) + //fmt.Println(body) + // + //test := map[string]int { + // "1": 1, + // "2": 2, + //} + // + //k, ok := test["1"] + + tmp := "123456" + tmp1 := tmp[1:] + fmt.Printf("%p\n", &tmp) + fmt.Printf("%p", &tmp1) + + +} diff --git a/practise/test_rate/main.go b/practise/test_rate/main.go new file mode 100644 index 0000000..b597cc9 --- /dev/null +++ b/practise/test_rate/main.go @@ -0,0 +1,116 @@ +package main + +import ( + "fmt" + "strconv" + "sync" + "time" +) + +type QpsLimiter struct { + BucketMap *sync.Map + qps int + tickerMap *sync.Map +} + +func NewQpsLimiter(qps int) *QpsLimiter { + if qps <= 0 { + qps = 1 + } + + bucket := new(sync.Map) + ticker := new(sync.Map) + ql := &QpsLimiter{ + BucketMap: bucket, + qps: qps, + tickerMap: ticker, + } + + return ql +} + +func (ql *QpsLimiter) Add(api string) { + if _, ok := ql.BucketMap.Load(api); !ok { + bucket := make(chan int, 1) + ql.BucketMap.Store(api, bucket) + ticker := time.NewTicker(time.Second / time.Duration(ql.qps)) + ql.tickerMap.Store(api, ticker) + } + + go ql.StartTicker(api) +} + +func (ql *QpsLimiter) StartTicker(api string) { + if bucket, ok := ql.BucketMap.Load(api); ok { + if ticker, ok := ql.tickerMap.Load(api); ok { + t := ticker.(*time.Ticker).C + go func() { + for range t { + bucket.(chan int) <- 1 + } + }() + } + } +} + +func (ql *QpsLimiter) GetToken(api string) bool { + if bucket, ok := ql.BucketMap.Load(api); ok { + for range bucket.(chan int) { + return true + } + } + return false +} + +func (ql *QpsLimiter) Stop(api string) { + if ticker, ok := ql.tickerMap.Load(api); ok { + t := ticker.(*time.Ticker) + t.Stop() + } +} + +func main() { + limiter := NewQpsLimiter(3) + + limiter.Add("123") + go func() { + for i := 0; i < 20; i++ { + go func(limiter *QpsLimiter) { + if limiter.GetToken("123") { + fmt.Println("123----------" + strconv.FormatInt(time.Now().Unix(), 10)) + } + }(limiter) + } + }() + + limiter.Add("456") + go func() { + for i := 0; i < 20; i++ { + go func(limiter *QpsLimiter) { + if limiter.GetToken("456") { + fmt.Println("456----------" + strconv.FormatInt(time.Now().Unix(), 10)) + } + }(limiter) + } + }() + + limiter.Add("789") + go func() { + for i := 0; i < 20; i++ { + go func(limiter *QpsLimiter) { + if limiter.GetToken("789") { + fmt.Println("789----------" + strconv.FormatInt(time.Now().Unix(), 10)) + } + }(limiter) + } + }() + time.Sleep(time.Second * 10000) + + limiter.Stop("123") + limiter.Stop("456") + limiter.Stop("789") + + + + time.Sleep(time.Second * 10000) +} diff --git a/practise/test_strings/main.go b/practise/test_strings/main.go new file mode 100644 index 0000000..70ff4f9 --- /dev/null +++ b/practise/test_strings/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +type Rope string + +func testHasPrefix(s, prefix string) { + fmt.Printf("Does '%s' has %s prefix: %v\n", s, prefix, strings.HasPrefix(s, prefix)) +} + +func testLastIndex(s, substr string) { + fmt.Println(strings.LastIndex(s, substr)) +} + +func main() { + group := "a.group_id" + ad := "ad_id" + + fmt.Println(group[strings.Index(group, ".") + 1:]) + fmt.Println(ad[:strings.Index(ad, "_")]) + + var align Rope = "test" + fmt.Println(align) + + testHasPrefix("This is QQMAN", "h") + + testLastIndex("app_id:uid", ":") + + println(strconv.IntSize) +} diff --git a/the_way_to_go/chapter_10/main.go b/the_way_to_go/chapter_10/main.go new file mode 100644 index 0000000..98df6f6 --- /dev/null +++ b/the_way_to_go/chapter_10/main.go @@ -0,0 +1,52 @@ +package main + +import "fmt" + +type A struct {a int} +type B struct {c, b int} + +type C struct {A; B} + +type engine interface { + start() + stop() +} + +type Car struct { + wheelCount int + engine +} + +func (self *Car) numberOfWheels() int { + return self.wheelCount +} + +func (self *Car) start() { + fmt.Println("start engine! Buzz~ Buzz~ Buzz~") +} + +func (self *Car) stop() { + fmt.Println("Ops~ The engine is stopped.") +} + +type Mercedes struct { + Car +} + +func (m *Mercedes) sayHiToMerkel() { + fmt.Println("Mercedes Bens ! I will get it.") +} + +func main() { + c := Car{ + wheelCount: 4, + } + + fmt.Println(c.numberOfWheels()) + + c.start() + c.stop() + + m := Mercedes{} + m.sayHiToMerkel() +} diff --git a/the_way_to_go/chapter_11/chapter_11.go b/the_way_to_go/chapter_11/chapter_11.go new file mode 100644 index 0000000..34e2c53 --- /dev/null +++ b/the_way_to_go/chapter_11/chapter_11.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" +) + +type Shaper interface { + Area() float32 +} + +type Square struct { + side float32 +} + +func (sq *Square) Area() float32 { + return sq.side * sq.side +} + +type Rectangle struct { + length, width float32 +} + +func (r Rectangle) Area() float32 { + return r.length * r.width +} + +type Triangle struct { + length float32 + high float32 +} + +func (t Triangle) Area() float32 { + return (t.length * t.high) / 2 +} + +func main() { + var s Shaper = Triangle{ + length: 3.1, + high: 2.3, + } + + fmt.Println(s.Area()) +} diff --git a/the_way_to_go/chapter_12/main.go b/the_way_to_go/chapter_12/main.go new file mode 100644 index 0000000..9458aa4 --- /dev/null +++ b/the_way_to_go/chapter_12/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "bufio" + "fmt" + "io/ioutil" + "os" +) + +func main() { + fileHandler, err := os.Open("fileName.txt") + if err != nil { + fmt.Println("open file err: %v", err) + return + } + + defer fileHandler.Close() + + reader := bufio.NewReader(fileHandler) + ioutil.ReadAll(reader) +} diff --git a/the_way_to_go/chapter_13/main.go b/the_way_to_go/chapter_13/main.go new file mode 100644 index 0000000..b945974 --- /dev/null +++ b/the_way_to_go/chapter_13/main.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +func main() { + f() + fmt.Println("Returned normally from f.") +} + +func f() { + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered in f", r) + } + }() + fmt.Println("Calling g.") + g(0) + fmt.Println("Returned normally from g.") +} + +func g(i int) { + if i > 3 { + fmt.Println("Panicking!") + panic(fmt.Sprintf("%v", i)) + } + defer fmt.Println("Defer in g", i) + fmt.Println("Printing in g", i) + g(i + 1) +} diff --git a/the_way_to_go/chapter_14/main.go b/the_way_to_go/chapter_14/main.go new file mode 100644 index 0000000..cb0ae94 --- /dev/null +++ b/the_way_to_go/chapter_14/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "sync" +) + +func f1(in chan int) { + fmt.Println(<-in) +} + +func producer(c chan interface{}, arr []int, wg *sync.WaitGroup) { + defer (*wg).Done() + + for _, num := range arr { + c <- num + } + + c <- "done" +} + +func consumer(c chan interface{}, wg *sync.WaitGroup) { + defer (*wg).Done() +forloop: + for { + select { + case data := <- c: + switch data.(type) { + case string: + break forloop + case int: + fmt.Println(data) + } + } + } +} + +func main() { + //out := make(chan int) + //// 由于创建的 chan 是无缓冲的,所以会一直阻塞在这一步。 + //out <- 2 + //go f1(out) + + c := make(chan interface{}) + wg := new(sync.WaitGroup) + wg.Add(2) + go producer(c, []int{10, 20, 30, 40, 50, 60, 70, 80, 90}, wg) + go consumer(c, wg) + + wg.Wait() + fmt.Println("done") +} \ No newline at end of file diff --git a/the_way_to_go/chapter_5/string_conversion2.go b/the_way_to_go/chapter_5/string_conversion2.go new file mode 100644 index 0000000..65e0634 --- /dev/null +++ b/the_way_to_go/chapter_5/string_conversion2.go @@ -0,0 +1,25 @@ +package main +// +//import ( +// "fmt" +// "strconv" +//) +// +//func main() { +// var orig string = "ABC" +// // var an int +// var newS string +// // var err error +// +// fmt.Printf("The size of ints is: %d\n", strconv.IntSize) +// // anInt, err = strconv.Atoi(origStr) +// an, err := strconv.Atoi(orig) +// if err != nil { +// fmt.Printf("orig %s is not an integer - exiting with error\n", orig) +// return +// } +// fmt.Printf("The integer is %d\n", an) +// an = an + 5 +// newS = strconv.Itoa(an) +// fmt.Printf("The new string is: %s\n", newS) +//} diff --git a/the_way_to_go/chapter_5/switch.go b/the_way_to_go/chapter_5/switch.go new file mode 100644 index 0000000..13bad85 --- /dev/null +++ b/the_way_to_go/chapter_5/switch.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" +) + +func getSeason(month int) { + switch month { + case 3, 4, 5: + fmt.Println("It's Spring!") + case 6, 7, 8: + fmt.Println("It's Summer!") + case 9, 10, 11: + fmt.Println("It's Autumn!") + case 12, 1, 2: + fmt.Println("It's Winter!") + } +} + +func main() { + month := 0 + _, _ = fmt.Scan(&month) + + getSeason(month) + +} \ No newline at end of file diff --git a/the_way_to_go/chapter_6/func.go b/the_way_to_go/chapter_6/func.go new file mode 100644 index 0000000..5102265 --- /dev/null +++ b/the_way_to_go/chapter_6/func.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func printOneToTen(n int) { + if n <= 0 { + return + } + + fmt.Println(n) + printOneToTen(n - 1) +} + +func main() { + printOneToTen(10) +} diff --git a/the_way_to_go/chapter_7/practice_7_9.go b/the_way_to_go/chapter_7/practice_7_9.go new file mode 100644 index 0000000..7ea8924 --- /dev/null +++ b/the_way_to_go/chapter_7/practice_7_9.go @@ -0,0 +1,80 @@ +package main + +import "fmt" + +func cutSliceByIndex(str string, index int) []string { + res := make([]string, 0) + + res = append(res, str[:index]) + res = append(res, str[index + 1: ]) + return res +} + +// 7.16 bubble sort +func BubbleSort(arr []int) []int { + for i := 0; i < len(arr); i++ { + for j := i; j < len(arr); j++ { + if arr[i] > arr[j] { + tmp := arr[i] + arr[i] = arr[j] + arr[j] = tmp + } + } + } + + return arr +} + +func mulTen(arr []int) []int { + for index := range arr { + arr[index] = arr[index] * 10 + } + + return arr +} + + + +func main() { + //s := []int{1, 2, 3} + // + //factor := 19 + // + //if len(s) + factor > cap(s) { + // newSlice := make([]int, len(s) * factor) + // copy(newSlice, s) + // s = newSlice + //} + // + //fmt.Println(s) + + //tmp := "123456789101111" + //a := 0 + //s := "123" + //s1 := s[1:] + //var tmp2 string = tmp[0:3] + //tmpByte := []byte(tmp) + // + //fmt.Println((*reflect.StringHeader)(unsafe.Pointer(&tmp)).Data) + //fmt.Println((*reflect.StringHeader)(unsafe.Pointer(&tmp2)).Data) + //fmt.Println((*reflect.SliceHeader)(unsafe.Pointer(&tmpByte)).Data) + // + //fmt.Printf("tmp %p\n", &tmp) + //fmt.Printf("tmp2 %p\n", &tmp2) + //fmt.Printf("%p\n", &tmpByte) + //fmt.Printf("%p\n", &a) + // + //tmpByte[0] = '9' + // + //fmt.Println(tmp) + //fmt.Println(tmp2) + //fmt.Println(s1) + // + //fmt.Println(string(tmpByte)) + + // 7.16 + unsortArr := []int{4, 2, 7, 4, 3, 2, 1, 6} + sortArr := BubbleSort(unsortArr) + fmt.Println(sortArr) + +} diff --git a/the_way_to_go/chapter_9/main.go b/the_way_to_go/chapter_9/main.go new file mode 100644 index 0000000..2dc2b89 --- /dev/null +++ b/the_way_to_go/chapter_9/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "container/list" + "fmt" + "unsafe" +) + +func main() { + // go 双向链表的基本运用 + l := list.New() + l.PushBack(1) + l.PushBack(2) + l.PushBack(3) + + for i := l.Front(); i != nil; i = i.Next() { + fmt.Println(i.Value) + } + + + // 测试电脑一个 整型 占多少个字节 + var a int = 1 + fmt.Println(unsafe.Sizeof(a)) +}