File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed
solution/0000-0099/0003.Longest Substring Without Repeating Characters Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change 26
26
27
27
<pre >
28
28
<strong >输入: </strong >s = "abcabcbb"
29
- <strong >输出: </strong >3
29
+ <strong >输出: </strong >3
30
30
<strong >解释:</strong > 因为无重复字符的最长子串是 <code >"abc"</code >,所以其长度为 3。
31
31
</pre >
32
32
@@ -309,6 +309,27 @@ proc lengthOfLongestSubstring(s: string): int =
309
309
result = res # result has the default return value
310
310
```
311
311
312
+ #### Kotlin
313
+
314
+ ``` kotlin
315
+ class Solution {
316
+ fun lengthOfLongestSubstring (s : String ): Int {
317
+ var char_set = BooleanArray (128 )
318
+ var left = 0
319
+ var ans = 0
320
+ s.forEachIndexed { right, c ->
321
+ while (char_set[c.code]) {
322
+ char_set[s[left].code] = false
323
+ left++
324
+ }
325
+ char_set[c.code] = true
326
+ ans = Math .max(ans, right - left + 1 )
327
+ }
328
+ return ans
329
+ }
330
+ }
331
+ ```
332
+
312
333
<!-- tabs: end -->
313
334
314
335
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -307,6 +307,27 @@ proc lengthOfLongestSubstring(s: string): int =
307
307
result = res # result has the default return value
308
308
```
309
309
310
+ #### Kotlin
311
+
312
+ ``` kotlin
313
+ class Solution {
314
+ fun lengthOfLongestSubstring (s : String ): Int {
315
+ var char_set = BooleanArray (128 )
316
+ var left = 0
317
+ var ans = 0
318
+ s.forEachIndexed { right, c ->
319
+ while (char_set[c.code]) {
320
+ char_set[s[left].code] = false
321
+ left++
322
+ }
323
+ char_set[c.code] = true
324
+ ans = Math .max(ans, right - left + 1 )
325
+ }
326
+ return ans
327
+ }
328
+ }
329
+ ```
330
+
310
331
<!-- tabs: end -->
311
332
312
333
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun lengthOfLongestSubstring (s : String ): Int {
3
+ var char_set = BooleanArray (128 )
4
+ var left = 0
5
+ var ans = 0
6
+ s.forEachIndexed { right, c ->
7
+ while (char_set[c.code]) {
8
+ char_set[s[left].code] = false
9
+ left++
10
+ }
11
+ char_set[c.code] = true
12
+ ans = Math .max(ans, right - left + 1 )
13
+ }
14
+ return ans
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments