Skip to content

Commit a5c969d

Browse files
committed
feat: add kotlin solution to lc problem: No.0003
1 parent b97c6d3 commit a5c969d

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tags:
2626

2727
<pre>
2828
<strong>输入: </strong>s = "abcabcbb"
29-
<strong>输出: </strong>3
29+
<strong>输出: </strong>3
3030
<strong>解释:</strong> 因为无重复字符的最长子串是 <code>"abc"</code>,所以其长度为 3。
3131
</pre>
3232

@@ -309,6 +309,27 @@ proc lengthOfLongestSubstring(s: string): int =
309309
result = res # result has the default return value
310310
```
311311

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+
312333
<!-- tabs:end -->
313334

314335
<!-- solution:end -->

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,27 @@ proc lengthOfLongestSubstring(s: string): int =
307307
result = res # result has the default return value
308308
```
309309

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+
310331
<!-- tabs:end -->
311332

312333
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)