1、题目

27. Remove Element——Easy

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example 1:

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

大致意思是把一个列表里指定的数字去掉,并且不新增存储空间,最后返回去除指定数字后的列表长度;

2、我的解法

还是采用双指针法,大致解法如下:

# -*- coding: utf-8 -*-
# @Time : 2020/2/3 15:38
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 27. Remove Element.py
from typing import List
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
lens = len(nums)
if lens > 0: #判断列表是否为空
j = 0
for i in range(lens):
if nums[i] != val: #若i指定的数字不等于val
temp = nums[j]
nums[j] = nums[i]
nums[i] = temp #i和j指向的数字位置互换
j = j + 1 #j再往后移动
return j
print(Solution().removeElement([3,3,2],3))

leetCode练题——27. Remove Element的更多相关文章

  1. LeetCode记录之27——Remove Element

    这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...

  2. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  3. [LeetCode&Python] Problem 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...

  4. LeetCode Array Easy 27. Remove Element 解题

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  5. 【leetcode❤python】27. Remove Element

    #-*- coding: UTF-8 -*- class Solution(object):    def removeElement(self, nums, val):        "& ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  8. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  9. Leetcode 题目整理-7 Remove Element & Implement strStr()

    27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...

随机推荐

  1. 解决ERROR 1130: Host 'x.x.x.x' is not allowed to connect to this MariaDB server 方法

    问题描述 在使用SQLyog操作Linux上的MariaDB时候,会出现如下错误: 解决方法 改表法 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电 ...

  2. c# DPI SCale

    public class Screen { /// Primary Screen #region Win32 API [DllImport("user32.dll")] stati ...

  3. OpenCV函数 重映射

    重映射是什么意思? 把一个图像中一个位置的像素放置到另一个图片指定位置的过程. 为了完成映射过程, 有必要获得一些插值为非整数像素坐标,因为源图像与目标图像的像素坐标不是一一对应的. 我们通过重映射来 ...

  4. Web API和Web Service

    首先,Web API是由Web Service演变而来,它们两者关系就是所有Web Service都是API,但并非所有API都是Web Service.其次,两者都有利于信息的传输,但Web API ...

  5. hadoop docker ...

    hadoop docker ... 待办 昨天待办 decription decription 今日待办 decription decription decription decription had ...

  6. Unity相机鼠标基本控制

    一.滚轮控制视角缩放 /// <summary> /// 滚轮控制相机视角缩放 /// </summary> public void CameraFOV() { //获取鼠标滚 ...

  7. linux nmon安装

    系统版本红帽7.7: [root@hostuser1 nmon_permon]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core ...

  8. python 变量的赋值【内存地址】

    注意: python所有的数据都是对象,变量只是指向一个对象的地址,一旦将变量的值或者类型改变,变量指向的地址就有可能发生变化 这个特性在使用默认参数的时候一定要注意

  9. AcWing 861. 二分图的最大匹配 匈牙利算法

    #include <cstring> #include <iostream> #include <algorithm> using namespace std; , ...

  10. Swiper 移动端全屏轮播图效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...