题目来源:

  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. Html 小插件6 百度新闻插件

    新闻免费代码"http://news.baidu.com/newscode.html ,便可在输入希望订阅的关键词后,根据相关选项的设置,百度便非常快的在当前页面的文本编辑框内生成相关代码. ...

  2. PHP并发最佳实践

    直接参考大牛的: http://www.searchtb.com/2012/06/rolling-curl-best-practices.html

  3. Using SetWindowRgn

    Using SetWindowRgn Home Back To Tips Page Introduction There are lots of interesting reasons for cre ...

  4. Mirantis Certification summary

    preface Mirantis Certification (MCA100 )summary roughly question types handy remain by Ruiy!

  5. 菱形java代码

    public class boy { //菱形 public static void main(String[] args) { int m=4; for (int i=0;i<=m;i++){ ...

  6. String、StringBuffer和StringBuild的区别

    public class Test1 { public static void stringReplace (String text) { text = text.replace('j','i') ; ...

  7. Android应用开发基础篇(11)-----ViewFlipper

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/01/2376067.html 一.概述 ViewFlipper这个部件是用来实现多页显示的,多页 ...

  8. MicroStrategy笔试

    1. coding判定二叉树是否是有序二叉树 2. 一个有序数组A(buffer足够大),和一个有序数组B,设计算法,merge两个数组后有序,不使用任何额外的内存空间 3. 100个点灯问题,初始状 ...

  9. PHP自练项目之数字分页效果

    学习要点:1.LIMIT 用法2.各种参数3.超链接调用 第一:先在文件中设置数字分页模块:我的文件是(blog.php) //分页模块 $_page = $_GET['page']; $_pages ...

  10. Oracle 奇葩的问题:创建存储过程没有反应

    问题描述:需要在oracle 数据库中再创建一个数据库(数据库实例)然后作为临时数据库,一切成功: 现在需要在数据库中新建一个表空间然后创建用户,使用创建的用户登录创建一个存储过程,执行提交刷新一下, ...