"Leetcode C + +" Remove Element

Source: Internet
Author: User

Remove Element

Given an array and a value, remove allinstances of this value in place and return the new length.

The order of elements can is changed. ITDOESN ' t matter what you leave beyond the new length.

Deletes the given element in the array. There is no specific explanation, then, this element may be distributed sporadically.

In a scan of the idea, or to change the scanning switch. Not erase is because of the efficiency of the problem, the previous blog has said. The topic is also vague hints, with assignment or exchange is good.

By the thought of the previous question, we also come to a side scan array edge exchange ideas.

We call the deleted elements a garbage element.

First, locate the garbage element first. With two pointers, the most recent garbage element from the array-pointer 1 and the most recent reserved element-pointer 2.


Then exchange them.


At this point, pointer 1 points to a reserved element, and pointer 2 points to a garbage element.

Our intention is to use pointer 1 to point to the garbage element closest to the first of the array, and pointer 2 to the most recent reserved element from the first array. So let's go ahead and let the pointer 1 point to the garbage element closest to the top of the array, and pointer 2 to the last reserved element from the top of the array.


I found it soon, so I found it.

At a certain step, the rubbish element becomes more and more.

It doesn't matter, pointer 2 go a few more steps forward.



When the finishing phase is reached, the pointer 2 cannot find the reserved element, and we will judge the end of the pointer 2 to the tail.

The question asks to return the length of the new array, which is actually the distance the pointer 1 is taking.

In this way, we can solve the problem in exchange.

Leetcode acceptedsolutions Runtime Distribution (15-06-07)


Source://There is a note out of the code, said the idea is to start positioning two pointers, followed by two pointer scan, then the code is divided into two parts. The idea of the annotated code is that each loop makes the pointer 2 reposition again, merging the two parts of the code, but the pointer 2 scans more.

int removeelement (vector<int>& nums, int val) {//////////////////////////////////////leetcode27//////////
	int len = Nums.size ();
	if (0 = len) return 0;
		if (1 = len) {if (val = = nums[0]) return 0;
		else return 1; return val = = Nums[0]?
	0:1;
	/////////////////////////////////////////len = 0;
	Positioning is to locate the first picture of the diagram Vector<int>::iterator Iter1 = Nums.begin ();
	Vector<int>::iterator Iter2;
		while (Val!= *iter1 && nums.end ()!= iter1) {iter1++;
	len++;
	if (nums.end () = = Iter1) return len;
	Iter2 = Iter1;
	while (val = = *iter2 && nums.end ()!= iter2) {iter2++;
			}//positioned over while (Nums.end ()!= iter2) {while (Val!= *iter1)//Find the nearest garbage element {iter1++;
		len++;
			while (val = = *iter2)//Find the most recent reserved element {iter2++;
		if (Iter2 = = Nums.end ()) return Len;
		Swap (*iter1, *iter2);//Exchange//showvector (Nums);
		iter1++;
		len++;
	iter2++; return Len;
	/* int len = nums.size ();
	if (0 = len) return 0;
		if (1 = len) {if (val = = nums[0]) return 0;
		else return 1; return val = = Nums[0]?
	0:1;
	len = 0;
	Vector<int>::iterator iter1 = Nums.begin ();
	Vector<int>::iterator Iter2;
			while (Nums.end ()!= iter2) {while (Val!= *iter1 && nums.end ()!= iter1) {iter1++;
		len++;
		if (nums.end () = = Iter1) return len;
		Iter2 = Iter1;
			while (val = = *iter2) {iter2++;
		if (Iter2 = = Nums.end ()) return Len;
		Swap (*iter1, *iter2);
		Showvector (Nums);
		iter1++;
		len++;
	iter2++;
	return Len; */
}


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.