Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f115be0

Browse files
Merge pull request youngyangyang04#1305 from ZongqinWang/patch01
添加(0977.有序数组的平方 和 0209.长度最小的子数组) Scala版本
2 parents 7684a1c + 871d96a commit f115be0

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

‎problems/0209.长度最小的子数组.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,54 @@ class Solution {
400400
}
401401
}
402402
```
403+
Scala:
404+
405+
滑动窗口:
406+
```scala
407+
object Solution {
408+
def minSubArrayLen(target: Int, nums: Array[Int]): Int = {
409+
var result = Int.MaxValue // 返回结果,默认最大值
410+
var left = 0 // 慢指针,当sum>=target,向右移动
411+
var sum = 0 // 窗口值的总和
412+
for (right <- 0 until nums.length) {
413+
sum += nums(right)
414+
while (sum >= target) {
415+
result = math.min(result, right - left + 1) // 产生新结果
416+
sum -= nums(left) // 左指针移动,窗口总和减去左指针的值
417+
left += 1 // 左指针向右移动
418+
}
419+
}
420+
// 相当于三元运算符,return关键字可以省略
421+
if (result == Int.MaxValue) 0 else result
422+
}
423+
}
424+
```
425+
426+
暴力解法:
427+
```scala
428+
object Solution {
429+
def minSubArrayLen(target: Int, nums: Array[Int]): Int = {
430+
import scala.util.control.Breaks
431+
var res = Int.MaxValue
432+
var subLength = 0
433+
for (i <- 0 until nums.length) {
434+
var sum = 0
435+
Breaks.breakable(
436+
for (j <- i until nums.length) {
437+
sum += nums(j)
438+
if (sum >= target) {
439+
subLength = j - i + 1
440+
res = math.min(subLength, res)
441+
Breaks.break()
442+
}
443+
}
444+
)
445+
}
446+
// 相当于三元运算符
447+
if (res == Int.MaxValue) 0 else res
448+
}
449+
}
450+
```
403451

404452
-----------------------
405453
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0977.有序数组的平方.md‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,41 @@ class Solution {
358358
}
359359
}
360360
```
361-
361+
Scala:
362+
363+
双指针:
364+
```scala
365+
object Solution {
366+
def sortedSquares(nums: Array[Int]): Array[Int] = {
367+
val res: Array[Int] = new Array[Int](nums.length)
368+
var top = nums.length - 1
369+
var i = 0
370+
var j = nums.length - 1
371+
while (i <= j) {
372+
if (nums(i) * nums(i) <= nums(j) * nums(j)) {
373+
// 当左侧平方小于等于右侧,res数组顶部放右侧的平方,并且top下移,j左移
374+
res(top) = nums(j) * nums(j)
375+
top -= 1
376+
j -= 1
377+
} else {
378+
// 当左侧平方大于右侧,res数组顶部放左侧的平方,并且top下移,i右移
379+
res(top) = nums(i) * nums(i)
380+
top -= 1
381+
i += 1
382+
}
383+
}
384+
res
385+
}
386+
}
387+
```
388+
骚操作(暴力思路):
389+
```scala
390+
object Solution {
391+
def sortedSquares(nums: Array[Int]): Array[Int] = {
392+
nums.map(x=>{x*x}).sortWith(_ < _)
393+
}
394+
}
395+
```
362396

363397

364398
-----------------------

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /