leetcode:Remove Element删除元素
2014-11-18 13:51
429 查看
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.
删除数组中与给定的元素相等的元素,并返回数组新的长度,不限定你是否改变数组的排序
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
删除数组中与给定的元素相等的元素,并返回数组新的长度,不限定你是否改变数组的排序
class Solution { public: int removeElement(int A[], int n, int elem) { /* int last = n-1; int length = n; for (int i = 0; i <= last; i++) { if(A[i]==elem) { while (A[last]==elem && i<=last) { --length; --last; } if(i<last) { A[i] = A[last]; --last; --length; } } } return length; */ for (int i = 0; i <n ; i++) { if (A[i]==elem) { while (A[n-1]==elem && i<=n-1) --n; if(i<n-1) { A[i] = A[n-1]; --n; } } } return n; } };
相关文章推荐
- [C++]LeetCode 27: Remove Element(删除数组中指定元素)
- LeetCode 27. Remove Element(删除元素)
- leetCode 27.Remove Element (删除元素) 解题思路和方法
- leetcode:Remove Element (删除给定元素)【面试算法题】
- LeetCode:Remove Element(删除数组中的特定元素)
- leetCode 27.Remove Element (删除元素) 解题思路和方法
- LeetCode Remove Element 快速删除数组中的某一值
- Remove Element 删除元素
- lintcode 容易题:Remove Element 删除元素
- LeetCode-Remove Duplicates from Sorted Array II (删除重复元素,使重复次数最多N次)
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
- leetcode 82-- Remove Duplicates from Sorted List II 链表 双指针 删除元素
- 【LeetCode-面试算法经典-Java实现】【203-Remove Linked List Elements(删除单链表中的元素)】
- LeetCode | Remove Duplicates from Sorted Array(删除有序数组的重复元素)
- Remove Element 数组中删除给定元素 @ LeetCode
- [leetcode] Remove Nth Node From End of List 删除链表开始的第n个元素
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- [LeetCode]Merge Two Sorted Lists & Remove Duplicates from Sorted List 两个有序链表合并为一个&删除链表重复元素
- Leetcode Remove Linked List Elements 删除链表中的元素
- LeetCodet题解--19. Remove Nth Node From End of List(删除链表的倒数第n个元素)