diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 3d850c08..e6cc668b 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -373,6 +373,60 @@ func subsets(_ nums: [Int]) -> [[Int]] { } ``` +## Scala + +思路一: 使用本题解思路 + +```scala +object Solution { + import scala.collection.mutable + def subsets(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(startIndex: Int): Unit = { + result.append(path.toList) // 存放结果 + if (startIndex >= nums.size) { + return + } + for (i <- startIndex until nums.size) { + path.append(nums(i)) // 添加元素 + backtracking(i + 1) + path.remove(path.size - 1) // 删除 + } + } + + backtracking(0) + result.toList + } +} +``` + +思路二: 将原问题转换为二叉树,针对每一个元素都有**选或不选**两种选择,直到遍历到最后,所有的叶子节点即为本题的答案: + +```scala +object Solution { + import scala.collection.mutable + def subsets(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + + def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = { + if (startIndex == nums.length) { + result.append(path.toList) + return + } + path.append(nums(startIndex)) + backtracking(path, startIndex + 1) // 选择元素 + path.remove(path.size - 1) + backtracking(path, startIndex + 1) // 不选择元素 + } + + backtracking(mutable.ListBuffer[Int](), 0) + result.toList + } +} +``` + -----------------------
diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 909c5692..e85ec66d 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -434,6 +434,63 @@ func subsetsWithDup(_ nums: [Int]) -> [[Int]] { } ``` +### Scala + +不使用userd数组: + +```scala +object Solution { + import scala.collection.mutable + def subsetsWithDup(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + var num = nums.sorted // 排序 + + def backtracking(startIndex: Int): Unit = { + result.append(path.toList) + if (startIndex >= num.size){ + return + } + for (i <- startIndex until num.size) { + // 同一树层重复的元素不进入回溯 + if (!(i > startIndex && num(i) == num(i - 1))) { + path.append(num(i)) + backtracking(i + 1) + path.remove(path.size - 1) + } + } + } + + backtracking(0) + result.toList + } +} +``` + +使用Set去重: +```scala +object Solution { + import scala.collection.mutable + def subsetsWithDup(nums: Array[Int]): List[List[Int]] = { + var result = mutable.Set[List[Int]]() + var num = nums.sorted + def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = { + if (startIndex == num.length) { + result.add(path.toList) + return + } + path.append(num(startIndex)) + backtracking(path, startIndex + 1) // 选择 + path.remove(path.size - 1) + backtracking(path, startIndex + 1) // 不选择 + } + + backtracking(mutable.ListBuffer[Int](), 0) + + result.toList + } +} +``` -----------------------
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index a2717d09..5ef23944 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2629,21 +2629,21 @@ JavaScript: var minDepth = function(root) { if (root === null) return 0; let queue = [root]; - let deepth = 0; + let depth = 0; while (queue.length) { let n = queue.length; - deepth++; + depth++; for (let i=0; i = new Map(); + const helperMap2: Map = new Map(); + for (let i = 0, length = s.length; i < length; i++) { + let temp1: string | undefined = helperMap1.get(s[i]); + let temp2: string | undefined = helperMap2.get(t[i]); + if (temp1 === undefined && temp2 === undefined) { + helperMap1.set(s[i], t[i]); + helperMap2.set(t[i], s[i]); + } else if (temp1 !== t[i] || temp2 !== s[i]) { + return false; + } + } + return true; +}; +``` + + + -----------------------
diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index e593b876..5b75e64e 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -136,17 +136,28 @@ public: ### Java ```java +/** +时间复杂度 : O(NlogN) 排序需要 O(NlogN) 的复杂度 + +空间复杂度 : O(logN) java所使用的内置函数用的是快速排序需要 logN 的空间 +*/ class Solution { public int findMinArrowShots(int[][] points) { if (points.length == 0) return 0; - Arrays.sort(points, (o1, o2) -> Integer.compare(o1[0], o2[0])); - + //用x[0] - y[0] 会大于2147483647 造成整型溢出 + Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0])); + //count = 1 因为最少需要一个箭来射击第一个气球 int count = 1; - for (int i = 1; i < points.length; i++) { - if (points[i][0] > points[i - 1][1]) { + //重叠气球的最小右边界 + int leftmostRightBound = points[0][1]; + //如果下一个气球的左边界大于最小右边界 + if (points[i][0] > leftmostRightBound ) { + //增加一次射击 count++; + leftmostRightBound = points[i][1]; + //不然就更新最小右边界 } else { - points[i][1] = Math.min(points[i][1],points[i - 1][1]); + leftmostRightBound = Math.min(leftmostRightBound , points[i][1]); } } return count; diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 291a19bd..080984f2 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -522,5 +522,39 @@ func findSubsequences(_ nums: [Int]) -> [[Int]] { ``` +## Scala + +```scala +object Solution { + import scala.collection.mutable + def findSubsequences(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(startIndex: Int): Unit = { + // 集合元素大于1,添加到结果集 + if (path.size > 1) { + result.append(path.toList) + } + + var used = new Array[Boolean](201) + // 使用循环守卫,当前层没有用过的元素才有资格进入回溯 + for (i <- startIndex until nums.size if !used(nums(i) + 100)) { + // 如果path没元素或 当前循环的元素比path的最后一个元素大,则可以进入回溯 + if (path.size == 0 || (!path.isEmpty && nums(i) >= path(path.size - 1))) { + used(nums(i) + 100) = true + path.append(nums(i)) + backtracking(i + 1) + path.remove(path.size - 1) + } + } + } + + backtracking(0) + result.toList + } +} +``` + -----------------------