leetcode:283. Move Zeroes(Java)解答
2024-08-25 22:52:45
转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50493772
题目地址:https://leetcode.com/problems/move-zeroes/
Move Zeroes
Given an array 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, nums should 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.
解法(java):
public class Solution {
public void moveZeroes(int[] nums) {
if (nums != null) {
int length = nums.length;
for (int i = 0, j = 0; i < length; i++) {
if (nums[i] != 0) {
if (i != j) {
nums[j] = nums[i];
nums[i] = 0;
}
j++;
}
}
}
}
}
最新文章
- 设计模式--单例模式Singleton(创建型)
- sqlserver巧用row_number和partition by分组取top数据
- 什么是json
- 如何用linux远程登录windows计算机
- POJ 3074 Sudoku (DLX)
- Git命令详解(一)-个人使用
- 【动态规划】【KMP】HDU 5763 Another Meaning
- java删除数组中的第n个数
- 高性能C++网络库libtnet实现:IOLoop
- Python面向对象3:面向对象的三大特性
- Ubuntu Linux 解决 bash ./ 没有那个文件或目录 的方法
- Java之工具类:判断对象是否为空或null
- eclipse 关闭控制台 自动弹出
- nodejs中aes-128-cbc加密和解密
- Linux Device Tree
- Oracle 数存储——物理结构
- Win10应用《纸书科学计算器》更新啦!
- csharp: Converting chinese character to Unicode
- RPG游戏地牢设计的29个要点
- AngularJS(三):重复HTML元素、数据绑定