leetcode--Remove Element
来源:互联网 发布:大连pm2.5历年数据 编辑:程序博客网 时间:2024/06/11 01:47
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.
算法:
一:通过首尾双指针进行判断和交换,类似于快排。
二:访问一个元素,判断是不是elem,决定是不是要向前移动。
算法二实现如下:
java:public class Solution { public int removeElement(int[] A, int elem) { int n = A.length; if(n==0) return 0; int cnt=0; for(int i=0;i<n;i++){ if(A[i]==elem){ cnt++; }else{ A[i-cnt]=A[i]; } } return n-cnt; }}c++:
class Solution {public: int removeElement(int A[], int n, int elem) { if(n==0) return 0; int cnt=0; for(int i=0;i<n;i++){ if(A[i]==elem){ cnt++; }else{ A[i-cnt]=A[i]; } } return n-cnt; }};
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
- iOS6 自动布局 入门–Auto Layout(转)
- log4j 日志管理
- Android SDK Android NDK Android Studio 官方下载地址
- iText操作PDF之学习笔记
- Uva1347
- leetcode--Remove Element
- 如何对修改后的java源代码进行重新打包
- log4cplus:一个按天为单位、按不同类型归类的配置文件
- 如何修改(更换)SVN账户
- c++模板类
- 树(图)的建立、初始化和遍历
- OpenCV学习笔记(三):将图像显示在MFC控件上并做直方图均衡化处理
- 为什么0.1无法被二进制小数精确表示?
- flex渲韵效果