题目概述:

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. Sokcet方式请求HTTP/HTTPS的封装类HttpHelper

    using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...

  2. 上传本地代码到github

    第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令git init第二步:将项目的所有文件添加到仓库中git add .如果想添加某个特定的文件,只需把.换成特定的文件名即可第三步:将 ...

  3. bzoj 4318 OSU!

    期望dp. 考虑问题的简化版:一个数列有n个数,每位有pi的概率为1,否则为0.求以每一位结尾的全为1的后缀长度的期望. 递推就好了. l1[i]=(l1[i-1]+1)*p[i]+0*(1-p[i] ...

  4. Servlet实现asp.net中的Global.asax启动事件(Servlet和Listener使用)

    1.Java Web中没有像asp.net的全局启动事件,但是可以通过web.xml中的load-on-startup节点来控制Servlet的开机启动和启动次数.web.xml详细配置参考:http ...

  5. git版本控制?

    git是一个分布式的版本控制系统,版本控制系统,类似于保险箱,而我们的代码就是资产:通过对代码的有效管理可以更好的提高我们的生产效率:maven是主要是一个项目构建工具,解决的是我们个人在开发过程中的 ...

  6. [HAOI2009]求回文串

    神奇到爆炸的贪心,策略很简单.但是实现上好像比较恶心.换了一种思路.先保存所有点应该转移到的位置,BIT搞个逆序对就好了. 如何找到每个点应该转移到的位置?这个处理方式也是比较玄学.看代码吧. //O ...

  7. CSS复习

    CSS 选择器 p.into  表示带有into类的p元素 伪类: a)      first-line b)      last-line 伪元素: :before  能在指定的元素前添加内容(创造 ...

  8. Java开发面试总结

    Java开发面试总结.. ----------------------- java 基础知识点这一块: 1.面向对象的三大特征.(继承,封装,多态) 1.1 在什么样的场合下面会使用到继承 1.2 什 ...

  9. 【11-23】mysql学习笔记02

    SQL的历史 SQL是Structed Query Language 的缩写,即”结构化查询语言” SQL原名是 sequel,后来由于法律原因,改名 SQL最早可以追溯到 1974 年,源于 IBM ...

  10. 惊艳!9个不可思议的 HTML5 Canvas 应用试验

    HTML5 <canvas> 元素给网页中的视觉展示带来了革命性的变化.Canvas 能够实现各种让人惊叹的视觉效果和高效的动画,在这以前是需要 Flash 支持或者 JavaScript ...