mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
添加0034 Kotlin版,0203 Kotlin版,0209 Koltin版, 0977 Kotlin版
This commit is contained in:
@@ -417,6 +417,38 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
滑动窗口
|
||||
```kotlin
|
||||
class Solution {
|
||||
fun minSubArrayLen(target: Int, nums: IntArray): Int {
|
||||
// 左边界 和 右边界
|
||||
var left: Int = 0
|
||||
var right: Int = 0
|
||||
// sum 用来记录和
|
||||
var sum: Int = 0
|
||||
// result记录一个固定值,便于判断是否存在的这样的数组
|
||||
var result: Int = Int.MAX_VALUE
|
||||
// subLenth记录长度
|
||||
var subLength = Int.MAX_VALUE
|
||||
|
||||
|
||||
while (right < nums.size) {
|
||||
// 从数组首元素开始逐次求和
|
||||
sum += nums[right++]
|
||||
// 判断
|
||||
while (sum >= target) {
|
||||
var temp = right - left
|
||||
// 每次和上一次比较求出最小数组长度
|
||||
subLength = if (subLength > temp) temp else subLength
|
||||
// sum减少,左边界右移
|
||||
sum -= nums[left++]
|
||||
}
|
||||
}
|
||||
// 如果subLength为初始值,则说明长度为0,否则返回subLength
|
||||
return if(subLength == result) 0 else subLength
|
||||
}
|
||||
}
|
||||
```
|
||||
Scala:
|
||||
|
||||
滑动窗口:
|
||||
|
||||
Reference in New Issue
Block a user