leetcode之旅(7)-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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
思路:
说实话。刚看到有点头大,后面尝试写,思路是遍历每一个元素,如果是0,就把后面的元素往前移动一个,然后把零移到后面。
注意:在循环的时候,不要急着对循环变量加1,先判断移动后,是不是0.如果是零,接着移动,不加1.还有每次移动,都把后面就加一个 零,设置一个变量count技术,后面循环n = n - count
代码:
public class Solution {
public void moveZeroes(int[] nums) {
int n = nums.length;
//用来后面减去移动次数的计数
int num = n;
int i = 0;
while(i < num){
if(nums[i] == 0){
int last = nums[i];
for(int a = i;a < n-1;a++){
nums[a] = nums[a+1];
}
nums[n-1] = last;
}
if(nums[i] != 0){
//如果不是零,才往后加1
i = i+1;
}else{
//减去移动次数,与因为后面都是零
num = num-1;
}
}
}
}
leetcode之旅(7)-Move Zeroes的更多相关文章
- 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&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- LeetCode(283)Move Zeroes
题目 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- LeetCode Array Easy 283. Move Zeroes
Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- 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
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 ...
随机推荐
- Hibernate之多对多表,操作实例
多表操作之多对多关系简介 思路就是: 在数据库底层通过添加中间表来指定关联关系. 在双方的实体中添加一个保存对方的集合 在双方的配置文件中使用set标签和many-to-many标签来进行关联关系的配 ...
- scala学习笔记5 (隐式转化/参数/类)
隐式转化: 隐式参数: 隐式类:
- UNIX网络编程——使用select函数编写客户端和服务器
首先看原先<UNIX网络编程--并发服务器(TCP)>的代码,服务器代码serv.c: #include<stdio.h> #include<sys/types.h> ...
- 海量数据挖掘MMDS week6: MapReduce算法(进阶)
http://blog.csdn.net/pipisorry/article/details/49445519 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- javascript之BOM事件注册和案例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java Serializable 生成随机序列
如果你implements 了 Serializable接口 但是没写 UID,eclipse会在你的类名边上有一个警告,你点击一下,有一个选项自动生成 UID,所以请用eclipse写java代码
- 【Android 应用开发】Android 平台 HTTP网速测试 案例 API 分析
作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速测试标准 : 除普通网页测速 ...
- Leetcode_168_Excel Sheet Column Title
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42554641 Given a positive integ ...
- 使用GDAL将下载的Google卫星图像转为带坐标的tif
网上有很多下载Google地图的卫片的软件,一般下载下来的图像都是jpg格式的,另外附带一个坐标信息的描述文件.这样的数据不能直接拿来在遥感或者GIS软件中使用,因为图像里面没有投影和坐标信息,所以就 ...
- Boosting 和梯度Boosting
Boosting方法: Boosting这其实思想相当的简单,大概是,对一份数据,建立M个模型(比如分类),一般这种模型比较简单,称为弱分类器(weak learner)每次分类都将上一次分错的数据权 ...