LeetCode——Move Zeroes
Description:
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.
时空复杂度都在O(n)内
public class Solution {
public void moveZeroes(int[] nums) { //nums = [0, 1, 0, 3, 12]->[1, 3, 12, 0, 0]
for(int i=0,j=0; i<nums.length; i++) {
if(nums[i]!=0) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
j ++;
}
} }
}
LeetCode——Move Zeroes的更多相关文章
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
- [LeetCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode Move Zeroes (简单题)
题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- leetcode之旅(7)-Move Zeroes
Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...
- 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】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
- 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 ...
随机推荐
- Win2008 Server系统安装打印服务器与配置
原文:http://60808.org/thread-20259-1-1.html 视频地址:http://edu.51cto.com/lesson/id-20163.html 本文介绍的是在Win2 ...
- eclipse lua
eclipse中的ldt插件是Lua Development Tools,开发lua专用的插件: 1.点击help->install new softWare,输入http://luaeclip ...
- ubuntu配置apache和cgi
ubuntu配置apache和cgi . 更新源并进行安装,否则后面的下载可能会不成功. sudo apt-get update sudo apt-get upgrade . 安装apache2服务 ...
- am335x uart分析
/************************************************************ * am335x uart分析 * 本文记录am335x uart驱动的注册 ...
- i2c驱动笔记
基于bcm5300x芯片 注册平台总线设备,设备名bcm5300x_i2c,通过名称与驱动进行匹配. 注册平台总线驱动.驱动名称"bcm5300x_i2c",与设备进行匹配. dr ...
- Eclipse 调试总进入Spring代理的解决办法
一直都是跳入代理类中,手动切换查看内容,还以为别人也是这样,结果被告知不是.瞬间囧囧. 搜了一番,看起来有两个办法. 第一个:使用step filter,过滤掉不需要的package.--未测试 第二 ...
- 标准的 C++ 由三个重要部分组成
标准的 C++ 由三个重要部分组成: 核心语言,提供了所有构件块,包括变量.数据类型和常量,等等.C++ 标准库,提供了大量的函数,用于操作文件.字符串等.标准模板库(STL),提供了大量的方法,用于 ...
- jquery 中json数组的操作(转)
在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...
- console.log()注意事项。
console.log常因不明原因在IE9出现SCRIPT5009: 'console' is undefined (console未被定义) 错误! IE9说console变量未定义? 但F12打开 ...
- Session超时问题(AOP 过滤器)
public class TimeoutAttribute : ActionFilterAttribute { public override void OnActionExecuting(Actio ...