File tree Expand file tree Collapse file tree 18 files changed +478
-63
lines changed
0003.Longest Substring Without Repeating Characters
0008.String to Integer (atoi)
0014.Longest Common Prefix
0200-0299/0238.Product of Array Except Self
0345.Reverse Vowels of a String Expand file tree Collapse file tree 18 files changed +478
-63
lines changed Original file line number Diff line number Diff line change 1414
1515<pre >
1616<strong >输入: </strong >s = "abcabcbb"
17- <strong >输出: </strong >3
17+ <strong >输出: </strong >3
1818<strong >解释:</strong > 因为无重复字符的最长子串是 <code >"abc",所以其</code >长度为 3。
1919</pre >
2020
@@ -150,6 +150,35 @@ class Solution {
150150}
151151```
152152
153+ ### ** Go**
154+
155+ ``` go
156+ func lengthOfLongestSubstring (s string ) int {
157+ window := make (map [byte ]int )
158+ n := len (s)
159+ ans := 0
160+ left , right := 0 , 0
161+ for right < n {
162+ b := s[right]
163+ right++
164+ window[b]++
165+ for window[b] > 1 {
166+ window[s[left]]--
167+ left++
168+ }
169+ ans = max (ans, right-left)
170+ }
171+ return ans
172+ }
173+
174+ func max (x , y int ) int {
175+ if x > y {
176+ return x
177+ }
178+ return y
179+ }
180+ ```
181+
153182### ** ...**
154183
155184```
Original file line number Diff line number Diff line change @@ -132,6 +132,35 @@ class Solution {
132132}
133133```
134134
135+ ### ** Go**
136+
137+ ``` go
138+ func lengthOfLongestSubstring (s string ) int {
139+ window := make (map [byte ]int )
140+ n := len (s)
141+ ans := 0
142+ left , right := 0 , 0
143+ for right < n {
144+ b := s[right]
145+ right++
146+ window[b]++
147+ for window[b] > 1 {
148+ window[s[left]]--
149+ left++
150+ }
151+ ans = max (ans, right-left)
152+ }
153+ return ans
154+ }
155+
156+ func max (x , y int ) int {
157+ if x > y {
158+ return x
159+ }
160+ return y
161+ }
162+ ```
163+
135164### ** ...**
136165
137166```
Original file line number Diff line number Diff line change 1- /*
2- * Report by leetcode.com
3- * Runtime: 8 ms, Memory Usage: 3.2 MB
4- */
51func lengthOfLongestSubstring (s string ) int {
6- mathMax := func (a , b int ) int {
7- if a > b {
8- return a
2+ window := make (map [byte ]int )
3+ n := len (s )
4+ ans := 0
5+ left , right := 0 , 0
6+ for right < n {
7+ b := s [right ]
8+ right ++
9+ window [b ]++
10+ for window [b ] > 1 {
11+ window [s [left ]]--
12+ left ++
913 }
10- return b
14+ ans = max ( ans , right - left )
1115 }
12- cache := map [rune ]int {}
13- var max , position int
14- for i , r := range s {
15- if num , ok := cache [r ]; ok {
16- position = mathMax (position , num + 1 )
17- }
18- cache [r ] = i
19- max = mathMax (max , i - position + 1 )
16+ return ans
17+ }
18+
19+ func max (x , y int ) int {
20+ if x > y {
21+ return x
2022 }
21- return max
22- }
23+ return y
24+ }
Original file line number Diff line number Diff line change @@ -191,6 +191,46 @@ class Solution {
191191}
192192```
193193
194+ ### ** Go**
195+
196+ ``` go
197+ func myAtoi (s string ) int {
198+ i , n := 0 , len (s)
199+ num := 0
200+
201+ for i < n && s[i] == ' ' {
202+ i++
203+ }
204+ if i == n {
205+ return 0
206+ }
207+
208+ sign := 1
209+ if s[i] == ' -' {
210+ sign = -1
211+ i++
212+ } else if s[i] == ' +' {
213+ i++
214+ }
215+
216+ for i < n && s[i] >= ' 0' && s[i] <= ' 9' {
217+ num = num*10 + int (s[i]-' 0' )
218+ i++
219+ if num > math.MaxInt32 {
220+ break
221+ }
222+ }
223+
224+ if num > math.MaxInt32 {
225+ if sign == -1 {
226+ return math.MinInt32
227+ }
228+ return math.MaxInt32
229+ }
230+ return sign * num
231+ }
232+ ```
233+
194234### ** ...**
195235
196236```
Original file line number Diff line number Diff line change @@ -180,6 +180,46 @@ class Solution {
180180}
181181```
182182
183+ ### ** Go**
184+
185+ ``` go
186+ func myAtoi (s string ) int {
187+ i , n := 0 , len (s)
188+ num := 0
189+
190+ for i < n && s[i] == ' ' {
191+ i++
192+ }
193+ if i == n {
194+ return 0
195+ }
196+
197+ sign := 1
198+ if s[i] == ' -' {
199+ sign = -1
200+ i++
201+ } else if s[i] == ' +' {
202+ i++
203+ }
204+
205+ for i < n && s[i] >= ' 0' && s[i] <= ' 9' {
206+ num = num*10 + int (s[i]-' 0' )
207+ i++
208+ if num > math.MaxInt32 {
209+ break
210+ }
211+ }
212+
213+ if num > math.MaxInt32 {
214+ if sign == -1 {
215+ return math.MinInt32
216+ }
217+ return math.MaxInt32
218+ }
219+ return sign * num
220+ }
221+ ```
222+
183223### ** ...**
184224
185225```
Original file line number Diff line number Diff line change 1- func myAtoi (str string ) int {
2- cer := 0
3- result := 0
4- tmpResult := 0
5- flag := false
6- for _ , n := range str {
7- if ! flag && n == ' ' {
8- continue
9- }
10- flag = true
11- if cer == 0 {
12- if n >= '0' && n <= '9' {
13- cer = 1
14- } else if n == '+' {
15- cer = 1
16- continue
17- } else if cer == 0 && (n == '-' ) {
18- cer = - 1
19- continue
20- }
21- }
1+ func myAtoi (s string ) int {
2+ i , n := 0 , len (s )
3+ num := 0
4+
5+ for i < n && s [i ] == ' ' {
6+ i ++
7+ }
8+ if i == n {
9+ return 0
10+ }
2211
23- if n >= '0' && n <= '9' {
24- tmpResult = tmpResult * 10 + ((int )(n ) - 48 )
25- result = cer * tmpResult
26- } else {
12+ sign := 1
13+ if s [i ] == '-' {
14+ sign = - 1
15+ i ++
16+ } else if s [i ] == '+' {
17+ i ++
18+ }
19+
20+ for i < n && s [i ] >= '0' && s [i ] <= '9' {
21+ num = num * 10 + int (s [i ]- '0' )
22+ i ++
23+ if num > math .MaxInt32 {
2724 break
2825 }
29- if result < math .MinInt32 {
30- return math .MinInt32
31- }
32- if result > math .MaxInt32 {
33- return math .MaxInt32
34- }
3526 }
36- return result
37- }
27+
28+ if num > math .MaxInt32 {
29+ if sign == - 1 {
30+ return math .MinInt32
31+ }
32+ return math .MaxInt32
33+ }
34+ return sign * num
35+ }
Original file line number Diff line number Diff line change @@ -101,6 +101,31 @@ public:
101101};
102102```
103103
104+ ### **Go**
105+
106+ ```go
107+ func longestCommonPrefix(strs []string) string {
108+ if len(strs) == 0 {
109+ return ""
110+ }
111+
112+ var b strings.Builder
113+ m, n := len(strs[0]), len(strs)
114+
115+ LOOP:
116+ for i := 0; i < m; i++ {
117+ for j := 1; j < n; j++ {
118+ if i >= len(strs[j]) || strs[0][i] != strs[j][i] {
119+ break LOOP
120+ }
121+ }
122+ b.WriteByte(strs[0][i])
123+ }
124+
125+ return b.String()
126+ }
127+ ```
128+
104129### ** ...**
105130
106131```
Original file line number Diff line number Diff line change @@ -92,6 +92,31 @@ public:
9292};
9393```
9494
95+ ### **Go**
96+
97+ ```go
98+ func longestCommonPrefix(strs []string) string {
99+ if len(strs) == 0 {
100+ return ""
101+ }
102+
103+ var b strings.Builder
104+ m, n := len(strs[0]), len(strs)
105+
106+ LOOP:
107+ for i := 0; i < m; i++ {
108+ for j := 1; j < n; j++ {
109+ if i >= len(strs[j]) || strs[0][i] != strs[j][i] {
110+ break LOOP
111+ }
112+ }
113+ b.WriteByte(strs[0][i])
114+ }
115+
116+ return b.String()
117+ }
118+ ```
119+
95120### ** ...**
96121
97122```
Original file line number Diff line number Diff line change 11func longestCommonPrefix (strs []string ) string {
2- result := ""
3- for i , v := range strs {
4- if i == 0 {
5- result = v
6- }
7- index := 0
8- for index < len (result ) && index < len (v ) && result [index ] == v [index ] {
9- index ++
2+ if len (strs ) == 0 {
3+ return ""
4+ }
5+
6+ var b strings.Builder
7+ m , n := len (strs [0 ]), len (strs )
8+
9+ LOOP:
10+ for i := 0 ; i < m ; i ++ {
11+ for j := 1 ; j < n ; j ++ {
12+ if i >= len (strs [j ]) || strs [0 ][i ] != strs [j ][i ] {
13+ break LOOP
14+ }
1015 }
11- result = result [: index ]
16+ b . WriteByte ( strs [ 0 ][ i ])
1217 }
13- return result
14- }
18+
19+ return b .String ()
20+ }
You can’t perform that action at this time.
0 commit comments