Leetcode - Pointers/Array - Remove Element
2014-04-28 18:32
162 查看
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) { if(n==0) return 0; int *p=&A[0]; int *q=&A[n-1]; while(q>=p) { if(*p==elem) { if(*q==elem) { q--; } else { *p=*q; q--; p++; } n--; } else { p++; } } return n; } };
class Solution { public: int removeElement(int A[], int n, int elem) { if(A==NULL) return 0; for(int i=0;i<n;) { if(A[i]==elem) { swap(A[i],A[n-1]); n--; } else i++; } return n; } };
相关文章推荐
- LeetCode-Array-27 Remove Element
- LeetCode[Array]: Remove Element
- [leetcode][two pointers] Remove Element
- [leetcode][two pointers] Remove Duplicates from Sorted Array II
- leetcode 27. [Array]Remove Element
- 5---LeetCode【tag: Array】【Remove Element】|C语言|总结
- leetcode Remove Duplicates from Sorted Array & Remove Element
- leetcode.array--27. Remove Element
- Leetcode:Remove Duplicates from Sorted Array与Remove Element
- Leetcode - Pointers - Remove Duplicates from Sorted Array
- [leetcode][array][two pointers] Merge Sorted Array
- Leetcode - Pointers/Array - Two sum
- [LeetCode] Remove Element、Remove Duplicates from Sorted Array、Remove Duplicates from Sorted Array II
- Two pointers (5) -- Remove Duplicates from Sorted Array I, II, Move Zeroes, Remove Element
- leetcode题解-26. Remove Duplicates from Sorted Array && 27. Remove Element
- LeetCode 117 Populating Next Right Pointers in Each Node II (链表 层次遍历 推荐)
- LeetCode之路——LeetCode_27_Remove Element
- LeetCode-Search in Rotated Sorted Array(在反转数组中的关键字)
- [LeetCode][Java] Search in Rotated Sorted Array II
- [leetcode]Find Minimum in Rotated Sorted Array II