【Leetcode】Remove Element
来源:互联网 发布:ubuntu终端退出全屏 编辑:程序博客网 时间:2024/05/01 05:20
题目:
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.
解题思路:设置两个指针pa,pb,pa指针从0开始依次向n-1滑动,当pa指向的元素不是待删除的元素时,将pa指向的元素赋给pb指向的元素,同时pb向前滑动。
代码:
class Solution {public: int removeElement(int A[], int n, int elem) { int pa,pb=0; for(pa=0;pa<n;pa++){ if(A[pa]!=elem){ A[pb++]=A[pa]; } } return pb; }};
0 0
- LeetCode:Remove Element
- LeetCode: Remove Element
- [Leetcode] Remove Element
- LeetCode: Remove Element
- leetcode 39: Remove Element
- [LeetCode] Remove Element
- Leetcode: Remove Element
- Leetcode:Remove Element
- Leetcode:Remove Element
- Leetcode:Remove Element
- [LeetCode]Remove Element
- LeetCode-Remove Element
- LeetCode - Remove Element
- LeetCode | Remove Element
- leetcode之Remove Element
- LeetCode - Remove Element
- LeetCode: Remove Element
- 【LeetCode】Remove Element
- A. Vanya and Cards
- Andriod 打包 签名,发布到Google play
- Git学习笔记1 神奇的git stash
- Android系统性能调优工具介绍
- 体育在线:林书豪表现仍是很犀利
- 【Leetcode】Remove Element
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(31)-MVC使用RDL报表
- ids & hdmi 原理
- outlook Express 6邮件的文件大小超过2G时的办法
- [Codeforces] Round #235 (Div. 2)
- Linux下 Apache 不能解析php 和不能解析 mht shtml 文件格式问题汇总
- SQL自动并且异地备份重要的数据库文件
- 黑马程序员----泛型
- 在asp.net mvc中使用json