【leetcode】283. Move Zeroes
2016-05-17 18:31
393 查看
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.
相当于设了两个指针,然后进行运算
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.
相当于设了两个指针,然后进行运算
/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ var moveZeroes = function(nums) { var ll = nums.length; var loc_zero=-1; for(var i=0;i<ll;i++){ if(loc_zero===-1&&nums[i]===0){ loc_zero=i; } if(nums[i]!==0&&loc_zero!==-1){ nums[loc_zero]=nums[i]; nums[i]=0; loc_zero++; while(nums[loc_zero]!==0){ loc_zero++; } } } };
相关文章推荐
- AssetBundle5.3.4依赖包加载流程
- [转载]河北易县:黑恶势力称“霸”房地产市场 谁为其撑腰?
- 纯数字字符串判定
- CRM
- java中类的各种定义
- Windows下adb安装配置
- 求解LambdaMART的疑惑?
- Android AndroidStudio JSR/RET are not supported with computeFrames option
- Mac配置环境
- 自定义控件(继承系统控件,非自绘)
- 自定义圆角控件
- uinty本地图片的读取和剪裁
- 阿里云持续交付平台CRP | 从代码托管,到一键发布,从持续集成,到持续交付,CRP让交付更加简单、可靠、高效
- opengl fbo 纹理 开发步骤
- 一个矩阵中最大的二维矩阵(元素和最大)
- hibernate中HQL语句
- MongoDB的界面操作工具之rockmongo
- 制作子弹痕迹(到shareMaterial)-Unity
- GBDT(MART) 迭代决策树入门教程 | 简介
- 利用AJAX实现WordPress中的文章列表及评论的分页功能