Remove Element
来源:互联网 发布:酷狗音乐无法使用网络 编辑:程序博客网 时间:2024/04/29 17:41
题目描述
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.
解题思路
将提供的元素去掉,并返回剩余数组的长度。
注意问题
1 注意不是只要求返回剩余数组的长度,而且必须是将元素都移动到数组的长度以内。
2 注意去除了多少个元素,就将元素前移几位。
代码示例
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.
解题思路
将提供的元素去掉,并返回剩余数组的长度。
注意问题
1 注意不是只要求返回剩余数组的长度,而且必须是将元素都移动到数组的长度以内。
2 注意去除了多少个元素,就将元素前移几位。
代码示例
package leetcode;import java.util.Arrays;public class RemoveElement {public int removeElement(int[] A, int elem) {int sum = 0;for(int i = 0 ; i < A.length ; i++){if(A[i] == elem) sum++;else A[i-sum] = A[i];} return A.length-sum; }public static void main(String[] args) {int[] A = {1, 2, 1, 3, 4};int elem = 2;RemoveElement re = new RemoveElement();System.out.println(re.removeElement(A, elem));}}
0 0
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- CSS的声明方式,单位,注释方式
- Perl 正则表达式语法
- CSS选择器的常用规则
- Merge Two Sorted Lists
- android-横竖屏切换总结-布局改变和数据保存
- Remove Element
- 熊绎:我看软件工程师的职业规划
- Balanced Binary Tree
- Remove Duplicates from Sorted Array
- Symmetric Tree
- iOS 通知,代理,单例,block传值
- Merge Sorted Array
- Plus One
- Pascal's Triangle