Move Zeroes——Leetcode
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.
题目大意:给定一个数组,把所有的0放到数组后面,所有的非0元素放到前面,并且保证非0元素顺序不变。
解题思路,从前往后遍历,遇见0则cnt++,遇见非0则往前移cnt个,最后把后cnt个设置为0就可以了。
public void moveZeroes(int[] nums) {
if(nums==null||nums.length==0){
return;
}
int cnt = 0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
cnt++;
}else{
nums[i-cnt]=nums[i];
}
}
if(cnt>0){
Arrays.fill(nums,nums.length-cnt,nums.length,0);
}
}
Move Zeroes——Leetcode的更多相关文章
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
- 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: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 ...
- [LintCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
随机推荐
- SSO单点登录之数据同步
完成单点登录,我们便可以通过只登录一个账号,来访问不同系统,然而会遇到以下问题. 1. 通过A站登录的用户访问B站,在B站并没有真实用户,此时如果在B站新建用户,则可能造成用户信息不全,或者数据冲突等 ...
- 如何在ASP.NET 项目中使用Silverlight页面
闲来无事,想写个网站玩玩,比较懒,不想写太多的样式来美化,看中了Silverlight,样式布局比较省事,但是又不想全部都用Silverlight 来写,所以才有此一文. 其实Silverlight最 ...
- SET QUOTED_IDENTIFIER OFF语句的作用
先看下面几个sql语句 1 SET QUOTED_IDENTIFIER ON 2 SELECT * FROM "USER" WHERE a='netasp' 3 4 S ...
- jsp中<c:if>与<s:if>的区别
<c:if>时jstl标签,一般与el表达式一起使用,参考http://www.360doc.com/content/11/1121/16/7874148_166229306.shtml ...
- 一行代码实现headView弹簧拉伸效果
前言 很多app的个人中心上部的headView都实现了弹簧拉伸的效果,即tableView的top并不随着下拉而滑动,而是紧紧的停在屏幕的最上方. 我们今天就分析一下这个效果的实现方式. 分析 关键 ...
- 吐槽:Lambda表达式
前面我曾经讨论过Lambda表达式(也就是匿名表达式)的用法, 这里我就主要强调一下匿名表达式的好处. 首先是不需要写多余的方法体,特别是订阅事件的时候,但是也有一个问题,那就是单个方法会因为匿名表达 ...
- Data Abstraction
What is an object? (Page 238) In C++, an object is just a variable, and the purest definition is &qu ...
- github避免每次输入账户密码
方法1: 显示所有隐藏目录,找到目录./git下的文件config文件,通过文本方式打开,在最前面添加如下两行.之后再次输入一次密码后就会记住账号密码. [credential] helper ...
- awk 多分隔符
#!/bin/bash log_path="./log/" dates=`date -d '-1 days' +'%Y%m%d'` cd $log_path; for i in ` ...
- Python资源汇集
Python资源汇集 一 实用教程 廖雪峰网站 第一,Python教程:提供了循序渐进,重点是可操作的实用教程. 第二,Web App 项目教程.给出一个用16天完成的Python Web APP项目 ...