Bloomberg 面试题05012012[1]

来源:互联网 发布:mysql密码字典 编辑:程序博客网 时间:2024/06/05 14:12

Given an unsorted integer array, place all zeros to the end of the array without changing the sequence of non-zero elements. (i.e. [1,3,0,8,12, 0, 4, 0,7] --> [1,3,8,12,4,7,0,0,0])


#include <iostream>int main(){int a[9] = {1, 3, 0, 8, 12, 0, 4, 0, 7};int first = 0, cur = 0;for(; cur < 9; cur++){if(a[cur] != 0){a[first] = a[cur];first++;}}for(; first < 9; first++){a[first] = 0;}for(int i = 0; i < 9; i++){printf("%d ", a[i]);}printf("\n");return 0;}


原创粉丝点击