码迷,mamicode.com
首页 > 移动开发 > 详细

LeetCode 283. 移动零

时间:2019-09-26 00:07:31      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:个数   blank   div   i++   题目   mss   etc   get   https   

题目链接:https://leetcode-cn.com/problems/move-zeroes/

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

思路:把非零元素前移最后补零就行了

 1 void moveZeroes(int* nums, int numsSize){
 2     int t=0;
 3     for(int i=0;i<numsSize;i++){
 4         if(nums[i]!=0){
 5             nums[t++]=nums[i];
 6         }
 7     }
 8     for(int i=t;i<numsSize;i++){
 9         nums[i]=0;
10     }
11 }

 

LeetCode 283. 移动零

标签:个数   blank   div   i++   题目   mss   etc   get   https   

原文地址:https://www.cnblogs.com/shixinzei/p/11588290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!