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 ...
随机推荐
- svn使用技巧一:更新、提交、资源库同步之间区别
提交:是用本地文件覆盖服务器的文件,只有提交会导致服务器上发生变化 更新:只是把服务器上最新版本下载到客户端,规则如下: 1.如果你本地的某个文件没有修改过,而服务器上的这个文件别人已经提交过新版本, ...
- 手机定位室内gps没信号
手机定位一般分3种,gps,手机信号基站,上网地点,其中gps信号一般只有户外有,所以在室外的时候只开启定位和gps就可以定位了,但是在室内没有gps的情况,就需要开网络定位了.
- 类型:Oracle;问题:oracle 查询表详细信息;结果:oracle查询表信息(索引,外键,列等)
oracle查询表信息(索引,外键,列等) oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助: 1.查询出所有的用户表sel ...
- linux命令-df查看磁盘命令
格式 df -h 人性化变换数据单位 -k 数据以k为单位 -m 数据以m为单位 -i 查看indoe使用情况 free(查看swap)
- PopupWindow-----点击弹出 PopupWindow 初始化菜单
/** * 点击弹出 PopupWindow 初始化菜单 */ private void initPopupWindow() { PopupWindowAdapter adapter = new Po ...
- 关于android中,菜单按钮点击事件首次执行之后再次执行需要双击按钮的问题
有时候在获取事件的时候,需要双击才能获取,解决方法很简单,把返回值设为true,那么这个事件就不会再分发了,我预计是设为其他值会继续分发,造成事件的相应混乱
- 关于mybatis和spring复合pom的异常
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...
- 修改Win10默认窗口背景色为护眼色的方法
按Windows键+R,打开“运行”对话框,输入regedit: 修改[HKEY_CURRENT_USER\Control Panel\Colors]下Windows键值为 199 237 204, ...
- Luogu 4867 Gty的二逼妹子序列
BZOJ3809,是权限题. 我永远喜欢莫队. 先莫队一下移下左右指针,然后用一个数据结构维护一下区间$[a, b]$中的颜色的值,跟着指针移动一起修改修改,每一次$query$的时候就相当于查询一下 ...
- python3-列表中存储字典
# Auther: Aaron Fan #示例1:#定义几个字典alien_0 = {"color":"green", "points":5 ...