27. Remove Element [easy]
来源:互联网 发布:淘宝买万艾可怎么购买 编辑:程序博客网 时间:2024/05/01 15:41
题目
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 = 3
Your function should return length = 2, with the first two elements of nums being 2.
Java
public class Solution { public int removeElement(int[] A, int elem) { int num = 0, l=A.length; for(int i=0;i<l-num;i++){ while(A[l-num-1]==elem){ num++; if(l-num-1<i){return l-num;} } if(A[i]==elem) { A[i] = A[l-num-1]; num++; } } return l-num; }}
Python
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ L = len(nums)-1; cur=len(nums)-1; while cur >=0: if nums[cur]==val: nums[cur] = nums[L] L-=1 cur-=1 return L+1
0 0
- 27. Remove Element [easy]
- 27. Remove Element (Easy)
- 27. Remove Element [easy] (Python)
- leetcode 27. Remove Element (EASY)
- 27. Remove Element 难度:easy
- Easy-题目28:27.Remove Element
- Leetcode 27. Remove Element (Easy) (cpp)
- Leetcode 27. Remove Element (Easy) (java)
- 【LeetCode】27.Remove Element(Easy)解题报告
- Remove Element(easy)
- [Leetcode 27, easy] Remove Element
- <LeetCode><Easy> 28 Remove Element
- Easy 27题 Remove Element
- Leet Code OJ 27. Remove Element [Difficulty: Easy]
- [LeetCode] 027. Remove Element (Easy) (C++)
- 【LeetCode】2(7)Remove Element(Easy)
- LeetCode-27-Remove Element(数组)-Easy
- Easy 8 Remove Element(27)
- numpy数据类型
- 霍夫曼编码
- Android中Glide(加载图片)的使用
- mysql自身运行日志文件详解
- Git学习笔记整理
- 27. Remove Element [easy]
- Opencv3.0-python的那些事儿:(一)、Opencv的图像和视频处理基本用法
- Reverse String
- c#的委托(3)之多重委托
- TextView 中 android:maxEms="" ,maxLine="",maxlenght="";的区别
- gitlab环境配置
- 关于eclipse项目转到android studio的经验
- malloc和new的十一个区别
- 监控Linux性能的18个命令行工具