leetCode练题——26. Remove Duplicates from Sorted Array
1、题目
Given a sorted array nums, remove the duplicates in-place such that each element appear only once 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.
Example 1:
Given nums = [1,1,2], Your function should return length =2, with the first two elements ofnumsbeing1and2respectively. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length =5, with the first five elements ofnumsbeing modified to0,1,2,3, and4respectively. It doesn't matter what values are set beyond the returned length.
说实话,一开始真没看懂题目。后来才明白,是要把列表重复的数字去掉并且不增加多余的存储空间(体现在过程中),并且最后返回的是去重后的列表长度。
2、我的解答
采用双指针法,i和j依次指向列表中的元素,一旦 i 和 j 不等,nums[j+1]=nums[i],具体如下:
# -*- coding: utf-8 -*-
# @Time : 2020/2/2 12:23
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 26. Remove Duplicates from Sorted Array from typing import List
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
lens = len(nums)
if lens > 0:
j = 0
for i in range(lens):
if nums[j] != nums[i]:
j = j + 1
nums[j] = nums[i]
nums= nums[0:(j + 1)]
return j+1
print(Solution().removeDuplicates([1,2,2,3,3,5,5,5]))
leetCode练题——26. Remove Duplicates from Sorted Array的更多相关文章
- LeetCode记录之26——Remove Duplicates from Sorted Array
国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...
- 【leetcode❤python】26. Remove Duplicates from Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def removeDuplicates(self, nums): "&quo ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
随机推荐
- MyBatis(2)——增删改查
增删改查: 1.在实体类的映射文件中增加insert.update.delete标签与数据库语句,例如 <!-- 会去获取到对应的实体类的getter方法 --> <insert i ...
- SpringMVC--提交表单
今天使用AbstractCommandController做一个提交表单的样例 (1)首先,建立一个User.java package com.zk.domain; import java.util. ...
- 506,display有哪些值?说明他们的作用
block:转换成块状元素 inline:装换成行内元素 none:设置元素不可见 inline-block:想行内元素那样显示,但是其内容像块类型元素一样显示 list-item:想块类型元素一样显 ...
- Linux shell lrzsz上传下载命令
安装lrzsz做上传下载 工具使用secretCRT yum install -y lrzsz 1. 服务器<发送>文件,使用命令sz 2. 服务器<接收>文件,使用命令rz
- 架构师必备技能指南:SaaS(软件即服务)架构设计
1.介绍 从计算机诞生开始,就伴随着计算机应用程序的演变.简短的回顾历史,我们可以清楚的看到应用程序发生的巨大变化.上世纪70年代中期,随着个人PC机的爆炸式增长以及程序员的崛起,让计算机的计算能力得 ...
- kafka集群搭建记录
本文记录搭建kafka搭建过程. 一.硬件机器介绍 192.168.183.195 master-node 192.168.183.194 data-node1 192.168.183.196 dat ...
- JS中的字符串可以直接调用字符串对象的属性和方法
在JS中,会自动在字符串与字符串对象之间进行转换,因此,任何一个字符串常量都可以看作是一个String对象,其可以直接作为对象使用,只要在字符串变量的后面加 “.” 便可以直接调用String对象的属 ...
- winform 多个datagridview 之间同步滚动
1.添加Scroll事件 2.注意需判断数据长度,避免溢出 private void dgYY_Scroll(object sender, ScrollEventArgs e) { ) { dgFee ...
- 深入delphi编程理解之消息(六)无窗口单元消息的创建、接受及dispatch模式编程
一.程序界面 二.程序代码 (一).主界面代码 //========================================================================== ...
- 红帽RHCE培训-课程1笔记内容
ssh -X root@s0 1.环境变量 env 系统变量名都为大写; 引用变量名对应的值时使用$引导: SHELL下,修改变量临时生效. # PS1=' # ' # echo $PS1 永久生效放 ...