Skip to content

Commit e3fc9ed

Browse files
committed
萤火虫-N3
1 parent b81a0ea commit e3fc9ed

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'java'
3+
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
34
}
45

56
group 'group.redspider'
@@ -13,4 +14,15 @@ repositories {
1314

1415
dependencies {
1516
testCompile group: 'junit', name: 'junit', version: '4.12'
17+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
18+
}
19+
compileKotlin {
20+
kotlinOptions {
21+
jvmTarget = "1.8"
22+
}
23+
}
24+
compileTestKotlin {
25+
kotlinOptions {
26+
jvmTarget = "1.8"
27+
}
1628
}

src/main/java/N1_100/N3.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package N1_100
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun lengthOfLongestSubstring(s: String): Int {
9+
var maxLength = 0
10+
val n = s.length
11+
val map = HashMap<Char, Int>()
12+
var i = 0
13+
for (j in 0 until n) {
14+
if (s[j] in map) {
15+
i = max(map.get(s[j])!!, i)
16+
}
17+
maxLength = max(maxLength, j - i + 1)
18+
map.put(s[j], j + 1)
19+
}
20+
return maxLength
21+
}
22+
23+
@Test
24+
fun test() {
25+
assertEquals(lengthOfLongestSubstring("abcabcbb"), 3)
26+
}
27+
}

0 commit comments

Comments
 (0)