27. Remove Element
来源:互联网 发布:mac 查看所有进程 编辑:程序博客网 时间:2024/05/23 10:20
原题
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.
题目分析
要求移除指定元素,空间复杂度为O(1)。
代码实现
public int RemoveElement(int[] nums, int val) { int i=0; //指向不等于元素val for(int j=0; j<nums.Length;j++) { while(j<nums.Length&&nums[j]==val) j++; if(i<j && j<nums.Length) nums[i]= nums[j]; if(j<nums.Length) i++; } return i; }
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp
2 0
- [LeetCode]27.Remove Element
- LeetCode --- 27. Remove Element
- LeetCode 27.Remove Element
- [Leetcode] 27. Remove Element
- 27.Remove Element
- [Leetcode] 27.Remove Element
- 27. Remove Element
- 27. Remove Element
- 【leetcode】27. Remove Element
- 【leetcode】27. Remove Element
- 27. Remove Element
- 27. Remove Element
- 27. Remove Element
- Leetcode 27. Remove Element
- 27. Remove Element
- LeetCode 27. Remove Element
- 27. Remove Element LeetCode
- 27. Remove Element
- 数据结构与算法分析 c++11 题2.27 找K是否在N*N 矩阵中
- C语言--从指针到二级指针(下)
- LIS小程序小bug
- 我眼中的csdn
- JS原型与原型链终极详解
- 27. Remove Element
- Django 数据模型字段及属性
- 分类算法3----线性判别分析(LDA)原理和推导过程
- COM库中用于创建组件的三个函数
- Android简单本地视频播放器
- 面向过程和面向对象的理解
- CSS3 2D转换
- MySQL的备份
- AvatarView头像