leetcode第27题-Remove Element

#include<stdio.h>
#include<stdlib.h>

int removeElement(int A[], int n, int elem)
{
	if(n==0)
		return 0;
	int len=n;
	for(int i=0,pos=0;i<n;i++)
	{
		if(A[i]==elem)
			len--;
		else
			A[pos++]=A[i];
	}
	return len;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int *a=(int *)malloc(sizeof(int)*n);
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		int elem;
		scanf("%d",&elem);
		int newLen=removeElement(a,n,elem);
		printf("%d\n",newLen);
	}
	return 0;
}

时间: 2024-08-28 01:20:54

leetcode第27题-Remove Element的相关文章

LeetCode(27)Remove Element

题目 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. 分析 这是一道很简单的题目,对数组进行一次遍历,删除与给定值相等的关键字,返回新长度即可. AC代码 cla

leetcode第25题--Remove Element

problem: 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. 将一个数组里的和给定元素相同的元素删除,在该数组里操作,返回剩下的个数.注意,数组的前部分要保留

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode(27)题解:Remove Element

https://leetcode.com/problems/remove-element/ 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. 思路: 因为可以改变元

leetcode第26题--Remove Duplicates from Sorted Array

problem: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input ar

leetcode第27题:删除vector数组的元素(array)

题目: 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. 这个比较简单,掌握vector的用法即可. 1 int removeElement(vector<int>

leetcode第27题--Implement strStr()

Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 就是判断haystack中是否有needle,如果包含的话,返回第一次包含的地址.如果没有则返回NULL. 这题官网打出来的是easy,但是要做好绝不简单.我能想到的就是O(m*n)的复杂度了.最经典的是用KMP算法. class Soluti

第18题 Remove Element

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: public class Solution { public int removeEleme

leetcode第19题-Remove Nth Node From End of List

本题比较简单,主要考察了单链表的创建与删除. 但是有一个问题需要着重的考虑,如何快速定位链表的倒数第n个节点.这就需要两个辅助节点,一个节点先走到正数第n个位置,然后两个辅助节点一块往后走,最后后面的节点的位置就是我们需要的倒数第n个节点. #include<stdio.h> #include<stdlib.h> struct ListNode//定义节点 { int value; struct ListNode *next; }; ListNode *removeNthFromE