【leetcode】Remove Element (easy)
2015-03-31 16:23
309 查看
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.
思路:
s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换。
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
思路:
s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换。
int removeElement(int A[], int n, int elem) { int s = 0, e = n - 1; while(s <= e) { if(A[e] == elem) //末尾待删 直接删除 { e--; continue; } if(A[s] == elem) { A[s++] = A[e--]; } else { s++; } } return e + 1; }
相关文章推荐
- leetcode_27_ Remove Element (easy)
- [Leetcode 27, easy] Remove Element
- 【Leetcode-easy】Remove Element
- LeetCode刷题(C++)——Remove Element(Easy)
- leetcode 027 Remove Element(难易度:Easy)
- 【Leetcode】【Easy】Remove Element
- LeetCode-Easy刷题(8) Remove Element
- LeetCode-27-Remove Element(数组)-Easy
- leetcode 026 Remove Duplicates from Sorted Array(难易度:Easy)
- [Leetcode 20, Easy] Valid Parentheses
- 【leetcode】Remove Element
- leetcode 520. Detect Capital(easy)
- 【leetcode】length of last word (easy)
- [Leetcode 169, Easy] Majority Element
- 【leetcode】Valid Sudoku (easy)
- [LeetCode-27] Remove Element(从数组删除指定的元素)
- LeetCode: 继续easy题2
- leetcode 506. Relative Ranks(easy)
- 【Leetcode】【Easy】Symmetric Tree
- leetcode解题方案--027--Remove Element