题目概述:

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

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

解题思路:

说实话,这道题目的描述很是不清楚,以至于我觉得很多人会觉得过oj的话只需要统计有多少个不是elem了(至少我开始这么想的)。

于是我开始写了这么一段:

class Solution2:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
l = len(A)
for i in A:
if i == elem:
l -= 1
return l

然后得到了这么一个错误:

Input:	[4,5], 4
Output: [4]
Expected: [5]

纳闷了挺久,后来各种纠结才发现这个oj不光检查了返回值,连A也要一起检查,而且是检查A的前若干个是不是是满足提议的。证据如下:

AC代码:

class Solution:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
s = 0
for i in A:
if i != elem:
A[s] = i
s += 1
return s

WA代码:

class Solution:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
s = 0
l = len(A)
for i in A:
if i != elem:
A[l-s-1] = i
s += 1
return s

两份代码唯一不同的地方就是前面一个我是把满足提议的存在前面后面一个我是存在后面了。

【leetcode】Remove Element的更多相关文章

  1. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  2. 【Leetcode】【Easy】Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  3. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  4. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  6. 【leetcode】Remove Nth Node From End of List

    题目简述: Given a linked list, remove the nth node from the end of list and return its head. For example ...

  7. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

  8. 【leetcode】 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  9. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

随机推荐

  1. 红米3 TWRP-3.0.2(android_6.0.1_r72分支)中文版Recovery更新于20161018

    TWRP3.0.2更新简介 TWRP是TeamWin团队https://github.com/TeamWin/Team-Win-Recovery-Project的开源项目,也是Omnirom系统默认的 ...

  2. 整块div设置为超链接进行界面跳转

    鼠标点击当前整块DIV任意一个地方均可进行页面跳转,如果复制过去的代码不能用,请注意双引号和单引号,是否为英文状态下的输入法填写出来的. 1.跳转至新建页面 <div class="& ...

  3. JavaScript 中对内存的一些了解

    在使用JavaScript进行开发的过程中,了解JavaScript内存机制有助于开发人员能够清晰的认识到自己写的代码在执行的过程中发生过什么,也能够提高项目的代码质量.其实关于内存的文章也有很多,写 ...

  4. python3随记——字符编码

    1.1什么是字节 字节(Byte)是计算机信息技术用于计量存储容量的一种计量单位,也表示一些计算机编程语言中的数据类型和语言字符. 比特(bit)在计算机中最小的单位,在二进制位的电脑的系统中,每一b ...

  5. Python Day6

    面向对象 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强...&qu ...

  6. GIFT-EMS礼记----青软S2SH(笔记)

    这个S2SH的项目,是这本书的一个贯穿项目,所以这里要记录一下, 看这个项目有两个目的: 1.借助这个项目,学习一下S2SH的综合配置及使用 2.借助这个项目练习一下如何做需求分析和项目架构设计. P ...

  7. redis和memcached的区别(总结)

    1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...

  8. 大熊君大话NodeJS之------MongoDB模块(额外篇)

    一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为 ...

  9. cpu对各种运算的速度对比

    今天忽然想到cpu对各种基本运算的速度对比,我对比了异或,加法和乘法,结果发现速度时间,加法小于乘法小于异或, 原本我以为异或会是最快的,结果异或是最慢的,这跟cpu中的alu算术逻辑单元的实现有关, ...

  10. PHP中spl_autoload_register()函数的用法

    spl_autoload_register (PHP 5 >= 5.1.2) spl_autoload_register — 注册__autoload()函数 说明 bool spl_autol ...