当前位置: 首页 > news >正文

leetcode-27. Remove Element

题目阐释:
根据告知的元素,从列表中删除,并计算剩余元素的个数
重点:
通过移动一个列表的元素,记录index位置,将一个列表内的所有元素分类。
计算剩余元素的个数,也可以看成先分类,再统计。
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.

class Solution:
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        start,end=0,len(nums)-1
        while start<=end:
            if nums[start]==val:
                nums[start],nums[end]=nums[end],nums[start]
                end-=1
            else:
                start+=1
        return start

相关文章:

  • Spark进阶之路-Spark提交Jar包执行
  • 算法与数据结构1800题 数组和广义表(一)
  • JavaScript 函数式编程(二)
  • volatile 可见性的模拟分析示例
  • 管理微服务中的数据
  • bzoj 2002 弹飞绵羊 lct裸题
  • 【c学习-3】
  • BZOJ 4589 Hard Nim
  • MongoDB学习笔记Day3
  • teamview被限制使用的解决办法
  • 随机数据构造-Faker
  • mybaits出现错误
  • 如何恢复u盘误删文件,看完就不会觉得自己很菜了
  • ubuntu 查看apt-get有哪些软件
  • 用Visual Studio开发以太坊智能合约
  • 《剑指offer》分解让复杂问题更简单
  • Angular6错误 Service: No provider for Renderer2
  • Go 语言编译器的 //go: 详解
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • Java 最常见的 200+ 面试题:面试必备
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 从零开始在ubuntu上搭建node开发环境
  • 高程读书笔记 第六章 面向对象程序设计
  • 关于 Cirru Editor 存储格式
  • 规范化安全开发 KOA 手脚架
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 每天10道Java面试题,跟我走,offer有!
  • 在Mac OS X上安装 Ruby运行环境
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​iOS实时查看App运行日志
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (31)对象的克隆
  • (Ruby)Ubuntu12.04安装Rails环境
  • (ZT)薛涌:谈贫说富
  • (八)Flask之app.route装饰器函数的参数
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (差分)胡桃爱原石
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (力扣)循环队列的实现与详解(C语言)
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (转)创业的注意事项
  • (转)使用VMware vSphere标准交换机设置网络连接
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .Net - 类的介绍
  • .Net mvc总结
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .NET程序员迈向卓越的必由之路
  • @RequestBody的使用
  • [《百万宝贝》观后]To be or not to be?
  • [ASP]青辰网络考试管理系统NES X3.5