有一个非空的乱序数组,其中有若干个0。请将所有的0,移到数组末尾
时间: 2023-04-05 11:03:13 浏览: 178
可以使用双指针法,一个指针指向当前已经处理好的非0元素的末尾,另一个指针不断向后扫描,遇到非0元素就与第一个指针指向的位置交换,然后第一个指针向后移动一位。最后,所有的0都被移到了数组末尾。
具体实现如下:
void moveZeroes(int* nums, int numsSize){
int i = 0, j = 0;
for (; j < numsSize; j++) {
if (nums[j] != 0) {
nums[i++] = nums[j];
}
}
while (i < numsSize) {
nums[i++] = 0;
}
}
相关问题
有一个非空的乱序数组,其中有若干个0。请将所有的0,移动到数组末尾。 编程用c++/python/ java其中的一种实现。
Python实现:
```python
def move_zeros(nums):
count = 0 # 记录0的个数
for i in range(len(nums)):
if nums[i] != 0:
nums[count] = nums[i]
count += 1
while count < len(nums):
nums[count] = 0
count += 1
return nums
```
该函数遍历数组,将所有非0的元素移到数组前面,并记录0的个数count。然后将数组后面的count个元素置为0。
示例:
```python
>>> nums = [0, 1, 0, 3, 12]
>>> move_zeros(nums)
[1, 3, 12, 0, 0]
```
C语言实现:
```c
void move_zeros(int nums[], int length) {
int count = 0;
for (int i = 0; i < length; i++) {
if (nums[i] != 0) {
nums[count] = nums[i];
count++;
}
}
while (count < length) {
nums[count] = 0;
count++;
}
}
```
该函数遍历数组,将所有非0的元素移到数组前面,并记录0的个数count。然后将数组后面的count个元素置为0。
示例:
```c
int nums[] = {0, 1, 0, 3, 12};
move_zeros(nums, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", nums[i]);
}
// 输出:1 3 12 0 0
```
Java实现:
```java
public int[] moveZeros(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[count] = nums[i];
count++;
}
}
while (count < nums.length) {
nums[count] = 0;
count++;
}
return nums;
}
```
该函数遍历数组,将所有非0的元素移到数组前面,并记录0的个数count。然后将数组后面的count个元素置为0。
示例:
```java
int[] nums = {0, 1, 0, 3, 12};
moveZeros(nums);
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
// 输出:1 3 12 0 0
```
用C语言写一个求int数组末尾0的个数
以下是用C语言写一个求int数组末尾0的个数的代码:
```c
#include <stdio.h>
int count_zeros(int arr[], int n) {
int count = 0;
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == 0) {
count++;
} else {
break;
}
}
return count;
}
int main() {
int arr[] = {1, 2, 0, 0, 0, 3, 4, 0, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int zeros = count_zeros(arr, n);
printf("末尾0的个数为:%d\n", zeros);
return 0;
}
```
这个程序会输出:末尾0的个数为:3
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)