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 ...
随机推荐
- Form DataGridView绑定BindingSource的几种方式
本文链接:https://blog.csdn.net/qq_15138169/article/details/83341076 在WinForm的开发中,ListView和DataGridView应用 ...
- Nexus坑人系列-license格式问题
这种情况一般出现在RMA或者新设备使用的时候.这些时候一般需要安装license,在安装完license的时候,例如我们去配置一些三层特性,例如feature eigrp等,可能会出现设备拒绝了你的命 ...
- 删除文件时提示,你需来自SYSTEM的权限
1. 提示如下 2. 对要删除的文件操作如下 2.1 为删除的文件添加本地账户 2.2 提示如下,多点几次继续就好 2.3 给本地账户添加完全控制权限
- iptables详解(2):四表五链
关于iptables中“四表五链”,我们今天来好好唠唠: 1.表的概念: 我们把具有相同功能的规则的集合叫做"表",所以说,不同功能的规则,我们可以放置在不同的表中进行管理,而ip ...
- 【C语言】用函数实现两个数排序(指针作函数参数)
原理就不讲了,这里用来理解指针的使用方法 #include <stdio.h> void fun(int* a,int* b) { int t; if(*a>=*b) { t = * ...
- 一个c程序的执行是从什么开始的?
一个C语言的执行是从本程序的main函数开始,到main函数结束,但需要注意的是,也是有特殊情况的,若是代码中出现了exit函数,则直接结束程序,对于这一点还是需要了解的. 关于c语言需要了解的是,C ...
- 爬虫 - Scrapy中间件
前提:看Scrapy架构图 不管什么Middlewares,都写在middlewares.py里面. 然后在settings.py里的DOWNLOADER_MIDDLEWARES或者SPIDER_MI ...
- Multisim中'地'的问题
1.地其实就是一个参考电压 对于示波器而言,只用连接一个探头,另一个探头默认就是连接地.
- CSS实现心形、六角星、六边形、平行四边形等几何
本文将利用border属性实现简单几何的绘制: 效果图: 正八角星 说明:采用两个正方形以中心进行旋转叠加: /* 八角星 */ #burst-8 { background: #6376ff1f; w ...
- https://www.cnblogs.com/lfs2640666960/p/8529115.html
https://www.cnblogs.com/lfs2640666960/p/8529115.html