leetcode:Remove Element 【Java】
2016-03-06 11:28
459 查看
一、问题描述
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.
二、问题分析
添加一个下标指针即可。
三、算法代码
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.
二、问题分析
添加一个下标指针即可。
三、算法代码
public class Solution { public int removeElement(int[] nums, int val) { int index = -1; for(int i = 0; i <= nums.length - 1; i++){ if(nums[i] != val){ nums[++index] = nums[i]; } } return index + 1; } }
相关文章推荐
- Java学习第一周博客
- Spark FrameWork源码研究
- Spring JMS
- 有关java.util.ConcurrentModificationException
- 调整数组顺序使奇数位于偶数前面Java
- leetcode:Container With Most Water 【Java】
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
- Java 回顾笔记_包
- 用Eclipse编写Java时调用algs4.jar和stdlib.jar解决StdOut和StdIn无法被识别的问题
- Java学习笔记
- 20145324 《Java程序设计》第1周学习总结
- Java的native方法
- java计划任务调度框架quartz结合spring实现调度的配置实例代码分享
- struts2 中redirectAction如何传递参数!
- 简单的springmvc的maven项目创建
- Java设计模式--观察者模式
- 20145214 《Java程序设计》第1周学习总结
- 第20课 :SPARK Top N彻底解秘 TOPN 排序(Scala)SPARK分组TOPN 算法(JAVA) 必须掌握!
- Java复习笔记(四)——Java基本类型的类型转换
- HDOJ——2002计算两点间的距离(java版)