导航页-LeetCode专题-Python实现

相关代码已经上传到github:https://github.com/exploitht/leetcode-python

文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo

1、读题

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

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:

Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

给定一个数组和一个值,移除这个数组中的所有该值。不要申请额外空间,在当前数组操作,操作完成后超出需要长度的值是什么无所谓。也就是说,数组[3,2,2,3]给定值为3,则处理之后需要返回2,数组处理成[2,2,…],前2个对就行了,后面的无所谓。这道题其实和上一题思路是一样的,都是原地处理数组,返回length,超出length的内容不关注。

2、解题

同上一题,只需要维护2个指针就OK了,快指针遍历数组,发现val直接后移,若不是则赋值给慢指针。代码很短,如下:

class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
point = 0
for i in range(0, len(nums)):
if nums[i] != val:
nums[point] = nums[i]
point += 1
return point

LeetCode专题-Python实现之第27题:Remove Element的更多相关文章

  1. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第1题:Two Sum

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. Android Getting Started

    Building Your First App Creating an Android Project 介绍如何Android开发环境,具体是:怎么使用Eclipse工具 Create a Proje ...

  2. tensorflow 使用 2 Felch ,Feed

    Felch ::在会话里可以执行多个 op , import tensorflow as tf input1 = tf.constant(3.0) input2 = tf.constant(2.0) ...

  3. Numpy 基础运算2

    # -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...

  4. VS2017 生成事件去除未修改项目

    1.右键“解决方案”→“配置管理器” 2.列“生成”,反勾选无需编译的项目 3.点击“确定”,重新编译即可跳过未勾选的项目.

  5. C++ 初步

    c++新特性: 1. 初始化方法: 复制初始化 int x=1024; 直接初始化 int x (1024); 2.变量随用随定义 3.增加bool数据类型 输入输出: cout<<x; ...

  6. [LeetCode] Transpose Matrix 转置矩阵

    Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...

  7. iptables安装

    1.安装iptable iptable-service #先检查是否安装了iptables service iptables status #安装iptables yum install -y ipt ...

  8. 将DataRow拷贝到另一个DataRow

    DataRow dr = dtPadFluid.Rows[gvPadFluid.FocusedRowHandle]; foreach (DataColumn dc in _dr.Table.Colum ...

  9. TechEmpower最新一轮的性能测试出炉,ASP.NET Core依旧表现不俗

    TechEmpower在10月30发布最新一轮(Round 17)针对“Web Framework Benchmarks”的性能测试报告,ASP.NET Core依旧表现不俗,在一些指标上甚至是碾压其 ...

  10. HttpClient 调用WebAPI时,传参的三种方式

    public void Post() { //方法一,传json参数 var d = new { username = " ", password = " ", ...