Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
programmercarl
2024-04-24 16:55:51 +08:00
13 changed files with 185 additions and 48 deletions

View File

@ -181,6 +181,23 @@ class Solution {
} }
``` ```
```java
// 将步骤 2,3 交换顺序,这样不用定义 temp 节点
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while (cur.next != null && cur.next.next != null) {
ListNode node1 = cur.next;// 第 1 个节点
ListNode node2 = cur.next.next;// 第 2 个节点
cur.next = node2; // 步骤 1
node1.next = node2.next;// 步骤 3
node2.next = node1;// 步骤 2
cur = cur.next.next;
}
return dummy.next;
}
```
### Python ### Python
```python ```python
@ -285,6 +302,21 @@ var swapPairs = function (head) {
}; };
``` ```
```javascript
// 递归版本
var swapPairs = function (head) {
if (head == null || head.next == null) {
return head;
}
let after = head.next;
head.next = swapPairs(after.next);
after.next = head;
return after;
};
```
### TypeScript ### TypeScript
```typescript ```typescript

View File

@ -564,6 +564,38 @@ public:
## 其他语言版本 ## 其他语言版本
### Java ### Java
```Java
class Solution {
/**
牺牲空间,换取最直白的暴力法
时间复杂度 O(n * m)
空间 O(n + m)
*/
public int strStr(String haystack, String needle) {
// 获取 haystack 和 needle 的长度
int n = haystack.length(), m = needle.length();
// 将字符串转换为字符数组,方便索引操作
char[] s = haystack.toCharArray(), p = needle.toCharArray();
// 遍历 haystack 字符串
for (int i = 0; i < n - m + 1; i++) {
// 初始化匹配的指针
int a = i, b = 0;
// 循环检查 needle 是否在当前位置开始匹配
while (b < m && s[a] == p[b]) {
// 如果当前字符匹配,则移动指针
a++;
b++;
}
// 如果 b 等于 m说明 needle 已经完全匹配,返回当前位置 i
if (b == m) return i;
}
// 如果遍历完毕仍未找到匹配的子串,则返回 -1
return -1;
}
}
```
```Java ```Java
class Solution { class Solution {

View File

@ -332,6 +332,7 @@ impl Solution {
### Python ### Python
```python ```python
# 第一种二分法: [left, right]左闭右闭区间
class Solution: class Solution:
def searchInsert(self, nums: List[int], target: int) -> int: def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1 left, right = 0, len(nums) - 1
@ -348,6 +349,26 @@ class Solution:
return right + 1 return right + 1
``` ```
```python
# 第二种二分法: [left, right)左闭右开区间
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while (left < right):
middle = (left + right) // 2
if nums[middle] > target:
right = middle
elif nums[middle] < target:
left = middle + 1
else:
return middle
return right
```
### JavaScript ### JavaScript
```js ```js

View File

@ -77,7 +77,7 @@ return depth;
所以整体c++代码如下: 所以整体c++代码如下:
```CPP ```CPP
class solution { class Solution {
public: public:
int getdepth(TreeNode* node) { int getdepth(TreeNode* node) {
if (node == NULL) return 0; if (node == NULL) return 0;
@ -94,7 +94,7 @@ public:
代码精简之后c++代码如下: 代码精简之后c++代码如下:
```CPP ```CPP
class solution { class Solution {
public: public:
int maxDepth(TreeNode* root) { int maxDepth(TreeNode* root) {
if (root == null) return 0; if (root == null) return 0;
@ -110,7 +110,7 @@ public:
本题当然也可以使用前序,代码如下:(**充分表现出求深度回溯的过程**) 本题当然也可以使用前序,代码如下:(**充分表现出求深度回溯的过程**)
```CPP ```CPP
class solution { class Solution {
public: public:
int result; int result;
void getdepth(TreeNode* node, int depth) { void getdepth(TreeNode* node, int depth) {
@ -144,7 +144,7 @@ public:
注意以上代码是为了把细节体现出来,简化一下代码如下: 注意以上代码是为了把细节体现出来,简化一下代码如下:
```CPP ```CPP
class solution { class Solution {
public: public:
int result; int result;
void getdepth(TreeNode* node, int depth) { void getdepth(TreeNode* node, int depth) {
@ -183,7 +183,7 @@ public:
c++代码如下: c++代码如下:
```CPP ```CPP
class solution { class Solution {
public: public:
int maxDepth(TreeNode* root) { int maxDepth(TreeNode* root) {
if (root == NULL) return 0; if (root == NULL) return 0;
@ -232,7 +232,7 @@ public:
c++代码: c++代码:
```CPP ```CPP
class solution { class Solution {
public: public:
int maxDepth(Node* root) { int maxDepth(Node* root) {
if (root == 0) return 0; if (root == 0) return 0;
@ -249,7 +249,7 @@ public:
依然是层序遍历,代码如下: 依然是层序遍历,代码如下:
```CPP ```CPP
class solution { class Solution {
public: public:
int maxDepth(Node* root) { int maxDepth(Node* root) {
queue<Node*> que; queue<Node*> que;
@ -278,7 +278,7 @@ public:
104.二叉树的最大深度 104.二叉树的最大深度
```java ```java
class solution { class Solution {
/** /**
* 递归法 * 递归法
*/ */
@ -319,7 +319,7 @@ class Solution {
``` ```
```java ```java
class solution { class Solution {
/** /**
* 迭代法,使用层序遍历 * 迭代法,使用层序遍历
*/ */
@ -369,7 +369,7 @@ class Solution {
``` ```
```java ```java
class solution { class Solution {
/** /**
* 迭代法,使用层序遍历 * 迭代法,使用层序遍历
*/ */
@ -402,7 +402,7 @@ class solution {
递归法: 递归法:
```python ```python
class solution: class Solution:
def maxdepth(self, root: treenode) -> int: def maxdepth(self, root: treenode) -> int:
return self.getdepth(root) return self.getdepth(root)
@ -417,7 +417,7 @@ class solution:
递归法:精简代码 递归法:精简代码
```python ```python
class solution: class Solution:
def maxdepth(self, root: treenode) -> int: def maxdepth(self, root: treenode) -> int:
if not root: if not root:
return 0 return 0

View File

@ -334,6 +334,18 @@ class Solution:
return root return root
``` ```
递归 精简(自身调用)
```python
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid + 1 :])
return root
```
迭代法 迭代法
```python ```python

View File

@ -497,27 +497,67 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
```php ```php
/** /**
* Definition for singly-linked list. * Definition for a singly-linked list.
* type ListNode struct { * class ListNode {
* Val int * public $val = 0;
* Next *ListNode * public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* } * }
*/ */
// 虚拟头+双指针
func removeElements(head *ListNode, val int) *ListNode { //版本一(在原链表上直接删除):
dummyHead := &ListNode{} class Solution {
dummyHead.Next = head
pred := dummyHead /**
cur := head * @param ListNode $head
for cur != nil { * @param Integer $val
if cur.Val == val { * @return ListNode
pred.Next = cur.Next */
function removeElements($head, $val)
{
if ($head == null) {
return null;
}
$now = $head;
while ($now->next != null) {
if ($now->next->val == $val) {
$now->next = $now->next->next;
} else { } else {
pred = cur $now = $now->next;
} }
cur = cur.Next
} }
return dummyHead.Next if ($head->val == $val) {
return $head->next;
}
return $head;
}
}
//版本二(虚拟头结点方式):
class Solution {
/**
* @param ListNode $head
* @param Integer $val
* @return ListNode
*/
function removeElements($head, $val)
{
$dummyHead = new ListNode(0, $head);
$now = $dummyHead;
while ($now->next != null){
if ($now->next->val == $val) {
$now->next = $now->next->next;
} else {
$now = $now->next;
}
}
return $dummyHead->next;
}
} }
``` ```

View File

@ -13,9 +13,9 @@
而本题的迭代法中我们使用了队列,需要注意的是这不是层序遍历,而且仅仅通过一个容器来成对的存放我们要比较的元素,认识到这一点之后就发现:用队列,用栈,甚至用数组,都是可以的。 而本题的迭代法中我们使用了队列,需要注意的是这不是层序遍历,而且仅仅通过一个容器来成对的存放我们要比较的元素,认识到这一点之后就发现:用队列,用栈,甚至用数组,都是可以的。
那么做完本题之后,看如下两个题目。 那么做完本题之后,看如下两个题目。
* 100.相同的树 * [100.相同的树](https://leetcode.cn/problems/same-tree/description/)
* 572.另一个树的子树 * [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/)
**[二叉树:我对称么?](https://programmercarl.com/0101.对称二叉树.html)中的递归法和迭代法只需要稍作修改其中一个树的遍历顺序便可刷了100.相同的树。** **[二叉树:我对称么?](https://programmercarl.com/0101.对称二叉树.html)中的递归法和迭代法只需要稍作修改其中一个树的遍历顺序便可刷了100.相同的树。**