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 ...
随机推荐
- contiki rpl
Linking target: udp-client-temperature.stk3700arm-none-eabi-gcc -Xlinker -Map=udp-client-temperature ...
- C#操作word类文件
最近频繁操作Word文档,写了很多word的操作代码及方法,虽然已经有很多关于word的操作类了,自己还是进行了一下整合: 1.通过模板创建新文件 2.在书签处插入值 3.插入表格 4.合并单元格 5 ...
- SecureCRT连接AWS EC2云主机密码登录
申请了亚马逊的EC2,要通过ssh 加密钥的形式登录,特别麻烦,而且感觉ssh登录AWS的云主机后好卡,这里是更改成用户名和密码的形式登录云主机,可以通过SecureCRT直接登录 1.首先通过ssh ...
- Elasticsearch 5
Elasticsearch 5常见问题解决方案 安装运行 1.前置安装java8 jdk-8u112-linux-x64.rpm 下载地址:http://www.oracle.com/tech ...
- C/C++-中的sort排序用法
转载于:http://www.cnblogs.com/luorende/p/6121906.htmlSTL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#inclu ...
- Linux如何通过命令查看日志文件的某几行(中间几行或最后几行)
linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...
- 关于Cocos2d-x项目运行的过程和场景切换步骤
1.AppDelegate.cpp文件里面的applicationDidFinishLaunching函数其实可以看做C语言里面的main函数,是整个游戏运行的入口 这个函数里面的语句 auto sc ...
- Windows App开发之集成设置、帮助、搜索和共享
应用设置和应用帮助 "设置"合约 上一节中我们学习了怎样将应用设置保存到本地.这样的方式是通过在App内加入设置选项,这里另一种方式. 微软将其称为"设置"合约 ...
- 云计算中auto-scaling 最早的来源
什么是弹性?首先,整合计算资源,将计算资源池化,通过虚拟机按需使用计算资源;其次,按量计费,让用户能够根据使用量按月按时甚至按秒来进行付费. 不过,光有了这两条还不够.为什么?我举个例子: 很多做运维 ...
- Ubuntu下,如何解决Unable to locate package
在虚拟机上新装了一个ubuntu 12.10,想在上面装一个Git,却发生了以下错误信息 我觉得原因可能是我换了163的源,没有更新所以找不到这个包. 这时候就要使用 sudo apt-get upd ...