题目描述

已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变。比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0]

提醒:

  1. 你必须就地进行操作,不能生成数组的复制
  2. 使操作次数最少

测试样例

Input:num=[0,1,0,3,12]
Output:num=[1,3,12,0,0] Input:nums=[1,0,3,0,5,7]
Output:nums=[1,3,5,7,0,0]

详细分析

从左至右遍历数组,当发现零元素后,找到它后面第一个非零元素,两者交换即可 执行效果如下:

nums=[0,1,0,12]

nums=[1,0,0,12]

nums=[1,12,0,0]

代码实现

  • java实现
class Solution {
public void moveZeroes(int[] nums) {
List<Integer> nozero = new ArrayList<>(); for(int i=0;i<nums.length;i++){
if(nums[i]!=0){
nozero.add(nums[i]);
}
} int cnt = 0;
for(int x:nozero){
nums[cnt++] = x;
}
for(int s=cnt;s<nums.length;s++){
nums[s]=0;
}
} }
  • c实现
void moveZeroes(int* nums, int numsSize) {
for(int i=;i<numsSize;i++){
if(nums[i]!=){
int cnt = ;
while(cnt<i&&nums[cnt]!=){
cnt++;
}
if(cnt<i){
nums[cnt] = nums[i];
nums[i] = ;
}
}
}
}

Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)的更多相关文章

  1. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  2. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  3. Java [Leetcode 283]Move Zeroes

    题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  4. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  5. [leetcode]283. Move Zeroes移零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  6. [LeetCode] 283. Move Zeroes ☆(移动0到最后)

    描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...

  7. [LeetCode] 283. Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  8. Leetcode 283 Move Zeroes python

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  9. 11. leetcode 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

随机推荐

  1. SQL基础(3)

    SQL FULL JOIN (1)SQL FULL JOIN关键字 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. (2)语法 SELECT column_name(s) FROM t ...

  2. CreateRemoteThread 远程注入

    在release中可以成功,在debug中被注入的程序停止工作 #pragma once#include "stdafx.h"#include <windows.h># ...

  3. numpy.percentile

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.percentile.html numpy.percentile(a, q, axi ...

  4. JAVA之数组队列

    package xxj.datastructure0810; import java.util.Random; public class DataStructure { /** * @param ar ...

  5. 地图投影的N种姿势(转载)

    转载地址:http://blog.sina.com.cn/s/blog_517eed9f0102w4rm.html 一篇题为<我们看到的地图一直都错得离谱……>的文章在朋友圈里莫名流行起来 ...

  6. 每天一道算法题(14)——N个降序数组,找到最大的K个数

     题目: 假定有20个有序数组,每个数组有500个数字,降序排列,数字类型32位uint数值,现在需要取出这10000个数字中最大的500个. 思路 (1).建立大顶堆,维度为数组的个数,这里为20( ...

  7. Contentprovider的创建和使用流程概述

    首先在provider类中继承并实现provider的几个必要方法 -- boolean onCreate(),用来执行一些初始化的工作. -- cursor query(Uri, String[], ...

  8. 托盘在XP下不能显示tooltip,在Vista和Windows7下正常

    转自:http://blog.csdn.net/debehe/article/details/4294053 奇怪的问题,想了很多可能的理由,最终的答案竟然是一开始就被我否认了的一种可能!! 问题现象 ...

  9. ES6中变量的解析赋值的用途

    变量的解构赋值用途很多. (1)交换变量的值 let x = 1; let y = 2; [x, y] = [y, x]; 上面代码交换变量x和y的值,这样的写法不仅简洁,而且易读,语义非常清晰. ( ...

  10. SpringBoot05 数据操作02 -> JPA接口详解

    概览 JpaRepository 继承 PagingAndSortingRepository 继承 CrudRepository 继承 Repository 1 Repository 这是一个空接口, ...