mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -216,6 +216,25 @@ fn main() {
|
|||||||
println!("{:?}",remove_element(&mut nums, 5));
|
println!("{:?}",remove_element(&mut nums, 5));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Swift:
|
||||||
|
|
||||||
|
```swift
|
||||||
|
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
|
||||||
|
var slowIndex = 0
|
||||||
|
|
||||||
|
for fastIndex in 0..<nums.count {
|
||||||
|
if val != nums[fastIndex] {
|
||||||
|
if slowIndex != fastIndex {
|
||||||
|
nums[slowIndex] = nums[fastIndex]
|
||||||
|
}
|
||||||
|
slowIndex += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slowIndex
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
@ -234,8 +234,6 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -254,9 +252,6 @@ class Solution:
|
|||||||
return right + 1
|
return right + 1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
```js
|
```js
|
||||||
var searchInsert = function (nums, target) {
|
var searchInsert = function (nums, target) {
|
||||||
@ -277,6 +272,42 @@ var searchInsert = function (nums, target) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Swift:
|
||||||
|
|
||||||
|
```swift
|
||||||
|
// 暴力法
|
||||||
|
func searchInsert(_ nums: [Int], _ target: Int) -> Int {
|
||||||
|
for i in 0..<nums.count {
|
||||||
|
if nums[i] >= target {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nums.count
|
||||||
|
}
|
||||||
|
|
||||||
|
// 二分法
|
||||||
|
func searchInsert(_ nums: [Int], _ target: Int) -> Int {
|
||||||
|
var left = 0
|
||||||
|
var right = nums.count - 1
|
||||||
|
|
||||||
|
while left <= right {
|
||||||
|
let middle = left + ((right - left) >> 1)
|
||||||
|
|
||||||
|
if nums[middle] > target {
|
||||||
|
right = middle - 1
|
||||||
|
}else if nums[middle] < target {
|
||||||
|
left = middle + 1
|
||||||
|
}else if nums[middle] == target {
|
||||||
|
return middle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return right + 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -249,7 +249,6 @@ class Solution {
|
|||||||
```java
|
```java
|
||||||
// 代码精简版
|
// 代码精简版
|
||||||
class Solution {
|
class Solution {
|
||||||
TreeNode pre;
|
|
||||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
if (root == null || root.val == p.val ||root.val == q.val) return root;
|
if (root == null || root.val == p.val ||root.val == q.val) return root;
|
||||||
TreeNode left = lowestCommonAncestor(root.left,p,q);
|
TreeNode left = lowestCommonAncestor(root.left,p,q);
|
||||||
|
@ -217,7 +217,25 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```golang
|
||||||
|
func reconstructQueue(people [][]int) [][]int {
|
||||||
|
//先将身高从大到小排序,确定最大个子的相对位置
|
||||||
|
sort.Slice(people,func(i,j int)bool{
|
||||||
|
if people[i][0]==people[j][0]{
|
||||||
|
return people[i][1]<people[j][1]//这个才是当身高相同时,将K按照从小到大排序
|
||||||
|
}
|
||||||
|
return people[i][0]>people[j][0]//这个只是确保身高按照由大到小的顺序来排,并不确定K是按照从小到大排序的
|
||||||
|
})
|
||||||
|
//再按照K进行插入排序,优先插入K小的
|
||||||
|
result := make([][]int, 0)
|
||||||
|
for _, info := range people {
|
||||||
|
result = append(result, info)
|
||||||
|
copy(result[info[1] +1:], result[info[1]:])//将插入位置之后的元素后移动一位(意思是腾出空间)
|
||||||
|
result[info[1]] = info//将插入元素位置插入元素
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
```
|
||||||
Javascript:
|
Javascript:
|
||||||
```Javascript
|
```Javascript
|
||||||
var reconstructQueue = function(people) {
|
var reconstructQueue = function(people) {
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
## 513.找树左下角的值
|
## 513.找树左下角的值
|
||||||
|
|
||||||
|
题目地址:[https://leetcode-cn.com/problems/find-bottom-left-tree-value/](https://leetcode-cn.com/problems/find-bottom-left-tree-value/v)
|
||||||
|
|
||||||
给定一个二叉树,在树的最后一行找到最左边的值。
|
给定一个二叉树,在树的最后一行找到最左边的值。
|
||||||
|
|
||||||
示例 1:
|
示例 1:
|
||||||
|
@ -426,6 +426,46 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
|
|||||||
root1.Right = mergeTrees(root1.Right, root2.Right)
|
root1.Right = mergeTrees(root1.Right, root2.Right)
|
||||||
return root1
|
return root1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 迭代版本
|
||||||
|
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
|
||||||
|
queue := make([]*TreeNode,0)
|
||||||
|
if root1 == nil{
|
||||||
|
return root2
|
||||||
|
}
|
||||||
|
if root2 == nil{
|
||||||
|
return root1
|
||||||
|
}
|
||||||
|
queue = append(queue,root1)
|
||||||
|
queue = append(queue,root2)
|
||||||
|
|
||||||
|
for size:=len(queue);size>0;size=len(queue){
|
||||||
|
node1 := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
node2 := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
node1.Val += node2.Val
|
||||||
|
// 左子树都不为空
|
||||||
|
if node1.Left != nil && node2.Left != nil{
|
||||||
|
queue = append(queue,node1.Left)
|
||||||
|
queue = append(queue,node2.Left)
|
||||||
|
}
|
||||||
|
// 右子树都不为空
|
||||||
|
if node1.Right !=nil && node2.Right !=nil{
|
||||||
|
queue = append(queue,node1.Right)
|
||||||
|
queue = append(queue,node2.Right)
|
||||||
|
}
|
||||||
|
// 树 1 的左子树为 nil,直接接上树 2 的左子树
|
||||||
|
if node1.Left == nil{
|
||||||
|
node1.Left = node2.Left
|
||||||
|
}
|
||||||
|
// 树 1 的右子树为 nil,直接接上树 2 的右子树
|
||||||
|
if node1.Right == nil{
|
||||||
|
node1.Right = node2.Right
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root1
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
Reference in New Issue
Block a user