vault backup: 2026-03-05 12:28:58
This commit is contained in:
@@ -57,123 +57,7 @@ nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Go 解法
|
## 解法
|
||||||
|
|
||||||
```go
|
|
||||||
func threeSum(nums []int) [][]int {
|
|
||||||
result := [][]int{}
|
|
||||||
n := len(nums)
|
|
||||||
|
|
||||||
// 排序
|
|
||||||
sort.Ints(nums)
|
|
||||||
|
|
||||||
for i := 0; i < n-2; i++ {
|
|
||||||
// 去重:跳过重复的第一个数
|
|
||||||
if i > 0 && nums[i] == nums[i-1] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 优化:如果最小数 > 0,后面不可能有解
|
|
||||||
if nums[i] > 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// 双指针
|
|
||||||
left, right := i+1, n-1
|
|
||||||
for left < right {
|
|
||||||
sum := nums[i] + nums[left] + nums[right]
|
|
||||||
|
|
||||||
if sum == 0 {
|
|
||||||
result = append(result, []int{nums[i], nums[left], nums[right]})
|
|
||||||
|
|
||||||
// 去重:跳过重复的 left
|
|
||||||
for left < right && nums[left] == nums[left+1] {
|
|
||||||
left++
|
|
||||||
}
|
|
||||||
// 去重:跳过重复的 right
|
|
||||||
for left < right && nums[right] == nums[right-1] {
|
|
||||||
right--
|
|
||||||
}
|
|
||||||
|
|
||||||
// 移动指针
|
|
||||||
left++
|
|
||||||
right--
|
|
||||||
} else if sum < 0 {
|
|
||||||
// 和太小,left 向右移
|
|
||||||
left++
|
|
||||||
} else {
|
|
||||||
// 和太大,right 向左移
|
|
||||||
right--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Go 代码要点
|
|
||||||
1. `sort.Ints()` 排序
|
|
||||||
2. `append()` 添加结果
|
|
||||||
3. 多重去重逻辑
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Java 解法
|
|
||||||
|
|
||||||
```java
|
|
||||||
class Solution {
|
|
||||||
public List<List<Integer>> threeSum(int[] nums) {
|
|
||||||
List<List<Integer>> result = new ArrayList<>();
|
|
||||||
Arrays.sort(nums);
|
|
||||||
|
|
||||||
for (int i = 0; i < nums.length - 2; i++) {
|
|
||||||
// 去重:跳过重复的第一个数
|
|
||||||
if (i > 0 && nums[i] == nums[i - 1]) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 优化:如果最小数 > 0,后面不可能有解
|
|
||||||
if (nums[i] > 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 双指针
|
|
||||||
int left = i + 1, right = nums.length - 1;
|
|
||||||
while (left < right) {
|
|
||||||
int sum = nums[i] + nums[left] + nums[right];
|
|
||||||
|
|
||||||
if (sum == 0) {
|
|
||||||
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
|
|
||||||
|
|
||||||
// 去重:跳过重复的 left
|
|
||||||
while (left < right && nums[left] == nums[left + 1]) {
|
|
||||||
left++;
|
|
||||||
}
|
|
||||||
// 去重:跳过重复的 right
|
|
||||||
while (left < right && nums[right] == nums[right - 1]) {
|
|
||||||
right--;
|
|
||||||
}
|
|
||||||
|
|
||||||
left++;
|
|
||||||
right--;
|
|
||||||
} else if (sum < 0) {
|
|
||||||
left++;
|
|
||||||
} else {
|
|
||||||
right--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Java 代码要点
|
|
||||||
1. `Arrays.sort()` 排序
|
|
||||||
2. `Arrays.asList()` 创建列表
|
|
||||||
3. `ArrayList` 存储结果
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -49,113 +49,7 @@ LeetCode 2. Medium
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Go 解法
|
## 解法
|
||||||
|
|
||||||
```go
|
|
||||||
/**
|
|
||||||
* Definition for singly-linked list.
|
|
||||||
* type ListNode struct {
|
|
||||||
* Val int
|
|
||||||
* Next *ListNode
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
|
||||||
// 哑节点,简化边界处理
|
|
||||||
dummy := &ListNode{Val: 0}
|
|
||||||
current := dummy
|
|
||||||
carry := 0
|
|
||||||
|
|
||||||
// 同时遍历两个链表
|
|
||||||
for l1 != nil || l2 != nil {
|
|
||||||
// 获取当前位的值(如果链表已结束则为0)
|
|
||||||
x, y := 0, 0
|
|
||||||
if l1 != nil {
|
|
||||||
x = l1.Val
|
|
||||||
l1 = l1.Next
|
|
||||||
}
|
|
||||||
if l2 != nil {
|
|
||||||
y = l2.Val
|
|
||||||
l2 = l2.Next
|
|
||||||
}
|
|
||||||
|
|
||||||
// 计算和与进位
|
|
||||||
sum := x + y + carry
|
|
||||||
carry = sum / 10
|
|
||||||
|
|
||||||
// 创建新节点
|
|
||||||
current.Next = &ListNode{Val: sum % 10}
|
|
||||||
current = current.Next
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理最后的进位
|
|
||||||
if carry > 0 {
|
|
||||||
current.Next = &ListNode{Val: carry}
|
|
||||||
}
|
|
||||||
|
|
||||||
return dummy.Next
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Go 代码要点
|
|
||||||
1. 使用哑节点简化头节点处理
|
|
||||||
2. 注意 Go 的 nil 判断
|
|
||||||
3. 整数除法和取模:`sum / 10` 和 `sum % 10`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Java 解法
|
|
||||||
|
|
||||||
```java
|
|
||||||
/**
|
|
||||||
* Definition for singly-linked list.
|
|
||||||
* public class ListNode {
|
|
||||||
* int val;
|
|
||||||
* ListNode next;
|
|
||||||
* ListNode() {}
|
|
||||||
* ListNode(int val) { this.val = val; }
|
|
||||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
class Solution {
|
|
||||||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
|
|
||||||
// 哑节点
|
|
||||||
ListNode dummy = new ListNode(0);
|
|
||||||
ListNode current = dummy;
|
|
||||||
int carry = 0;
|
|
||||||
|
|
||||||
// 同时遍历两个链表
|
|
||||||
while (l1 != null || l2 != null) {
|
|
||||||
// 获取当前位的值
|
|
||||||
int x = (l1 != null) ? l1.val : 0;
|
|
||||||
int y = (l2 != null) ? l2.val : 0;
|
|
||||||
|
|
||||||
// 计算和与进位
|
|
||||||
int sum = x + y + carry;
|
|
||||||
carry = sum / 10;
|
|
||||||
|
|
||||||
// 创建新节点
|
|
||||||
current.next = new ListNode(sum % 10);
|
|
||||||
current = current.next;
|
|
||||||
|
|
||||||
// 移动指针
|
|
||||||
if (l1 != null) l1 = l1.next;
|
|
||||||
if (l2 != null) l2 = l2.next;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理最后的进位
|
|
||||||
if (carry > 0) {
|
|
||||||
current.next = new ListNode(carry);
|
|
||||||
}
|
|
||||||
|
|
||||||
return dummy.next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Java 代码要点
|
|
||||||
1. 三元运算符处理空指针
|
|
||||||
2. 对象引用操作(current.next)
|
|
||||||
3. 注意 null 判断
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
### 方法二:迭代(栈)
|
### 方法二:迭代(栈)
|
||||||
|
|
||||||
## Go 代码(迭代)
|
## 解法
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func inorderTraversal(root *TreeNode) []int {
|
func inorderTraversal(root *TreeNode) []int {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
### DFS / BFS
|
### DFS / BFS
|
||||||
|
|
||||||
## Go 代码(DFS)
|
## 解法
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func maxDepth(root *TreeNode) int {
|
func maxDepth(root *TreeNode) int {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
前序遍历:[根, [左子树], [右子树]]
|
前序遍历:[根, [左子树], [右子树]]
|
||||||
中序遍历:[[左子树], 根, [右子树]]
|
中序遍历:[[左子树], 根, [右子树]]
|
||||||
|
|
||||||
## Go 代码
|
## 解法
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func buildTree(preorder []int, inorder []int) *TreeNode {
|
func buildTree(preorder []int, inorder []int) *TreeNode {
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Java 实现(双指针法)
|
### 解法
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class RemoveNthFromEnd {
|
public class RemoveNthFromEnd {
|
||||||
|
|||||||
Reference in New Issue
Block a user