leetCode练题——27. Remove Element
1、题目
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length =5, with the first five elements ofnumscontaining0,1,3,0, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
大致意思是把一个列表里指定的数字去掉,并且不新增存储空间,最后返回去除指定数字后的列表长度;
2、我的解法
还是采用双指针法,大致解法如下:
# -*- coding: utf-8 -*-
# @Time : 2020/2/3 15:38
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 27. Remove Element.py
from typing import List
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
lens = len(nums)
if lens > 0: #判断列表是否为空
j = 0
for i in range(lens):
if nums[i] != val: #若i指定的数字不等于val
temp = nums[j]
nums[j] = nums[i]
nums[i] = temp #i和j指向的数字位置互换
j = j + 1 #j再往后移动
return j
print(Solution().removeElement([3,3,2],3))
leetCode练题——27. Remove Element的更多相关文章
- LeetCode记录之27——Remove Element
这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- [LeetCode&Python] Problem 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...
- LeetCode Array Easy 27. Remove Element 解题
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 【leetcode❤python】27. Remove Element
#-*- coding: UTF-8 -*- class Solution(object): def removeElement(self, nums, val): "& ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 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 ...
随机推荐
- C语言数据结构——第四章 串
四.串 4.1-串的基本概念 串的定义 串是由数字.字母或其他字符组成的有限序列,一般记为 StringName = “a[0]a[1]a[2]···a[i]···a[n-1]”(n>0,0&l ...
- JS高级---函数的几个成员
函数的几个成员 函数中有一个name属性----->函数的名字, name属性是只读的, 不能修改 函数中有一个arguments属性--->实参的个数 函数中有一个length属性--- ...
- IntelliJ IDEA 2017.3百度-----文件树状结构
- docker跨主机链接
三种方式 一,使用网桥实现跨主机容器连接
- 初篇——目录(JavaMail)
结构图 目录 邮件的知识体系由三部分组成,邮箱服务器,邮件程序,邮件协议. 邮箱服务器一般都是由公司的网络工程师搭建完成,基本上与程序员没有关系.但是笔者还是亲自尝试,使用Apache James搭建 ...
- 【13】堆排序 最小K个数
题目 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 收获 优先队列实现 (n1,n2)->n2-n1是 ...
- 一文明白所谓的CS与BS设计模式
CS设计模式 概念:CS设计模式,C代表的是Client,S代表的是Server.正如图中的所示,是客户机与服务器之间的交互.这种交互在早期的软件系统中,大多数都是采用这种模式,通过将任务合理分配到C ...
- 【转载】Java的JDBC事务详解
转自:http://www.cnblogs.com/azhqiang/p/4044127.html 事务的特性: 1) 原子性(atomicity):事务是数据库的逻辑工作单位,而且是必须是原子工作单 ...
- 力扣MYSQL练习
176编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . select IFNULL((SELECT distinct salary from employee or ...
- GCC的编译过程和链接
GCC编译过程 一个C/C++文件要经过预处理(preprocessing).编译(compilation).汇编(assembly).和连接(linking)才能变成可执行文件. gcc的常用选项 ...