Move Zeroes
2015-11-16 20:26
288 查看
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.
void moveZeroes(int* nums, int numsSize) { for(int i=0;i<numsSize;i++){ if(nums[i]==0){ int j=i; while(nums[j]==0){ //找到nums[i]之后第一个非零元素 j=j+1; if(j==numsSize) //j=numsSize,说明nums[i]后面全是零,返回 return; } int temp=nums[i]; //交换零与非零元素 nums[i]=nums[j]; nums[j]=temp; } } }
相关文章推荐
- 调整数组顺序使奇数位于偶数前面
- Cocos2D:塔防游戏制作之旅(一)
- Cocos2D:塔防游戏制作之旅(一)
- Unity Android 加密 so
- NSOrderedSame NSOrderedAscending NSOrderedDescending字符串枚举比较
- 来自苹果、谷歌、微软等知名公司六道有趣智力面试题
- GPU架构基础 之 关于CUDA中线程访存的关系
- 开发中端口被占用的问题
- Struts2中的ModelDriven机制及其运用
- 基于mini2440 的 bootloader 设计 ---- 7、进入C大门
- XML基础操作(1)
- JQuery实现“还能输入多少个字”
- 控件显示不出来 - 分析
- Hibernate关联映射
- Unity3D笔记2
- 数据结构实验题目
- linux内核空间与用户空间通信
- 链路聚合之LACP
- win7系统常用命令-win7运行命令大全
- HDU 2050:折线分割平面【数学】