Compare commits

...

3 commits

Author SHA1 Message Date
bd07ab1e88 Merge 2024-09-09 12:17:41 +08:00
a18ee2ce33 0909 2024-09-09 12:15:14 +08:00
a98623af75 0809 2024-08-10 10:30:28 +08:00
2 changed files with 55 additions and 0 deletions

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")))
}
}

23
src/Test560.kt Normal file
View 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))
}
}