【leetcode】Remove Element
题目概述:
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的更多相关文章
- 【leetcode】Remove Element (easy)
		Given an array and a value, remove all instances of that value in place and return the new length. T ... 
- 【Leetcode】【Easy】Remove Element
		Given an array and a value, remove all instances of that value in place and return the new length. T ... 
- 【leetcode】Remove Duplicates from Sorted Array
		题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ... 
- 【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 ... 
- 【leetcode】Remove Duplicates from Sorted List
		题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ... 
- 【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 ... 
- 【leetcode】Majority Element
		题目概述: Given an array of size n, find the majority element. The majority element is the element that ... 
- 【leetcode】 Remove Duplicates from Sorted List
		Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ... 
- 【leetcode】Remove Duplicates from Sorted Array II
		Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ... 
随机推荐
- js获取域名
			<script language="javascript">//获取域名host = window.location.host;host2=document.domai ... 
- 【BZOJ-1419】Red is good     概率期望DP
			1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ... 
- VS2015链接错误一则
			以前天真的以为C是C++的子集,.c文件直接.cpp命名没什么影响: 后缀一改 链接器工具错误 LNK2019 
- Alpha阶段第十次Scrum Meeting
			情况简述 Alpha阶段第十次Scrum Meeting 敏捷开发起始时间 2016/11/3 00:00 敏捷开发终止时间 2016/11/4 00:00 会议基本内容摘要 详细定义了API接口,汇 ... 
- HTTP Content-type 对照表
			Application Type 文件扩展名 Content-Type(Mime-Type) 描述 . application/x- .* application/octet-stream 二进制 ... 
- %我的 tex 模版
			%我的 tex 模版 \documentclass[UTF8,a1paper,landscape]{ctexart}%UTF8 中文支持,a1paper 纸张大小,landscape 横向版面,cte ... 
- 基于canvas的陈列订货的分析
			订货会软件中又新增了进行陈列订货,即一杆衣服订的显示出来,没订的不显示出来 主要遇到的问题是如何呈现,原先老是想着定位,left,top但是花出来的图容易出现原先的数据填写错误导致后期的图片的呈现出现 ... 
- struts2 s:file标签使用及文件上传例子
			<s:form action="uploadaction" method="post" enctype="multipart/form-da ... 
- commons-fileupload.jar实现文件上传
			标签: uploadfileimportexceptionstringmyeclipse 2012-09-06 19:55 1497人阅读 评论(0) 收藏 举报 分类: 好东东(2) Jav ... 
- PHP读写文件
			一:读取文件 例1: $xml = ""; //打开文件 $f = fopen('http://app.eyuebus.com/Public/apk/version.xml', ' ... 
