题目来源:

  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. 老男孩python第六期

    01 python s6 day7 上节回顾02 python s6 day7 SNMP使用03 python s6 day7 大型监控架构讲解04 python s6 day7 Redis使用05 ...

  2. HDU 5820 Lights(扫描线+zkw线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5820 [题目大意] 在一个大小为50000*50000的矩形中,有n个路灯. 询问是否每一对路灯之 ...

  3. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  4. cc2540 cc2541 低功耗实測和总结-与注意事项 - 低功耗小于10uA

    CC2541 CC2540 实现超低功耗是很重要的: 我们来总结一下实现方法: 1,有定时器在跑时会一直跑在  PM2  电流在  300uA左右.    没有定时器跑后会到 PM3 , 电流会少于1 ...

  5. 一步一步学c#(六):数组

    数组 简单数组 如果需要使用同一个类型的多个对象,就可以使用数组.数组是一种数据结构,它可以包含同一类型的多个元素. 数组声明:int[] myArray 使用new运算符,指定数组中元素的类型和数量 ...

  6. sublime test3 使用技巧

    sublimeText3使用技巧 常用快捷键 ctrl+d :选中光标处的文本单元,继续按ctrl+d选中相同文本单元 alt+F3 :功能和ctrl+d类似,用于批量修改相同文本 shift+↑ ↓ ...

  7. <精华篇>:iOS视频大全-持续更新

    注意:新浪微博分享的资料和简书分享的资料,略有不同! 小码哥swift3.0版 斗鱼项目视频:点击下载  iOS开发25个项目实战:点击下载 2016PHP全套下载:点击下载  黑马刀哥iOS视频精选 ...

  8. JavaSE复习日记 : 算是个小前言吧

    /* * Java也学了好久了,抽个时间整理了一下课堂笔记,也有些是我刚开始学会犯的一些错误.在这里浅谈一下JavaSE的基础内容,对我来说也是一种不错的复习方式. * * 那好,对于初学者来说,学习 ...

  9. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  10. [LeetCode]题解(python):072-Edit Distance

    题目来源: https://leetcode.com/problems/edit-distance/ 题意分析: word1最少通过多少步可以变成word2.word1只能进行一下的操作.a)插入一个 ...