"Leetcode Problem Solving report" array element delete

Source: Internet
Author: User

Leetcode Remove Element

Problem description

Given an array and a value, remove all instances of that value in place and return the new length. Do the allocate extra space for another array, and you must does this on place with constant memory. The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.

For 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.

Analysis and Solution

Note: The order of elements can be changed, it is possible to find a position equal to Val from scratch, a position that is not equal to Val at the beginning of the tail, and an interchange with a double pointer, until the head and tail pointers cross. The reference code looks like this:

1 classSolution2 {3  Public:4     intRemoveelement (vector<int>& Nums,intval)5     {6         intLen = Nums.size (), i =0, j = Len-1; 7         if(J <0)return 0; 8          while(I <j)9         {Ten              while(I < len && nums[i]! = val) i++;  One              while(J >=0&& Nums[j] = = val) j--;  A             if(I < J) Swap (nums[i++], nums[j--]);  -         } -         return(Nums[j] = = val)? j:j+1;  the     } -};
View Code leetcode Remove duplicates from Sorted Array

Problem description

Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength. Do the allocate extra space for another array, and you must does this on place with constant memory.

For example, given input array nums = We [1,1,2]. Y function should return length = 2 , with the first Elements of nums being and 1 2 respectively. It doesn ' t matter what are you leave beyond the new length.

Analysis and Solution

Go to the weight, the reference code is as follows:

1 classSolution2 {3  Public:4     intRemoveDuplicates (vector<int>&nums)5     {6         intRET =0, length =nums.size ();7         if(Length <2)returnlength;8          for(inti =1; i < length; i++)9         {Ten             if(Nums[i]! = Nums[ret]) Nums[++ret] =Nums[i]; One         } A         returnret+1;  -     } -};
View Code leetcode Remove duplicates from Sorted Array II

Problem description

follow up for "Remove duplicates": What if duplicates is allowed at the most twice?

For example, Given sorted array nums = [1,1,1,2,2,3].  Your function should return length =5, with the first five elements ofNumsbeing1122 and3. It doesn ' t matter what are you leave beyond the new length.

Analysis and Solution

Go heavy, but note that the same value in the result can occur two times, the last problem extension, in fact, can be extended to the same value occurs k times. The reference code is as follows:

1 classSolution2 {3  Public:4     intRemoveDuplicates (vector<int>&nums)5     {6         intLen = Nums.size (), i =1, j =2; 7         if(Len <3)returnLen;8          while(J <len)9         {Ten             if(Nums[j]! = nums[i-1]) Nums[++i] =Nums[j]; OneJ + +;  A         } -         returni +1;  -     } the};
View Code leetcode 283 Move Zeros

Problem description

  Given an array nums , write a function to move all's to the 0 end of it while maintaining the relative order of the No N-zero elements.

For example, given nums = [0, 1, 0, 3, 12] , after calling your function, nums should is [1, 3, 12, 0, 0] .

Note:

1. You must does this in-place without making a copy of the array.

2. Minimize the total number of operations.

Analysis and Solution

Note that elements other than 0 are kept in the original order, still double pointers, but this is all starting from the beginning, I point to the first 0,j to the first non-0, the interchange, until the end of the array is J. The reference code looks like this:

1 classSolution2 {3  Public:4     voidMovezeroes (vector<int>&nums)5     {6         intLen = Nums.size (), i =0, j =0; 7          while(I <len)8         {9             if(Nums[i]! =0) Swap (nums[j++], nums[i]); Teni++;  One         } A     } -};
View Code

"Leetcode Problem Solving report" array element delete

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.