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 ...
随机推荐
- rpmgraph - 显示 RPM 软件包依赖关系图
SYNOPSIS rpmgraph PACKAGE_FILE ... DESCRIPTION rpmgraph 使用 PACKAGE_FILE 参数来产生一个软件包依赖关系图.每个 PACKAGE_F ...
- windows切换窗口和网页快捷键
alt+tab 切换窗口win+D 显示桌面,再按一下返回运来的网页win+M 所有程序最小化 网页之间切换(我用的是360) ctrl + tab 往回切 ctrl + shift +tab
- django报错
报错: SyntaxError Generator expression must be parenthesized 问题原因: 由于django 1.11版本和python3.7版本不兼容, 2.0 ...
- Java两个引用指向同一个数组
- source insight 4.0.86.0安装破解问题
source insight 4.0.86.0安装过程中碰到导入lic文件一直不正确 解决办法: 需要将SourceInsight\SW_Install\SI4安装及破解文件 目录下的sourcein ...
- Linux 进程通信之:内存共享(Shared Memory)(转,好文章)
https://blog.csdn.net/afei__/article/details/84188548
- Java中File类重修
IO流 概述 io流:输入输出流(input/output).流是一组有顺序的,有起点和终点的字节集合,是对各种数据传输的总称或抽象.即数据在两设备之间的传输称为流.流的本质是数据传输. InputS ...
- Java集合框架Map接口
集合框架Map接口 Map接口: 键值对存储一组对象 key不能重复(唯一),value可以重复 常用具体实现类:HashMap.LinkedHashMap.TreeMap.Hashtable Has ...
- shiro 安全框架 详解
---恢复内容开始--- Shiro 简介 简介• Apache Shiro 是 Java 的一个安全(权限)框架.• Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE 环境, ...
- mui is not defined
vue项目中引用mui.js,我是在main.js中这样引入的, 结果报错 查找资料,最后在mui.js的最后添加了这样一句 这是因为mui并不能像jquery那样作为全局对象存在,加上wi ...