Leetcode27--->remove element (to Remove the given element in the divisor Group)

Source: Internet
Author: User

title: Given an array of arrays and a value, remove all elements equal to the value in the array, return the length of the new array, request: cannot allocate extra array space, and must use the idea of in-place ordering, spatial complexity o (1);

Example:

Given input array nums = [3,2,2,3] , val =3

Your function should return length = 2, with the first of the elements of nums being 2.

Problem solving Ideas:

1. First find the first position equal to value and the first number not equal to value;

2. The number equal to value and not equal to value is all value;

3. Each exchange must be the first equal to value and the first number that is not equal to value for exchange;

To Illustrate:

3,3,3,2,2,3,1,2,4,3,4,3,2; Value = 3

1) initial: start = 0; index = 3; The number of exchange two locations, the result becomes: 2,3,3,3,2,3,1,2,4,3,4,3,2;

2) start = 1; index = 4; exchange:2,2, 3,3,3,3,1,2,4,3,4,3,2;

3) start = 2; index = 5; because nums[index] = 3, so index is incremented until nums[index]! = 3, at which point index = 6; exchange:2,2, 1,3,3,3,3,2,4,3,4,3,2;

4) start = 3; index = 7;exchange:2,2, 1,2,3,3,3,3,4,3,4,3,2;

5) start = 4; index = 8; exchange:2,2, 1,2,4,3,3,3,3,3,4,3,2;

6) start = 5; index = 9; nums[index] = 3, index is incremented until index = 10:exchange:2,2, 1,2,4,4,3,3,3,3,3,3,2;

7) start = 6; index = 11; nums[index] = 3, index is incremented until index = 12:exchange:2,2, 1,2,4,4,2,3,3,3,3,3,3;

At this point start = 7;index = 12 equals the length of the array, ending;

The code is as Follows:

1  public classSolution {2      public intRemoveelement (int[] nums,intVal) {3         if(nums = =NULL|| Nums.length < 1){4                 return0;5         }6         intCount = 0;7         intindex = 0;8          while(index < nums.length && nums[index]! = Val) {index + +; count + +;}//find the first number equal to value9         intStart =index;Ten          while(index < nums.length && nums[index] = = Val) {index + +;}//find the first number that is not equal to value one          while(start < nums.length && Index <Nums.length) { a Exchange (nums, start, index); -Count + +; -Start + +; the              while(index < nums.length && nums[index] = = Val) {index + +;} -  -         } -         returncount; +     } -       public Static voidExchangeint[] nums,intindex1,intindex2) +      { a         inttemp =nums[index1]; atnums[index1] =nums[index2]; -nums[index2] =temp; -      } -}

Leetcode27--->remove element (to Remove the given element in the divisor Group)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.