Merge branch 'youngyangyang04:master' into master

This commit is contained in:
ironartisan
2021-08-07 21:11:00 +08:00
committed by GitHub
6 changed files with 116 additions and 7 deletions

View File

@ -216,6 +216,25 @@ fn main() {
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)
* B站视频[代码随想录](https://space.bilibili.com/525438321)

View File

@ -234,8 +234,6 @@ class Solution {
```
Python
```python3
class Solution:
@ -254,9 +252,6 @@ class Solution:
return right + 1
```
Go
JavaScript:
```js
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)

View File

@ -249,7 +249,6 @@ class Solution {
```java
// 代码精简版
class Solution {
TreeNode pre;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root.val == p.val ||root.val == q.val) return root;
TreeNode left = lowestCommonAncestor(root.left,p,q);

View File

@ -217,7 +217,25 @@ class Solution:
```
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
var reconstructQueue = function(people) {

View File

@ -9,6 +9,8 @@
## 513.找树左下角的值
题目地址:[https://leetcode-cn.com/problems/find-bottom-left-tree-value/](https://leetcode-cn.com/problems/find-bottom-left-tree-value/v)
给定一个二叉树,在树的最后一行找到最左边的值。
示例 1:

View File

@ -426,6 +426,46 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
root1.Right = mergeTrees(root1.Right, root2.Right)
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: