mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
更新 027.移除元素 排版格式修复
This commit is contained in:
@ -5,7 +5,7 @@
|
|||||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||||
|
|
||||||
|
|
||||||
## 27. 移除元素
|
# 27. 移除元素
|
||||||
|
|
||||||
[力扣题目链接](https://leetcode.cn/problems/remove-element/)
|
[力扣题目链接](https://leetcode.cn/problems/remove-element/)
|
||||||
|
|
||||||
@ -159,8 +159,8 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java:
|
||||||
|
|
||||||
Java:
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int removeElement(int[] nums, int val) {
|
public int removeElement(int[] nums, int val) {
|
||||||
@ -197,7 +197,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
|
|
||||||
``` python 3
|
``` python 3
|
||||||
@ -233,8 +233,8 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Go:
|
||||||
|
|
||||||
Go:
|
|
||||||
```go
|
```go
|
||||||
func removeElement(nums []int, val int) int {
|
func removeElement(nums []int, val int) int {
|
||||||
length:=len(nums)
|
length:=len(nums)
|
||||||
@ -275,7 +275,8 @@ func removeElement(nums []int, val int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//时间复杂度:O(n)
|
//时间复杂度:O(n)
|
||||||
//空间复杂度:O(1)
|
//空间复杂度:O(1)
|
||||||
@ -290,7 +291,7 @@ var removeElement = (nums, val) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function removeElement(nums: number[], val: number): number {
|
function removeElement(nums: number[], val: number): number {
|
||||||
@ -305,7 +306,7 @@ function removeElement(nums: number[], val: number): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Ruby:
|
### Ruby:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
def remove_element(nums, val)
|
def remove_element(nums, val)
|
||||||
@ -319,7 +320,8 @@ def remove_element(nums, val)
|
|||||||
i
|
i
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
|
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
|
||||||
@ -335,7 +337,7 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
### Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
|
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
|
||||||
@ -351,7 +353,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
PHP:
|
### PHP:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class Solution {
|
class Solution {
|
||||||
/**
|
/**
|
||||||
@ -375,7 +378,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
C:
|
### C:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
int removeElement(int* nums, int numsSize, int val){
|
int removeElement(int* nums, int numsSize, int val){
|
||||||
int slow = 0;
|
int slow = 0;
|
||||||
@ -391,7 +395,8 @@ int removeElement(int* nums, int numsSize, int val){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Kotlin:
|
### Kotlin:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
fun removeElement(nums: IntArray, `val`: Int): Int {
|
fun removeElement(nums: IntArray, `val`: Int): Int {
|
||||||
var slowIndex = 0 // 初始化慢指针
|
var slowIndex = 0 // 初始化慢指针
|
||||||
@ -402,7 +407,8 @@ fun removeElement(nums: IntArray, `val`: Int): Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Scala:
|
### Scala:
|
||||||
|
|
||||||
```scala
|
```scala
|
||||||
object Solution {
|
object Solution {
|
||||||
def removeElement(nums: Array[Int], `val`: Int): Int = {
|
def removeElement(nums: Array[Int], `val`: Int): Int = {
|
||||||
@ -418,7 +424,8 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
C#:
|
### C#:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Solution {
|
public class Solution {
|
||||||
public int RemoveElement(int[] nums, int val) {
|
public int RemoveElement(int[] nums, int val) {
|
||||||
|
Reference in New Issue
Block a user