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 83726ac

Browse files
Merge pull request youngyangyang04#1328 from wzqwtt/patch09
添加(0015.三数之和、0018.四数之和)Scala版本
2 parents b91d3c2 + 68eed4a commit 83726ac

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

‎problems/0015.三数之和.md‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,49 @@ public class Solution
616616
}
617617
}
618618
```
619-
619+
Scala:
620+
```scala
621+
object Solution {
622+
// 导包
623+
import scala.collection.mutable.ListBuffer
624+
import scala.util.control.Breaks.{break, breakable}
625+
626+
def threeSum(nums: Array[Int]): List[List[Int]] = {
627+
// 定义结果集,最后需要转换为List
628+
val res = ListBuffer[List[Int]]()
629+
val nums_tmp = nums.sorted // 对nums进行排序
630+
for (i <- nums_tmp.indices) {
631+
// 如果要排的第一个数字大于0,直接返回结果
632+
if (nums_tmp(i) > 0) {
633+
return res.toList
634+
}
635+
// 如果i大于0并且和前一个数字重复,则跳过本次循环,相当于continue
636+
breakable {
637+
if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
638+
break
639+
} else {
640+
var left = i + 1
641+
var right = nums_tmp.length - 1
642+
while (left < right) {
643+
var sum = nums_tmp(i) + nums_tmp(left) + nums_tmp(right) // 求三数之和
644+
if (sum < 0) left += 1
645+
else if (sum > 0) right -= 1
646+
else {
647+
res += List(nums_tmp(i), nums_tmp(left), nums_tmp(right)) // 如果等于0 添加进结果集
648+
// 为了避免重复,对left和right进行移动
649+
while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
650+
while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
651+
left += 1
652+
right -= 1
653+
}
654+
}
655+
}
656+
}
657+
}
658+
// 最终返回需要转换为List,return关键字可以省略
659+
res.toList
660+
}
661+
}
662+
```
620663
-----------------------
621664
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0018.四数之和.md‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,49 @@ public class Solution
522522
}
523523
}
524524
```
525-
525+
Scala:
526+
```scala
527+
object Solution {
528+
// 导包
529+
import scala.collection.mutable.ListBuffer
530+
import scala.util.control.Breaks.{break, breakable}
531+
def fourSum(nums: Array[Int], target: Int): List[List[Int]] = {
532+
val res = ListBuffer[List[Int]]()
533+
val nums_tmp = nums.sorted // 先排序
534+
for (i <- nums_tmp.indices) {
535+
breakable {
536+
if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
537+
break // 如果该值和上次的值相同,跳过本次循环,相当于continue
538+
} else {
539+
for (j <- i + 1 until nums_tmp.length) {
540+
breakable {
541+
if (j > i + 1 && nums_tmp(j) == nums_tmp(j - 1)) {
542+
break // 同上
543+
} else {
544+
// 双指针
545+
var (left, right) = (j + 1, nums_tmp.length - 1)
546+
while (left < right) {
547+
var sum = nums_tmp(i) + nums_tmp(j) + nums_tmp(left) + nums_tmp(right)
548+
if (sum == target) {
549+
// 满足要求,直接加入到集合里面去
550+
res += List(nums_tmp(i), nums_tmp(j), nums_tmp(left), nums_tmp(right))
551+
while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
552+
while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
553+
left += 1
554+
right -= 1
555+
} else if (sum < target) left += 1
556+
else right -= 1
557+
}
558+
}
559+
}
560+
}
561+
}
562+
}
563+
}
564+
// 最终返回的res要转换为List,return关键字可以省略
565+
res.toList
566+
}
567+
}
568+
```
526569
-----------------------
527570
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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