27. Remove Element(Java)
来源:互联网 发布:织梦cms邀请码 编辑:程序博客网 时间:2024/05/02 03:00
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The order of elements can be changed. It doesn't matter what you leave beyond the new length.Example:Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.
本题比较简单,就是首先遍历该数组,如果数组元素的遍历时遇到与给定值相同的值,则将该元素删除,也就是用j+1的值覆盖j的值,注意当删除该元素之后,记得将遍历数组的指针向前移动一个(因为此时该指针指向的值还没有遍历,所以要将指针向前移动一个,即就是代码中的i--)
public class Solution { public int removeElement(int[] nums, int val){if(nums == null)return 0;int len = nums.length;for(int i = 0; i < len; i++){if(nums[i] == val){for(int j = i; j < len - 1; j++){nums[j] = nums[j + 1];}len--;i--;//注意将指针向前移动}}return len;}}
0 0
- 27. Remove Element(Java)
- 27. Remove Element(java)
- Leetcode:27. Remove Element(JAVA)
- leetcode-java-27. Remove Element
- (Java)LeetCode-27. Remove Element
- [LeetCode-Java]27. Remove Element
- [LeetCode] 27. Remove Element java
- [leetcode]27.Remove Element(Java)
- 27. Remove Element(C++/Java)
- LeetCode-27. Remove Element(Java)
- Leetcode 27. Remove Element (Easy) (java)
- 27. Remove Element —— Java
- [Leetcode] Remove Element (Java)
- Remove Element Java
- Remove Element (Java)
- [LeetCode][Java] Remove Element
- 【leetcode】Remove Element【java】
- (java)Remove Element
- 51nod 1185 威佐夫游戏 V2
- 421. Maximum XOR of Two Numbers in an Array
- Lost connection to MySQL server at 'waiting for initial communication packet', system error: 60 "
- tomcat启动startup.bat一闪而过
- MongoDB常用操作命令有哪些?
- 27. Remove Element(Java)
- PropertyPlaceholderConfigurer类使用
- 表白专用
- Android使用SurfaceView代替AnimationDrawable播放多图帧动画,避免OOM和卡顿
- getchar()的灵活使用
- 同步示例
- 上课编程练习六
- NOR与NAND
- Android多线程ListView加载并缓冲图片