mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -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)
|
||||||
|
Reference in New Issue
Block a user