0909
This commit is contained in:
parent
a98623af75
commit
a18ee2ce33
2 changed files with 33 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
fun main() {
|
||||
val t = Test560()
|
||||
val t = Test14()
|
||||
t.test()
|
||||
}
|
32
src/Test14.kt
Normal file
32
src/Test14.kt
Normal 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")))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue