[LeetCode27]Remove Element
来源:互联网 发布:数据集中存储的好处 编辑:程序博客网 时间:2024/05/29 11:22
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.
Analysis:
two pointers scan, one variable record the frequency of the value, detailed see in code
Java
public int removeElement(int[] A, int elem) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int count = 0; for(int i=0;i<A.length;i++){ if(A[i] == elem) count++; else if(count>0) A[i-count] = A[i]; }return A.length - count; }
Another one:
public int removeElement(int[] A, int elem) { int count = 0; for(int i=0;i<A.length;i++){ if(A[i]!=elem){ A[count++]=A[i]; }else{ continue; } } return count; }
c++
int removeElement(int A[], int n, int elem) { int cur =0; for(int i=0;i<n;i++){ if(A[i]==elem) continue; A[cur] = A[i]; cur++; } return cur; }
0 0
- [LeetCode27]Remove Element
- LeetCode27:Remove Element
- leetcode27:Remove Element
- leetcode27: Remove Element
- Leetcode27. Remove Element
- LeetCode27. Remove Element
- leetcode27:Remove Element
- leetcode27~Remove Element
- 【LeetCode27】【Remove Element】
- LeetCode27. Remove Element
- leetcode27. Remove Element
- leetcode27. Remove Element
- LeetCode27 Remove Element
- leetcode27. Remove Element
- LeetCode27. Remove Element
- leetcode27. Remove Element
- LeetCode27. Remove Element
- LeetCode27 Remove Element
- Boost.Asio C++ Network Programming(Chapter 2)
- Google 服务器 IP 地址列表
- FPGA 机器学习之BP神经网络1
- 使用初始化成员列表初始化数据与在构造函数中初始化数据
- C语言中void和NULL
- [LeetCode27]Remove Element
- 黑马程序员_学习笔记9——字符串
- UNIX环境高级编程(三)文件IO
- STM32中assert_param的使用
- windows核心编程(一) -----windows消息分流器
- 梳理springmvc
- String.Substring 方法 (Int32, Int32)
- 程序员加班到深夜,你经历过没?
- VMware虚拟机下Linux平台搭建samba服务器的基本配置