mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' into master
This commit is contained in:
@ -181,7 +181,71 @@ var removeNthFromEnd = function(head, n) {
|
||||
return ret.next;
|
||||
};
|
||||
```
|
||||
TypeScript:
|
||||
|
||||
版本一(快慢指针法):
|
||||
|
||||
```typescript
|
||||
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
|
||||
let newHead: ListNode | null = new ListNode(0, head);
|
||||
let slowNode: ListNode | null = newHead,
|
||||
fastNode: ListNode | null = newHead;
|
||||
for (let i = 0; i < n; i++) {
|
||||
fastNode = fastNode.next;
|
||||
}
|
||||
while (fastNode.next) {
|
||||
fastNode = fastNode.next;
|
||||
slowNode = slowNode.next;
|
||||
}
|
||||
slowNode.next = slowNode.next.next;
|
||||
return newHead.next;
|
||||
};
|
||||
```
|
||||
|
||||
版本二(计算节点总数法):
|
||||
|
||||
```typescript
|
||||
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
|
||||
let curNode: ListNode | null = head;
|
||||
let listSize: number = 0;
|
||||
while (curNode) {
|
||||
curNode = curNode.next;
|
||||
listSize++;
|
||||
}
|
||||
if (listSize === n) {
|
||||
head = head.next;
|
||||
} else {
|
||||
curNode = head;
|
||||
for (let i = 0; i < listSize - n - 1; i++) {
|
||||
curNode = curNode.next;
|
||||
}
|
||||
curNode.next = curNode.next.next;
|
||||
}
|
||||
return head;
|
||||
};
|
||||
```
|
||||
|
||||
版本三(递归倒退n法):
|
||||
|
||||
```typescript
|
||||
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
|
||||
let newHead: ListNode | null = new ListNode(0, head);
|
||||
let cnt = 0;
|
||||
function recur(node) {
|
||||
if (node === null) return;
|
||||
recur(node.next);
|
||||
cnt++;
|
||||
if (cnt === n + 1) {
|
||||
node.next = node.next.next;
|
||||
}
|
||||
}
|
||||
recur(newHead);
|
||||
return newHead.next;
|
||||
};
|
||||
```
|
||||
|
||||
Kotlin:
|
||||
|
||||
```Kotlin
|
||||
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
|
||||
val pre = ListNode(0).apply {
|
||||
|
@ -250,6 +250,38 @@ var swapPairs = function (head) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function swapPairs(head: ListNode | null): ListNode | null {
|
||||
/**
|
||||
* 初始状态:
|
||||
* curNode -> node1 -> node2 -> tmepNode
|
||||
* 转换过程:
|
||||
* curNode -> node2
|
||||
* curNode -> node2 -> node1
|
||||
* curNode -> node2 -> node1 -> tempNode
|
||||
* curNode = node1
|
||||
*/
|
||||
let retNode: ListNode | null = new ListNode(0, head),
|
||||
curNode: ListNode | null = retNode,
|
||||
node1: ListNode | null = null,
|
||||
node2: ListNode | null = null,
|
||||
tempNode: ListNode | null = null;
|
||||
|
||||
while (curNode && curNode.next && curNode.next.next) {
|
||||
node1 = curNode.next;
|
||||
node2 = curNode.next.next;
|
||||
tempNode = node2.next;
|
||||
curNode.next = node2;
|
||||
node2.next = node1;
|
||||
node1.next = tempNode;
|
||||
curNode = node1;
|
||||
}
|
||||
return retNode.next;
|
||||
};
|
||||
```
|
||||
|
||||
Kotlin:
|
||||
|
||||
```kotlin
|
||||
|
@ -432,6 +432,7 @@ func getRight(nums []int, target int) int {
|
||||
}
|
||||
}
|
||||
return border
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -574,7 +574,87 @@ var isSymmetric = function(root) {
|
||||
};
|
||||
```
|
||||
|
||||
## Swift:
|
||||
|
||||
> 递归
|
||||
```swift
|
||||
func isSymmetric(_ root: TreeNode?) -> Bool {
|
||||
return _isSymmetric(root?.left, right: root?.right)
|
||||
}
|
||||
func _isSymmetric(_ left: TreeNode?, right: TreeNode?) -> Bool {
|
||||
// 首先排除空节点情况
|
||||
if left == nil && right == nil {
|
||||
return true
|
||||
} else if left == nil && right != nil {
|
||||
return false
|
||||
} else if left != nil && right == nil {
|
||||
return false
|
||||
} else if left!.val != right!.val {
|
||||
// 进而排除数值不相等的情况
|
||||
return false
|
||||
}
|
||||
|
||||
// left 和 right 都不为空, 且数值也相等就递归
|
||||
let inSide = _isSymmetric(left!.right, right: right!.left)
|
||||
let outSide = _isSymmetric(left!.left, right: right!.right)
|
||||
return inSide && outSide
|
||||
}
|
||||
```
|
||||
|
||||
> 迭代 - 使用队列
|
||||
```swift
|
||||
func isSymmetric2(_ root: TreeNode?) -> Bool {
|
||||
guard let root = root else {
|
||||
return true
|
||||
}
|
||||
var queue = [TreeNode?]()
|
||||
queue.append(root.left)
|
||||
queue.append(root.right)
|
||||
while !queue.isEmpty {
|
||||
let left = queue.removeFirst()
|
||||
let right = queue.removeFirst()
|
||||
if left == nil && right == nil {
|
||||
continue
|
||||
}
|
||||
if left == nil || right == nil || left?.val != right?.val {
|
||||
return false
|
||||
}
|
||||
queue.append(left!.left)
|
||||
queue.append(right!.right)
|
||||
queue.append(left!.right)
|
||||
queue.append(right!.left)
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
> 迭代 - 使用栈
|
||||
```swift
|
||||
func isSymmetric3(_ root: TreeNode?) -> Bool {
|
||||
guard let root = root else {
|
||||
return true
|
||||
}
|
||||
var stack = [TreeNode?]()
|
||||
stack.append(root.left)
|
||||
stack.append(root.right)
|
||||
while !stack.isEmpty {
|
||||
let left = stack.removeLast()
|
||||
let right = stack.removeLast()
|
||||
|
||||
if left == nil && right == nil {
|
||||
continue
|
||||
}
|
||||
if left == nil || right == nil || left?.val != right?.val {
|
||||
return false
|
||||
}
|
||||
stack.append(left!.left)
|
||||
stack.append(right!.right)
|
||||
stack.append(left!.right)
|
||||
stack.append(right!.left)
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -653,5 +653,82 @@ int maxDepth(struct TreeNode* root){
|
||||
}
|
||||
```
|
||||
|
||||
## Swift
|
||||
|
||||
>二叉树最大深度
|
||||
```swift
|
||||
// 递归 - 后序
|
||||
func maxDepth1(_ root: TreeNode?) -> Int {
|
||||
return _maxDepth1(root)
|
||||
}
|
||||
func _maxDepth1(_ root: TreeNode?) -> Int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
let leftDepth = _maxDepth1(root!.left)
|
||||
let rightDepth = _maxDepth1(root!.right)
|
||||
return 1 + max(leftDepth, rightDepth)
|
||||
}
|
||||
|
||||
// 层序
|
||||
func maxDepth(_ root: TreeNode?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
var queue = [TreeNode]()
|
||||
queue.append(root)
|
||||
var res: Int = 0
|
||||
while !queue.isEmpty {
|
||||
res += 1
|
||||
for _ in 0 ..< queue.count {
|
||||
let node = queue.removeFirst()
|
||||
if let left = node.left {
|
||||
queue.append(left)
|
||||
}
|
||||
if let right = node.right {
|
||||
queue.append(right)
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
>N叉树最大深度
|
||||
```swift
|
||||
// 递归
|
||||
func maxDepth(_ root: Node?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
var depth = 0
|
||||
for node in root.children {
|
||||
depth = max(depth, maxDepth(node))
|
||||
}
|
||||
return depth + 1
|
||||
}
|
||||
|
||||
// 迭代-层序遍历
|
||||
func maxDepth1(_ root: Node?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
var depth = 0
|
||||
var queue = [Node]()
|
||||
queue.append(root)
|
||||
while !queue.isEmpty {
|
||||
let size = queue.count
|
||||
depth += 1
|
||||
for _ in 0 ..< size {
|
||||
let node = queue.removeFirst()
|
||||
for child in node.children {
|
||||
queue.append(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
return depth
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -684,7 +684,7 @@ class Solution:
|
||||
root.right = self.buildTree(preorder_right, inorder_right)
|
||||
|
||||
return root
|
||||
```
|
||||
```
|
||||
|
||||
106.从中序与后序遍历序列构造二叉树
|
||||
|
||||
@ -716,7 +716,7 @@ class Solution:
|
||||
root.right = self.buildTree(inorder_right, postorder_right)
|
||||
|
||||
return root
|
||||
```
|
||||
```
|
||||
|
||||
## Go
|
||||
|
||||
@ -816,6 +816,7 @@ var buildTree = function(preorder, inorder) {
|
||||
|
||||
## C
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
```c
|
||||
int linearSearch(int* arr, int arrSize, int key) {
|
||||
int i;
|
||||
@ -847,6 +848,7 @@ struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int po
|
||||
```
|
||||
|
||||
105 从前序与中序遍历序列构造二叉树
|
||||
|
||||
```c
|
||||
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
|
||||
// 递归结束条件:传入的数组大小为0
|
||||
@ -889,5 +891,100 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
|
||||
}
|
||||
```
|
||||
|
||||
## Swift
|
||||
|
||||
105 从前序与中序遍历序列构造二叉树
|
||||
|
||||
```swift
|
||||
class Solution {
|
||||
func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? {
|
||||
return helper(preorder: preorder,
|
||||
preorderBegin: 0,
|
||||
preorderEnd: preorder.count,
|
||||
inorder: inorder,
|
||||
inorderBegin: 0,
|
||||
inorderEnd: inorder.count)
|
||||
}
|
||||
|
||||
func helper(preorder: [Int], preorderBegin: Int, preorderEnd: Int, inorder: [Int], inorderBegin: Int, inorderEnd: Int) -> TreeNode? {
|
||||
if preorderBegin == preorderEnd {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 前序遍历数组的第一个元素作为分割点
|
||||
let rootValue = preorder[preorderBegin]
|
||||
let root = TreeNode(rootValue)
|
||||
|
||||
|
||||
if preorderEnd - preorderBegin == 1 {
|
||||
return root
|
||||
}
|
||||
|
||||
var index = 0 // 从中序遍历数组中找到根节点的下标
|
||||
if let ind = inorder.firstIndex(of: rootValue) {
|
||||
index = ind
|
||||
}
|
||||
|
||||
// 递归
|
||||
root.left = helper(preorder: preorder,
|
||||
preorderBegin: preorderBegin + 1,
|
||||
preorderEnd: preorderBegin + 1 + index - inorderBegin,
|
||||
inorder: inorder,
|
||||
inorderBegin: inorderBegin,
|
||||
inorderEnd: index)
|
||||
root.right = helper(preorder: preorder,
|
||||
preorderBegin: preorderBegin + 1 + index - inorderBegin,
|
||||
preorderEnd: preorderEnd,
|
||||
inorder: inorder,
|
||||
inorderBegin: index + 1,
|
||||
inorderEnd: inorderEnd)
|
||||
return root
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
```swift
|
||||
class Solution_0106 {
|
||||
func buildTree(inorder: [Int], inorderBegin: Int, inorderEnd: Int, postorder: [Int], postorderBegin: Int, postorderEnd: Int) -> TreeNode? {
|
||||
if postorderEnd - postorderBegin < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 后序遍历数组的最后一个元素作为分割点
|
||||
let rootValue = postorder[postorderEnd - 1]
|
||||
let root = TreeNode(rootValue)
|
||||
|
||||
if postorderEnd - postorderBegin == 1 {
|
||||
return root
|
||||
}
|
||||
|
||||
// 从中序遍历数组中找到根节点的下标
|
||||
var delimiterIndex = 0
|
||||
if let index = inorder.firstIndex(of: rootValue) {
|
||||
delimiterIndex = index
|
||||
}
|
||||
|
||||
root.left = buildTree(inorder: inorder,
|
||||
inorderBegin: inorderBegin,
|
||||
inorderEnd: delimiterIndex,
|
||||
postorder: postorder,
|
||||
postorderBegin: postorderBegin,
|
||||
postorderEnd: postorderBegin + (delimiterIndex - inorderBegin))
|
||||
|
||||
root.right = buildTree(inorder: inorder,
|
||||
inorderBegin: delimiterIndex + 1,
|
||||
inorderEnd: inorderEnd,
|
||||
postorder: postorder,
|
||||
postorderBegin: postorderBegin + (delimiterIndex - inorderBegin),
|
||||
postorderEnd: postorderEnd - 1)
|
||||
return root
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -404,8 +404,51 @@ var minDepth = function(root) {
|
||||
};
|
||||
```
|
||||
|
||||
## Swift
|
||||
|
||||
> 递归
|
||||
```Swift
|
||||
func minDepth(_ root: TreeNode?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
if root.left == nil && root.right != nil {
|
||||
return 1 + minDepth(root.right)
|
||||
}
|
||||
if root.left != nil && root.right == nil {
|
||||
return 1 + minDepth(root.left)
|
||||
}
|
||||
return 1 + min(minDepth(root.left), minDepth(root.right))
|
||||
}
|
||||
```
|
||||
|
||||
> 迭代
|
||||
```Swift
|
||||
func minDepth(_ root: TreeNode?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
var res = 0
|
||||
var queue = [TreeNode]()
|
||||
queue.append(root)
|
||||
while !queue.isEmpty {
|
||||
res += 1
|
||||
for _ in 0 ..< queue.count {
|
||||
let node = queue.removeFirst()
|
||||
if node.left == nil && node.right == nil {
|
||||
return res
|
||||
}
|
||||
if let left = node.left {
|
||||
queue.append(left)
|
||||
}
|
||||
if let right = node.right {
|
||||
queue.append(right)
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -302,7 +302,63 @@ var removeElements = function(head, val) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
版本一(在原链表上直接删除):
|
||||
|
||||
```typescript
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* class ListNode {
|
||||
* val: number
|
||||
* next: ListNode | null
|
||||
* constructor(val?: number, next?: ListNode | null) {
|
||||
* this.val = (val===undefined ? 0 : val)
|
||||
* this.next = (next===undefined ? null : next)
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
function removeElements(head: ListNode | null, val: number): ListNode | null {
|
||||
// 删除头部节点
|
||||
while (head !== null && head.val === val) {
|
||||
head = head.next;
|
||||
}
|
||||
if (head === null) return head;
|
||||
let pre: ListNode = head, cur: ListNode = head.next;
|
||||
// 删除非头部节点
|
||||
while (cur) {
|
||||
if (cur.val === val) {
|
||||
pre.next = cur.next;
|
||||
} else {
|
||||
pre = pre.next;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
return head;
|
||||
};
|
||||
```
|
||||
|
||||
版本二(虚拟头节点):
|
||||
|
||||
```typescript
|
||||
function removeElements(head: ListNode | null, val: number): ListNode | null {
|
||||
head = new ListNode(0, head);
|
||||
let pre: ListNode = head, cur: ListNode = head.next;
|
||||
// 删除非头部节点
|
||||
while (cur) {
|
||||
if (cur.val === val) {
|
||||
pre.next = cur.next;
|
||||
} else {
|
||||
pre = pre.next;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
return head.next;
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
|
@ -314,6 +314,54 @@ var reverseList = function(head) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
// 双指针法
|
||||
function reverseList(head: ListNode | null): ListNode | null {
|
||||
let preNode: ListNode | null = null,
|
||||
curNode: ListNode | null = head,
|
||||
tempNode: ListNode | null;
|
||||
while (curNode) {
|
||||
tempNode = curNode.next;
|
||||
curNode.next = preNode;
|
||||
preNode = curNode;
|
||||
curNode = tempNode;
|
||||
}
|
||||
return preNode;
|
||||
};
|
||||
|
||||
// 递归(从前往后翻转)
|
||||
function reverseList(head: ListNode | null): ListNode | null {
|
||||
function recur(preNode: ListNode | null, curNode: ListNode | null): ListNode | null {
|
||||
if (curNode === null) return preNode;
|
||||
let tempNode: ListNode | null = curNode.next;
|
||||
curNode.next = preNode;
|
||||
preNode = curNode;
|
||||
curNode = tempNode;
|
||||
return recur(preNode, curNode);
|
||||
}
|
||||
return recur(null, head);
|
||||
};
|
||||
|
||||
// 递归(从后往前翻转)
|
||||
function reverseList(head: ListNode | null): ListNode | null {
|
||||
if (head === null) return null;
|
||||
let newHead: ListNode | null;
|
||||
function recur(node: ListNode | null, preNode: ListNode | null): void {
|
||||
if (node.next === null) {
|
||||
newHead = node;
|
||||
newHead.next = preNode;
|
||||
} else {
|
||||
recur(node.next, node);
|
||||
node.next = preNode;
|
||||
}
|
||||
}
|
||||
recur(head, null);
|
||||
return newHead;
|
||||
};
|
||||
```
|
||||
|
||||
Ruby:
|
||||
|
||||
```ruby
|
||||
|
@ -522,5 +522,73 @@ int countNodes(struct TreeNode* root){
|
||||
}
|
||||
```
|
||||
|
||||
## Swift:
|
||||
|
||||
> 递归
|
||||
```swift
|
||||
func countNodes(_ root: TreeNode?) -> Int {
|
||||
return _countNodes(root)
|
||||
}
|
||||
func _countNodes(_ root: TreeNode?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
let leftCount = _countNodes(root.left)
|
||||
let rightCount = _countNodes(root.right)
|
||||
return 1 + leftCount + rightCount
|
||||
}
|
||||
```
|
||||
|
||||
> 层序遍历
|
||||
```Swift
|
||||
func countNodes(_ root: TreeNode?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
var res = 0
|
||||
var queue = [TreeNode]()
|
||||
queue.append(root)
|
||||
while !queue.isEmpty {
|
||||
let size = queue.count
|
||||
for _ in 0 ..< size {
|
||||
let node = queue.removeFirst()
|
||||
res += 1
|
||||
if let left = node.left {
|
||||
queue.append(left)
|
||||
}
|
||||
if let right = node.right {
|
||||
queue.append(right)
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
> 利用完全二叉树性质
|
||||
```Swift
|
||||
func countNodes(_ root: TreeNode?) -> Int {
|
||||
guard let root = root else {
|
||||
return 0
|
||||
}
|
||||
var leftNode = root.left
|
||||
var rightNode = root.right
|
||||
var leftDepth = 0
|
||||
var rightDepth = 0
|
||||
while leftNode != nil {
|
||||
leftNode = leftNode!.left
|
||||
leftDepth += 1
|
||||
}
|
||||
while rightNode != nil {
|
||||
rightNode = rightNode!.right
|
||||
rightDepth += 1
|
||||
}
|
||||
if leftDepth == rightDepth {
|
||||
return (2 << leftDepth) - 1
|
||||
}
|
||||
return countNodes(root.left) + countNodes(root.right) + 1
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -168,6 +168,32 @@ public class ListNode {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
|
||||
```javascript
|
||||
class ListNode {
|
||||
val;
|
||||
next = null;
|
||||
constructor(value) {
|
||||
this.val = value;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
class ListNode {
|
||||
public val: number;
|
||||
public next: ListNode = null;
|
||||
constructor(value: number) {
|
||||
this.val = value;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user