Compare commits
3 commits
070b481895
...
bd07ab1e88
Author | SHA1 | Date | |
---|---|---|---|
bd07ab1e88 | |||
a18ee2ce33 | |||
a98623af75 |
2 changed files with 55 additions and 0 deletions
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")))
|
||||
}
|
||||
}
|
23
src/Test560.kt
Normal file
23
src/Test560.kt
Normal file
|
@ -0,0 +1,23 @@
|
|||
class Test560 {
|
||||
class Solution {
|
||||
fun subarraySum(nums: IntArray, k: Int): Int {
|
||||
var result = 0
|
||||
for (i in nums.indices) {
|
||||
var sum = 0
|
||||
for (j in i..nums.lastIndex){
|
||||
sum += nums[j]
|
||||
if (sum == k){
|
||||
result ++
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun test(): Unit {
|
||||
// println(Solution().subarraySum(intArrayOf(1,1,1),2))
|
||||
// println(Solution().subarraySum(intArrayOf(1,2,3),3))
|
||||
println(Solution().subarraySum(intArrayOf(1,-1,0),0))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue