This commit is contained in:
kagura 2024-09-09 12:15:14 +08:00
parent a98623af75
commit a18ee2ce33
2 changed files with 33 additions and 1 deletions

View file

@ -1,4 +1,4 @@
fun main() {
val t = Test560()
val t = Test14()
t.test()
}

32
src/Test14.kt Normal file
View file

@ -0,0 +1,32 @@
class Test14 {
class Solution {
fun longestCommonPrefix(strs: Array<String>): String {
if (strs.size == 1){
return strs[0]
}
var sb = strs[0]
for (s in strs){
if (sb.isEmpty()){
return ""
}
if (sb.lastIndex > s.lastIndex){
sb = sb.substring(s.indices)
}
for (j in s.indices){
if (j > sb.lastIndex){
break
}
if (sb[j]!=s[j]){
sb = sb.substring(0..<j)
break
}
}
}
return sb
}
}
fun test(){
val s = Solution()
println(s.longestCommonPrefix(arrayOf("ab","a")))
}
}