LeetCode专题-Python实现之第27题:Remove Element
相关代码已经上传到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的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第1题:Two Sum
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- NOIP2012提高组day2 T2借教室
这题骗分可以骗到满分(可能是数据不太强给强行过去了) 这道题如果是按照题意去模拟用循环去修改区间的话只有45分,正解是二分+差分数组,骗分也是差分数组但是没有使用二分,时间复杂度在最坏的情况下是O(n ...
- web基础要点记录
最近公司项目做完了,不怎么忙,翻看了一些基础的资料,文章.就做了个简单的记录. 1.Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示, 可通过加入 CSS 属性 -we ...
- JS for循环 if判断、white循环。小练习
1----输入正整数n,求1-n的和. var n=prompt("请输入一个正整数"); var sum=0; for (var i=1;i<=n;i++) { sum=s ...
- Nuxt.js 从入门到放弃
Nuxt 是 Vue 上的 SSR,也就是服务端渲染应用框架,可在很大程度上解决当前 SPA 和 CSR 的首页加载慢,不利于 SEO 的问题.本场 Chat 就将详细介绍 Nuxt 的使用以及相关概 ...
- modbus tcp数据报文结构
modbus tcp数据报文结构 请求:00 00 00 00 00 06 09 03 00 00 00 01 响应:00 00 00 00 00 05 09 03 02 12 34 一次modbus ...
- 关系数据库数据与hadoop数据进行转换的工具 - Sqoop
Sqoop 本文所使用的Sqoop版本为1.4.6 1.官网 http://sqoop.apache.org 2.作用 A:可以把hadoop数据导入到关系数据库里面(e.g. Hive -> ...
- elasticsearch 文档资料
1.Function Score Query 自定义查询评分 https://www.elastic.co/guide/en/elasticsearch/reference/current/query ...
- 我们来谈谈最近最热门的微信小程序
最近微信小程序真是火到不行,我们的经理大人也就此给我做了一定的培训.他讲的太好,我实在忍不住跟大家简单分享一下: 1.什么是微信小程序? 一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦 ...
- C# CSV 文件转换成DataTable
{ DataTable dt = new DataTable(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess ...
- java points[复习]
1 - & 与 && 的区别: &:不管左边是true还是false,右端都会进行运算: &&:当左端为false时,右端不再进行运算: 即在与运算时, ...