首页 > 其他 > 详细

【leetcode】Remove Element

时间:2015-04-22 13:20:29      阅读:253      评论:0      收藏:0      [点我收藏+]

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

 

 

 1 class Solution{
 2 public:
 3     int removeElement(int a[],int n,int elem)
 4     {
 5         int i;
 6         int cnt;
 7         cnt=0;
 8         for(i=0;i<n;i++)
 9         {
10             if(a[i]==elem)
11             {
12                 cnt++;
13             }else if(cnt>0)
14             {
15                 a[i-cnt]=a[i];
16             }
17 
18         }
19 
20         return n-cnt;
21       }
22 };

 

【leetcode】Remove Element

原文:http://www.cnblogs.com/jawiezhu/p/4446759.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!