LeetCode 283 -Move Zeroes ( JAVA )
2016-04-06 15:57
447 查看
Given an array
to the end of it while maintaining the relative order of the non-zero elements.
For example, given
be
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
总结:传递的是引用所以对nums进行修改即可盖面nums的值,题目为,判断不是等于0就给第一个元素赋值,以此类推。
nums, write a function to move all
0's
to the end of it while maintaining the relative order of the non-zero elements.
For example, given
nums = [0, 1, 0, 3, 12], after calling your function,
numsshould
be
[1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
public class Solution { public void moveZeroes(int[] nums) { int j=0; for(int i=0;i<nums.length;i++){ if(nums[i]!=0){ nums[j++] = nums[i]; } } while(j<nums.length){ nums[j++] = 0; } } }
总结:传递的是引用所以对nums进行修改即可盖面nums的值,题目为,判断不是等于0就给第一个元素赋值,以此类推。
相关文章推荐
- 1005. 继续(3n+1)猜想 (25)
- 如何在Eclipse下查看JDK源代码
- java从字符串中截取数字
- 1004. 成绩排名 (20)
- Android开发-eclipse+phonegap(Cordova)环境搭建
- 1003. 我要通过!(20)
- springMVC+freemarker中Could not resolve view with name... 问题解决
- 关于spring+springMVC+myBatis的一些基础配置以及整合
- Java中的浮点型(Double&Float)计算问题
- java中TreeSet的Comparator比较器的三种使用方法
- 【Java线程】锁机制:synchronized、Lock、Condition
- String cannot be resolved to a type
- Java enum的用法详解
- Spring框架自动装配 Bean(三)
- Java的MyBatis框架中实现多表连接查询和查询结果分页
- JAVA多线程实现的三种方式
- Java模拟登录正方教务抓取成绩、课表、空教室
- Spring
- java 判断一个数是否为“开心数”
- Sublime Text3 设置Java开发环境,Javatar