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 ...
随机推荐
- 剑指Offer——回溯算法解迷宫问题(java版)
剑指Offer--回溯算法解迷宫问题(java版) 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路. 下面我们来详细讲一 ...
- FFmpeg源代码简单分析:makefile
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- ROS(indigo)ABB机器人MoveIt例子
ROS(indigo)ABB机器人例子 参考网址: 1 http://wiki.ros.org/Industrial 2 http://wiki.ros.org/abb 3 https://gi ...
- 安卓中的消息循环机制Handler及Looper详解
我们知道安卓中的UI线程不是线程安全的,我们不能在UI线程中进行耗时操作,通常我们的做法是开启一个子线程在子线程中处理耗时操作,但是安卓规定不允许在子线程中进行UI的更新操作,通常我们会通过Handl ...
- Java正则表达式小记
http://blog.csdn.net/pipisorry/article/details/51059500 正则表达式的一般规则都一样,见[python正则表达式] java正则表达式中的特殊字符 ...
- 03 ImageView 图片
四 ImageView 父类 : view >概念:展示图片的控件 >属性: <!-- android:adjustViewBounds=&qu ...
- ROS_Kinetic_17 使用V-Rep3.3.1(vrep_ros_bridge)
ROS_Kinetic_17 使用V-Rep3.3.1(vrep_ros_bridge) https://github.com/lagadic/vrep_ros_bridge v-rep也有官网也有教 ...
- 深入Java关键字instanceof
深入Java关键字instanceof instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例. 举个例子: public interface ...
- Bias and Variance 偏置和方差
偏置和方差 参考资料:http://scott.fortmann-roe.com/docs/BiasVariance.html http://www.cnblogs.com/kemaswill/ Bi ...
- NSAttributedString富文本简单介绍和常用方法浅析
NSAttributedString基本知识点介绍 1.初始化方法 - (instancetype)initWithString:(NSString *)str; - (instancetype)in ...