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         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         n--;
 7         for(int i=0; i<=n; i++){
 8             if(A[i] == elem){
 9                 while(n>i && A[n] == elem){
10                     n--;
11                 }
12                 A[i] = A[n];
13                 n--;
14             }
15         }
16         return n+1;
17     }
18 };

Run Status: Accepted!
Program Runtime: 20 milli secs

Progress: 112/112 test cases passed.

相关文章:

  • 2021-10-19
  • 2021-11-15
  • 2021-08-28
  • 2021-09-04
  • 2022-12-23
  • 2022-02-11
  • 2021-12-09
  • 2022-01-16
猜你喜欢
  • 2022-01-12
  • 2022-02-12
  • 2021-06-08
  • 2022-12-23
  • 2021-11-06
  • 2022-02-09
  • 2022-01-09
相关资源
相似解决方案