![JAR search and dependency download from the Maven repository](/logo.png)
g0701_0800.s0722_remove_comments.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0722_remove_comments
// #Medium #Array #String #2023_02_28_Time_164_ms_(100.00%)_Space_35.4_MB_(100.00%)
class Solution {
fun removeComments(source: Array): List {
val result: MutableList = ArrayList()
val sb = StringBuilder()
var multiComment = false
for (line in source) {
val n = line.length
var index = 0
while (index < n) {
val ch = line[index]
if (!multiComment && ch == '/') {
index++
if (index >= n) {
sb.append(ch)
continue
}
if (line[index] == '/') {
break
} else if (line[index] == '*') {
multiComment = true
} else {
sb.append(ch).append(line[index])
}
} else if (multiComment && ch == '*') {
index++
if (index >= n) {
continue
}
if (line[index] == '/') {
multiComment = false
} else {
index--
}
} else if (!multiComment) {
sb.append(ch)
}
index++
}
if (sb.isNotEmpty() && !multiComment) {
result.add(sb.toString())
sb.setLength(0)
}
}
return result
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy