mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -339,7 +339,6 @@ int removeElement(int* nums, int numsSize, int val){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Kotlin:
|
Kotlin:
|
||||||
```kotlin
|
```kotlin
|
||||||
fun removeElement(nums: IntArray, `val`: Int): Int {
|
fun removeElement(nums: IntArray, `val`: Int): Int {
|
||||||
@ -351,7 +350,6 @@ fun removeElement(nums: IntArray, `val`: Int): Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Scala:
|
Scala:
|
||||||
```scala
|
```scala
|
||||||
object Solution {
|
object Solution {
|
||||||
@ -368,5 +366,20 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C#:
|
||||||
|
```csharp
|
||||||
|
public class Solution {
|
||||||
|
public int RemoveElement(int[] nums, int val) {
|
||||||
|
int slow = 0;
|
||||||
|
for (int fast = 0; fast < nums.Length; fast++) {
|
||||||
|
if (val != nums[fast]) {
|
||||||
|
nums[slow++] = nums[fast];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -673,5 +673,63 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
暴力:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable // 导包
|
||||||
|
def combine(n: Int, k: Int): List[List[Int]] = {
|
||||||
|
var result = mutable.ListBuffer[List[Int]]() // 存放结果集
|
||||||
|
var path = mutable.ListBuffer[Int]() //存放符合条件的结果
|
||||||
|
|
||||||
|
def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
|
||||||
|
if (path.size == k) {
|
||||||
|
// 如果path的size == k就达到题目要求,添加到结果集,并返回
|
||||||
|
result.append(path.toList)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for (i <- startIndex to n) { // 遍历从startIndex到n
|
||||||
|
path.append(i) // 先把数字添加进去
|
||||||
|
backtracking(n, k, i + 1) // 进行下一步回溯
|
||||||
|
path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(n, k, 1) // 执行回溯
|
||||||
|
result.toList // 最终返回result的List形式,return关键字可以省略
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
剪枝:
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable // 导包
|
||||||
|
def combine(n: Int, k: Int): List[List[Int]] = {
|
||||||
|
var result = mutable.ListBuffer[List[Int]]() // 存放结果集
|
||||||
|
var path = mutable.ListBuffer[Int]() //存放符合条件的结果
|
||||||
|
|
||||||
|
def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
|
||||||
|
if (path.size == k) {
|
||||||
|
// 如果path的size == k就达到题目要求,添加到结果集,并返回
|
||||||
|
result.append(path.toList)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 剪枝优化
|
||||||
|
for (i <- startIndex to (n - (k - path.size) + 1)) {
|
||||||
|
path.append(i) // 先把数字添加进去
|
||||||
|
backtracking(n, k, i + 1) // 进行下一步回溯
|
||||||
|
path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(n, k, 1) // 执行回溯
|
||||||
|
result.toList // 最终返回result的List形式,return关键字可以省略
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -346,5 +346,34 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Scala:
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable // 导包
|
||||||
|
def combine(n: Int, k: Int): List[List[Int]] = {
|
||||||
|
var result = mutable.ListBuffer[List[Int]]() // 存放结果集
|
||||||
|
var path = mutable.ListBuffer[Int]() //存放符合条件的结果
|
||||||
|
|
||||||
|
def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
|
||||||
|
if (path.size == k) {
|
||||||
|
// 如果path的size == k就达到题目要求,添加到结果集,并返回
|
||||||
|
result.append(path.toList)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 剪枝优化
|
||||||
|
for (i <- startIndex to (n - (k - path.size) + 1)) {
|
||||||
|
path.append(i) // 先把数字添加进去
|
||||||
|
backtracking(n, k, i + 1) // 进行下一步回溯
|
||||||
|
path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(n, k, 1) // 执行回溯
|
||||||
|
result.toList // 最终返回result的List形式,return关键字可以省略
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -654,8 +654,7 @@ object Solution {
|
|||||||
// 最终返回res,return关键字可以省略
|
// 最终返回res,return关键字可以省略
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class MyQueue {
|
class MyQueue {
|
||||||
var queue = ArrayBuffer[Int]()
|
var queue = ArrayBuffer[Int]()
|
||||||
@ -678,5 +677,84 @@ class MyQueue {
|
|||||||
def peek(): Int = queue.head
|
def peek(): Int = queue.head
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
PHP:
|
||||||
|
```php
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* @param Integer[] $nums
|
||||||
|
* @param Integer $k
|
||||||
|
* @return Integer[]
|
||||||
|
*/
|
||||||
|
function maxSlidingWindow($nums, $k) {
|
||||||
|
$myQueue = new MyQueue();
|
||||||
|
// 先将前k的元素放进队列
|
||||||
|
for ($i = 0; $i < $k; $i++) {
|
||||||
|
$myQueue->push($nums[$i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
$result[] = $myQueue->max(); // result 记录前k的元素的最大值
|
||||||
|
|
||||||
|
for ($i = $k; $i < count($nums); $i++) {
|
||||||
|
$myQueue->pop($nums[$i - $k]); // 滑动窗口移除最前面元素
|
||||||
|
$myQueue->push($nums[$i]); // 滑动窗口前加入最后面的元素
|
||||||
|
$result[]= $myQueue->max(); // 记录对应的最大值
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单调对列构建
|
||||||
|
class MyQueue{
|
||||||
|
private $queue;
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
$this->queue = new SplQueue(); //底层是双向链表实现。
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pop($v){
|
||||||
|
// 判断当前对列是否为空
|
||||||
|
// 比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。
|
||||||
|
// bottom 从链表前端查看元素, dequeue 从双向链表的开头移动一个节点
|
||||||
|
if(!$this->queue->isEmpty() && $v == $this->queue->bottom()){
|
||||||
|
$this->queue->dequeue(); //弹出队列
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function push($v){
|
||||||
|
// 判断当前对列是否为空
|
||||||
|
// 如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。
|
||||||
|
// 这样就保持了队列里的数值是单调从大到小的了。
|
||||||
|
while (!$this->queue->isEmpty() && $v > $this->queue->top()) {
|
||||||
|
$this->queue->pop(); // pop从链表末尾弹出一个元素,
|
||||||
|
}
|
||||||
|
$this->queue->enqueue($v);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询当前队列里的最大值 直接返回队首
|
||||||
|
public function max(){
|
||||||
|
// bottom 从链表前端查看元素, top从链表末尾查看元素
|
||||||
|
return $this->queue->bottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 辅助理解: 打印队列元素
|
||||||
|
public function println(){
|
||||||
|
// "迭代器移动到链表头部": 可理解为从头遍历链表元素做准备。
|
||||||
|
// 【PHP中没有指针概念,所以就没说指针。从数据结构上理解,就是把指针指向链表头部】
|
||||||
|
$this->queue->rewind();
|
||||||
|
|
||||||
|
echo "Println: ";
|
||||||
|
while($this->queue->valid()){
|
||||||
|
echo $this->queue->current()," -> ";
|
||||||
|
$this->queue->next();
|
||||||
|
}
|
||||||
|
echo "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -352,6 +352,24 @@ function convertBST(root: TreeNode | null): TreeNode | null {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def convertBST(root: TreeNode): TreeNode = {
|
||||||
|
var sum = 0
|
||||||
|
def convert(node: TreeNode): Unit = {
|
||||||
|
if (node == null) return
|
||||||
|
convert(node.right)
|
||||||
|
sum += node.value
|
||||||
|
node.value = sum
|
||||||
|
convert(node.left)
|
||||||
|
}
|
||||||
|
convert(root)
|
||||||
|
root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -420,6 +420,24 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C#:
|
||||||
|
```csharp
|
||||||
|
public class Solution {
|
||||||
|
public int[] SortedSquares(int[] nums) {
|
||||||
|
int k = nums.Length - 1;
|
||||||
|
int[] result = new int[nums.Length];
|
||||||
|
for (int i = 0, j = nums.Length - 1;i <= j;){
|
||||||
|
if (nums[i] * nums[i] < nums[j] * nums[j]) {
|
||||||
|
result[k--] = nums[j] * nums[j];
|
||||||
|
j--;
|
||||||
|
} else {
|
||||||
|
result[k--] = nums[i] * nums[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<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