题目来源:

  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. perl 爬取上市公司业绩预告

    <pre name="code" class="python">use LWP::UserAgent; use utf8; use DBI; use ...

  2. Linux下安装memcached

    Linux下安装memcached 1.运行memcached需要本文开头介绍的libevent库 $ sudo yum install libevent libevent-deve 2.下载安装me ...

  3. 解决[warn] _default_ VirtualHost overlap on port 80, the first has precedence问题

    问题背景: 在apache的httpd.conf里新增加了1个VirtualHost,域名是xxx.com,此时,服务器总共2个VirtualHost ,service httpd restart的时 ...

  4. 脑波设备mindwave TGCD接口开发示例

    对于TGCD的开发,神念科技提供的文件包括,头文件thinkgear.h,thinkgear.lib,thinkgear.dll,有这三个文件,在win32下开发就不是什么难事了吧 如果是java语言 ...

  5. HTML技术简介

    1.DHTML:"Dynamic HTML"动态HTML技术的简称.DHTML并不是一项新技术,而是HTML,CSS,JavaScript技术组合的术语.DHTML背后的含义是: ...

  6. zen-coding for notepad++,前端最佳手写代码编辑器

    zen-Coding是一款快速编写HTML,CSS(或其他格式化语言)代码的编辑器插件,这个插件可以用缩写方式完成大量重复的编码工作,是web前端从业者的利器. zen-Coding插件支持多种编辑器 ...

  7. javascriptDOM编程艺术_学习笔记_知识点 动态创建标记

    传统技术:document.write 和 innerHTML 深入剖析DOM方法:createElement.createTextNode.appendChild 和 insertBefore   ...

  8. 网页往数据库里插数据要用utf8,否则就乱码

    把网页的这行<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ...

  9. C++获取当前机器内网IP地址

    /*头文件*/ #include "winsock2.h" #pragma comment(lib,"ws2_32.lib") /*Hui 获取当前服务器IP* ...

  10. 射频识别技术漫谈(15)——Mifare1的安全性及7字节序列号M1卡

    Mifare1的安全性主要指卡中数据的安全性,要求卡中的数据不能被非法修改或窃听.数据的安全性主要使用加密技术来保证,加密技术有两个关键因素:加密算法和密钥.现代加密技术的一大特点是加密算法公开,如果 ...