LeetCode Array Easy 283. Move Zeroes
Given an array
nums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.Example:
Input: [,,,,]
Output: [,,,,]Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
问题描述:给一个数组,将数组中所有的0移动到数组的尾部,而不改变其他非零元素的相对位置
个人思路:
1.暴力解法。遍历整个数组,当找到0时,开始从0的下一位去找非零元素,找到后交换两个元素。
public void MoveZeroes(int[] nums) {
for(int i = ; i < nums.Length; i++){
if(nums[i]==){
for(int j = i +; j < nums.Length; j++){
if(nums[j] != ){
nums[i]=nums[j];
nums[j]=;
break;
}
}
}
}
}

第二种解法:设定一个指针i=0,遍历数组,如果当前值不为零 则交换 i j 的元素,
public void MoveZeroes(int[] nums) {
if(nums.Length == )
return;
int i =;
for(int j =; j < nums.Length; j++){
if(nums[j] != ){
Swap(nums, i, j);
i++;
}
}
while(i < nums.Length){
nums[i++]=;
}
}
private void Swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j]=temp;
}

LeetCode Array Easy 283. Move Zeroes的更多相关文章
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- [LeetCode&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- 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
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- 283. Move Zeroes@python
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
随机推荐
- 微信小程序(16)-- bindtap,catchtap事件绑定的区别
bindtap,catchtap事件绑定的区别,这里就涉及冒泡事件了.bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡. logs.wxml <view cl ...
- [C++] 所有该类的对象共享静态类成员变量
问:智能指针可以对指针的引用数量进行计数,一个智能指针释放时,别的智能指针怎么知道的? 同一类的对象共享同一变量最简单的方法是静态变量: 不像普通的变量,静态成员变量是被所有类对象共享的,不同的对象可 ...
- 【LeetCode+51nod】股票低买高卖N题
[121]Best Time to Buy and Sell Stock (2018年11月25日重新复习) 给一个数组代表股票每天的价格,只能有一次交易,即一次买入一次卖出,求最大收益. 题解:用一 ...
- 微信小程序swiper组件实现图片宽度自适应
wxml 代码: <!--pages/swipe/swipe.wxml--> <view> <swiper circular="true" indic ...
- 设置flex 为1
父级的宽度: 375 用来每个子元素的宽度:40 设置了 flex:1,每个子元素的宽度为125 125*3= 375, 设置flex=1后子元素会平均的分配父级元素剩下的宽度
- Flutter-RaisedButton
RaisedButton({ Key key, //点击按钮的回调出发事件 @required VoidCallback onPressed, //水波纹高亮变化回调 ValueChanged< ...
- 02.自定义banner、全局配置文件、@Value获取自定义配置、@ConfigurationProperties、profiles配置
自定义banner src/main/resource 下新建 banner.txt,字符复制到banner.txt 中 生成字符网站推荐: http://patorjk.com/software/t ...
- vue自定义抽屉组件
<template> <div class="drawer"> <div :class="maskClass" @click=&q ...
- SQL server 2008 数据库优化常用脚本
--查询某个数据库的连接数 select count(*) from Master.dbo.SysProcesses where dbid=db_id() --前名其他等待类型 * from sys. ...
- 4412 RS485
一.485硬件原理 差分对传输数据的原理 IO数据的传输→差分对 rs232传输的距离在15米以下,RS485传输距离是几十米到1000米以上 为什么485可以传输这么远 差分对的机制可以降低电磁场的 ...