题目来源:

  https://leetcode.com/problems/remove-element/


题意分析:

  给定一个数组和一个数值val,将数组中数值等于val的数去除。不能申请额外空间,超过新数组长度部分忽略。


题目思路:

  这道题也是很简单的一道题。和上面一题一样,有i,j两个下标变量,如果nums[j] != val,那么nums[i] = nums[j],i,j各+ 1。


代码(python):

  

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

转载请注明出处:http://www.cnblogs.com/chruny/p/4885139.html

[LeetCode]题解(python):027-Remove Element的更多相关文章

  1. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  2. LeetCode 027 Remove Element

    题目要求:Remove Element Given an array and a value, remove all instances of that value in place and retu ...

  3. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  4. Leetcode 题目整理-7 Remove Element & Implement strStr()

    27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...

  5. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  6. 【LeetCode】027. Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  7. Java for LeetCode 027 Remove Element

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

  8. leetcode第25题--Remove Element

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

  9. LeetCode题解:(19) 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, ...

  10. LeetCode记录之27——Remove Element

    这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...

随机推荐

  1. OSCache报错error while trying to flush writer

    Struts2.3+spring3+hibernate3开发现在想在原有基础上使用 oscache提高性能,使用中发现问题例如:使用struts2标签<cache:cache time=&quo ...

  2. Mybatis上路_06-使用Java自动生成[转]

    Mybatis上路_06-使用Java自动生成 11人收藏此文章, 我要收藏发表于1个月前(2013-04-24 23:05) , 已有151次阅读 ,共0个评论 目录:[ - ] 1.编写Gener ...

  3. CodeIgniter框架开发的统计程序源代码开放

    文章来源: PHP开发学习门户 自己初学php时,用CodeIgniter框架开发的后台统计程序源代码 程序部分页面如图: 具体配置及下载源代码:http://bbs.phpthinking.com/ ...

  4. URAL 1036(dp+高精度)

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  5. Visual Studio的ASP.NET修改默认打开浏览器

    1.新建web空应用程序,输入项目名称. 2.右击项目名称,添加“新建项”—一般处理程序 3.右击新建一个HTML文件,任意命名. 4.右击新建的Html文件,选择“浏览方式” 5.选择后弹出浏览器选 ...

  6. BZOJ 2466: [中山市选2009]树( 高斯消元 )

    高斯消元解异或方程组...然后对自由元进行暴搜.树形dp应该也是可以的... ------------------------------------------------------------- ...

  7. ffmpeg 命令详解

    常用视频命令 //转码(并添加黑边) ffmpeg -ss start_time -i input.mp4 -t total_duration -vcodec copy/h264/... -acode ...

  8. 2.PHP 教程_PHP 安装

    您需要做什么? 找一个支持PHP和MySQL的主机 在您自己的PC机上安装web服务器,然后安装PHP和MySQL 使用支持PHP的Web的主机 如果您的服务器支持PHP,那么您不需要做任何事情. 只 ...

  9. A Byte of Python 笔记(5)函数:定义、形参、局部变量、默认参数、关键参数

    第7章  函数 函数是重要的程序段.它们允许你给一块语句一个名称,然后你可以在程序的任何地方使用这个名称任意多次地运行这个语句块.这被称为 调用 函数. 定义函数 函数通过 def 关键字定义.def ...

  10. 使用CarrierWave上传图片时,多版本文件名的统一

    第一次使用CarrierWavewe做上传,不能不说,虽然Rails已经把上传变得超简单了,而CarrierWave则是把上传变成了一种享受,特别是做图片上传,现在这年代,图片展示平台已经不仅仅是电脑 ...