随笔- 509  文章- 0  评论- 151  阅读- 22万 

Remove Element

2013.12.7 04:33

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.

Solution1:

  My first solution is scan the array from both ends, if a target value is found on the left side, then get a non-target value from the right side to fill this position. When left and right meets somewhere in the middle, the scan is done.

  Time complexity is O(n),  space complexity is O(1).

Accepted code:

复制代码
 1 class Solution {
 2 public:
 3     int removeElement(int A[], int n, int elem) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int i, j, k;
 7         int len;
 8         
 9         if(A == nullptr){
10             return 0;
11         }
12         
13         len = n;
14         i = 0;
15         j = n - 1;
16         while(i <= j){ // BUG: j > i, fixed here
17             if(A[i] == elem){
18                 while(j >= i){ // BUG: j > i, fixed here
19                     if(A[j] == elem){
20                         --j;
21                         --len;
22                     }else{
23                         A[i] = A[j];
24                         ++i;
25                         --j;
26                         --len;
27                         break;
28                     }
29                 }
30             }else{
31                 ++i;
32             }
33         }
34         
35         return len;
36     }
37 };
复制代码

Solution2:

  It was mentioned in the problem that "the order can be changed", but there seemed to be no extra benefit from changing the order. So let's do this without changing the order.

  Scan the array from left to right, when ever a target value is found, record the accumulated number of appearance of the target, and use this as an offset.Moveothernontargetvaluebackbyoffset positions to make a new array.

  Time complexity is O(n), space complexity is O(1).

Accepted code:

复制代码
 1 class Solution {
 2 public:
 3     int removeElement(int A[], int n, int elem) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int i, j;
 7         int len;
 8         
 9         if(A == nullptr){
10             return 0;
11         }
12         
13         len = n;
14         i = 0;
15         j = 0;
16         while(i < n){
17             if(A[i] == elem){
18                 --len;
19                 ++i;
20             }else{
21                 A[j++] = A[i++];
22             }
23         }
24         
25         return len;
26     }
27 };
复制代码

 

 posted on   zhuli19901106  阅读(275)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示