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. Mac升级后如何查看自己的网络端口

    OS X 10.9 下面 网络实用工具 从实用工具目录里消失了,可能这个程序用的人太少就取消了吧.但是对于做互联网的人还是有点用的. 参考http://www.mamicode.com/info-de ...

  2. PHP SDK+Oss 上传文件流

    // Endpoint以杭州为例,其它Region请按实际情况填写. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 云 ...

  3. E. Pavel and Triangles dp+问题转化

    E. Pavel and Triangles dp+问题转化 题意 给出n种线段,每种线段给出一定数量,其中每个线段都是 \(2^k\) 问最多能组成多少个三角形 思路 因为每个是\(2^k\)所以能 ...

  4. doGet与doPost简单理解

    get和post是http协议的两种方法 这两种方法有着本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字符串.Post的参数是通过另外的流传递,不通过url,所以可以很大 ...

  5. python调用c/c++ (入参出参为指针)

    python可以使用ctypes库调用c++编译的so库函数 0x01  c/c++编译为so库文件 编译C文件 gcc -o libpycallfoo.so -shared -fPIC rsa.c  ...

  6. 主席树板子 p2104

    #include<cstdio> #include<algorithm> #include<vector> using namespace std; ; int n ...

  7. 503,display:none;与visibility:hidden;的区别

    联系:他们都能让元素不可见 区别: display:none:会让元素从渲染树中消失,渲染的时候不占据任何空间: visibility:hidden:不会让元素从渲染树中消失,渲染时袁旭继续占据空间, ...

  8. scrapy下载 大文件处理

    # 一个校花网图片下载的案例,也适合大文件处理,多个文件视频,音频处理 工程流程 -- scrapy startproject xx cd xx scrapy genspider hh www.xx. ...

  9. HttpClient与TestNG结合

    1.HTTPclient插件的安装 在maven项目的pom.xml中引用HTTPclient包,如下 <dependencies> <dependency> <grou ...

  10. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...