题目概述:

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. BZOJ2818 Gcd

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  2. Java 内存管理

    java 内存管理机制 JAVA 内存管理总结 java 是如何管理内存的 Java 的内存管理就是对象的分配和释放问题.(两部分) 分配 :内存的分配是由程序完成的,程序员需要通过关键字 new 为 ...

  3. /etc/fstab 文件配置项简单介绍

    Untitled .note-content {font-family: 'Helvetica Neue', Arial, 'Hiragino Sans GB', STHeiti, 'Microsof ...

  4. 关于WEB项目的一点想法

    有点失落.迷茫,差点在上班的时候发了火.原因是之前离职的一位同事,在代码里不加注释,而且百般偷懒,致使很多应该的验证没有验证,很多应该考虑到的情况没有考虑.因为是老员工,我相比他来说是新员工.气势上总 ...

  5. html页面输入框input的美化

    input输入框是网页必不可少的组件,可是每个浏览器对于输入框的显示样式各有不同 例如:    上图分别就是谷歌浏览器和IE浏览器自带显示的输入框,样式也不足人意,所以大多都会自己写样式 以下是一个简 ...

  6. Windows操作系统

    Microsoft Windows,是美国微软公司研发的一套操作系统,它问世于1985年,起初仅仅是Microsoft-DOS模拟环境,后续的系统版本由于微软不断的更新升级,不但易用,也慢慢的成为家家 ...

  7. 图解JVM的类加载机制(详细版)

    注:本文为作者整理和原创,如有转载,请注明出处. 上一篇博文,把JAVA中的Class文件格式用图形的方式画了一下,逻辑感觉清晰多了,同时,也为以后查阅的方便. Class文件只是一种静态格式的二进制 ...

  8. ARCGIS9.3安装说明

    1) 安装LMSetup.exe"    其中第一步选择第一项,并使用"37102011.efl9"文件做为lic文件,在使用之前需要将该文件中的主机名改为本机的机器名, ...

  9. .NET 多个程序配置文件合并到主app.config

    .NET 多个程序配置文件合并到主app.config

  10. html5页面打包成App - Android或Iphone安装程序

    下载安装前端开发工具:HBuilder 官网下载:http://www.dcloud.io/ 根据官网说明安装 * 打开登录HBuilder,把做好的H5页面通过添加app项目把H5的文件夹加入进来( ...