题目描述

已知数组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. Python unittest excel数据驱动 写入

    之前写过一篇关于获取excel数据进行迭代的方法,今天补充上写入的方法.由于我用的是Python3,不兼容xlutils,所以无法使用copy excel的方式来写入.这里使用xlwt3创建excel ...

  2. vuex的实用知识点

    本文系统介绍vuex的全部内容 为什么用vuex 组件通信知道吧,相信很多同学,刚学的时候很难懂,实时上在实际应用中,大型项目如果使用最原始的组件通信会非常的麻烦,主要体现在多层嵌套的组件之间的通信, ...

  3. Oracle的REGEXP_REPLACE函数简单用法

    转载:http://blog.csdn.net/itmyhome1990/article/details/50380718

  4. leetcode590

    树的后序遍历. class Solution { public: vector<Node> Tree; void postTree(Node node) { for (auto n : n ...

  5. 字符串解压缩类库(zip、GZIP、QuickLz、snappy、lzf、jzlib)介绍

    1.ZIP. GZIP  计算机文件压缩算法,JDK中java.util.zip.*中实现.主要包括ZipInputStream/ ZipOutputStream.GZipInputStream/Zi ...

  6. 封装 Toast

    一. ToastView.java 1 import android.content.Context; import android.view.LayoutInflater; import andro ...

  7. CheckBoxJS选中与反选得到Value

    function XuanZe(val) {    datastr = $("#hid_AID").val();    var newstr = "";    ...

  8. Vue02 样式的动态绑定

    daigengxin......2018-3-8 21:09:18 跟angular2类似,分为CSS类绑定和Style样式绑定两种方式,详情参见

  9. SQL server2008无法收缩日志

    SQL server2008无法收缩日志,错误信息为: 1:由于最小日志空间要求,无法收缩日志文件 2:无法收缩日志文件 2 (XXX_log),因为该文件结尾的逻辑日志文件正在使用 描述: 用的是网 ...

  10. resize函数有五种插值算法

    转自http://blog.csdn.net/fengbingchun/article/details/17335477 最新版OpenCV2.4.7中,cv::resize函数有五种插值算法:最近邻 ...