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 ...
随机推荐
- win10/server2019 系统安装 详解
https://www.microsoft.com/zh-cn/software-download/windows10 https://go.microsoft.com/fwlink/?LinkId= ...
- 使用cython把python编译so
1.需求 为了保证线上代码安全和效率,使用python编写代码,pyc可直接反编译,于是把重要代码编译so文件 2.工作 2.1 安装相关库: pip install cython yum insta ...
- Python之函数目录(自定义函数,内置函数,装饰器,迭代器,生成器)
1.初始函数 2.函数嵌套及作用域 3.装饰器 4.迭代器和生成器 6.内置函数 7.递归函数 8.匿名函数 9.函数相关定义 10.三元表达式.列表推导式.生成器表达式 11.函数与方法的区别
- js笔试题一套(未完待续)
1.下面程序的运行结果是: function test(x, y, z) { alert(test.length); alert(arguments.length); alert(arguments. ...
- 部署和调优 1.6 vsftp部署和优化-2
映射个虚拟用户 创建个用户,不让他登录 useradd virftp -s /sbin/nologin 创建存放虚拟用户用户和密码的文件 vim /etc/vsftpd/vsftpd_login 写入 ...
- js和jQuery判断数组是否包含指定元素
最近遇见一些前台基础性问题,在这里笔者觉得有必要记录一下,为了以后自己查阅或者读者查看. 已知var arr = ['java','js','php','C++']; 问题:arr数组是否包含‘jav ...
- 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN" 之间的排序规则冲
在两个数据库之间进行复合查询时有时会出现如下错误: 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN&qu ...
- loader的意义和内部机制浅析
意义: loader可以异步的加载数据到我们的activity或者fragment上面,让加载数据的时候ui线程不阻塞. 而且当数据发生变化的时候,还可以及时更新 具体用法参考 http://deve ...
- 6.Model类
Basic Concepts 在Model/View结构中,Model提供标准的接口让View和Delegate获得数据.在QT中,标准的接口都被定义在QAbstractItemModel类 ...
- Python程序设计8——网络编程
Python是一个很强大的网络编程工具,python内有很多针对场景网络协议的库,在库顶部可以获得抽象层,这样就可以集中精力在程序的逻辑处理上,而不是停留在网络实现的细节中. 1 少数几个网络设计模块 ...