【Leetcode】Remove Element
2015-12-02 15:31
423 查看
题目链接:
题目:
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.
思路:
用count记录要删除元素数目。常用操作要熟记。
算法:
题目:
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.
思路:
用count记录要删除元素数目。常用操作要熟记。
算法:
public int removeElement(int[] nums, int val) { int length = nums.length; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == val) { count++; } else { nums[i - count] = nums[i]; } } length = nums.length - count; return length; }
相关文章推荐
- EClipse 项目导入 Android Studio 导致的乱码问题
- MySQL数据库水平切分的实现原理解析
- oracle中row_number() over()分析函数用法
- MVVM中间接使用事件(命令)
- NGUI的UIScrollview的实现原理
- RAID , LVM , 配置静态IP 浅析
- 计算机视觉的一些测试数据集和源码站点
- ASCII码表及扩展ASCII码表
- ThinkPHP提示请手动生成项目目录
- Scala 控制结构和函数
- JAVA文件上传
- 枚举是值类型而System.Enum却是引用类型的原因
- 原型 和 原型链
- asp.net c# 虾米音乐API
- HDU1712 分组背包 xingxing在努力
- Linux进程基础
- Java之Object构造方法(创建子类对父类构造方法的调用)
- mongodb异常关闭后重启
- 10个提升iOS开发效率的必用工具
- js正则表达test、exec和match的区别