Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)
题目描述
已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变。比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0]
提醒:
- 你必须就地进行操作,不能生成数组的复制
- 使操作次数最少
测试样例
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 移动数组中的零 (数组,模拟)的更多相关文章
- 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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 283. Move Zeroes ☆(移动0到最后)
描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- 第九章 Java中线程池
Java中的线程池是运用场景最多的并发框架,几乎所有需求异步或并发执行任务的程序都可以使用线程池.在开发过程中,合理地使用线程池能够带来3个好处. 降低资源消耗:通过重复利用已创建的线程降低线程创建和 ...
- IIS安装与部署,站点的部署与配置
第一章:IIS安装与部署 一,服务器概念的理解: 将设计好的软件只要部署到一台机器(服务器--->IIS)上,其它的员工通过浏览器(网址.)来进行访问. 做好的网站必须部署到这台机器上的IIS中 ...
- 如何通过outline为SQL语句指定执行计划
创建测试表 以用户jyu连接,创建测试表 SQL> conn jyu/jyu; Connected. SQL> create table t (id number, name varcha ...
- javascript——对象的概念——创建对象与销毁对象
一.创建对象 1.创建空对象 方式一: var o ={};o; //Object {} typeof(o); //"object" 方式二: var o=new Object() ...
- Synchronized关键字、Lock,并解释它们之间的区别
Synchronized 与Lock都是可重入锁,同一个线程再次进入同步代码的时候.可以使用自己已经获取到的锁. Synchronized是悲观锁机制,独占锁.而Locks.ReentrantLock ...
- Android中SQLite查询date类型字段出现有返回但是为错误值的情况
出现该情况的原因是因为查询精度与数据库中存储精度不相同造成的,例如,查询精度为 YYYY-MM-DD 但是存储精度为 YYYY-MM-DD HH:MM:SS,就会出现该错误. 更改查询精度为YYYY- ...
- cocos2d-js 热更新模块 使用AssetsManager
原帖子地址:http://cn.cocos2d-x.org/tutorial/show?id=1186 在这个文章中原作者已经说的很清楚,我在这个其他改动一些适用我项目中需求 1.满足Web和Nati ...
- MVC分层思想、SSM编程架构
1 MVC 和 SSM
- css 层叠式样式表(3)
样式分类 大小 -- 调整div大小,长 width,高 height.长可以直接100%横向沾满屏幕,高不可以. 背景 background-color 背景色 background-image ...
- Luogu 4323 [JSOI2016]独特的树叶
新技能get 树哈希,考虑到两棵树相同的条件,把每一个结点的哈希值和树的siz写进哈希值里去. 做出A树每一个结点为根时的树的哈希值丢进set中,然后暴力枚举B树中度数为1的点,求出删掉这个点之后的哈 ...