mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -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<n; i++) {
|
||||
let node = queue.shift();
|
||||
// 如果左右节点都是null,则该节点深度最小
|
||||
// 如果左右节点都是null(在遇见的第一个leaf节点上),则该节点深度最小
|
||||
if (node.left === null && node.right === null) {
|
||||
return deepth;
|
||||
return depth;
|
||||
}
|
||||
node.left && queue.push(node.left);;
|
||||
node.right && queue.push (node.right);
|
||||
node.right && queue.push(node.right);
|
||||
}
|
||||
}
|
||||
return deepth;
|
||||
return depth;
|
||||
};
|
||||
```
|
||||
|
||||
|
@ -156,6 +156,28 @@ var isIsomorphic = function(s, t) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function isIsomorphic(s: string, t: string): boolean {
|
||||
const helperMap1: Map<string, string> = new Map();
|
||||
const helperMap2: Map<string, string> = 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;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user