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 ...
随机推荐
- jquery ajax获取后台数据后无法输出
今天做ajax获取数据,再浏览器的debugger窗口也看到了数据 ajax代码 $('#userSearch').click(function(){ $.get("loadAllUsers ...
- A task in a suit and a tie:paraphrase generation with semantic augmentation解读
1.该算法核心:在seq2seq模型的编码器中增加语义的frame 和 roles 2.上图为算法整个流程: 1).首先输入一句话s,SLING会使用frame和role label注释输入语句s,然 ...
- DFC-3C和DFC-3B的区别和注意事项
1.Product numbers:WS-F6K-DFC(=)WS-F6K-DFC3A(=)WS-F6K-DFC3B(=)WS-F6K-DFC3BXL(=)WS-F6K-DFC3C(=)WS-F6K- ...
- 1.4、WebRTC源码
文章导读:本篇文章给读者展示WebRTC的源码目录结构,为读者构建全方位的知识体系,如果你有兴趣下载webrtc的源码来编译运行,本节内容可以作为你了解源码的简要说明书,webrtc源码非常庞大的,讲 ...
- maven的安装与使用(运行单元测试和打包等)
maven的下载与安装 maven是用于java的自动化构建工具. 1.下载: http://maven.apache.org/download.cgi 下载maven包,比如 apache-mave ...
- 09-Docker-Volumes数据管理
目录 09-Docker-Volumes数据管理 参考 数据卷类型 数据卷操作 bind数据卷 volume数据卷 tmpfs数据卷 09-Docker-Volumes数据管理 Docker Vers ...
- java调用第三方接口(转载)
在很多时候有些别人做过的东西直接拿来用就好了,例如:身份证的信息.某个地区的天气.电话归属地等等. 代码 //import com.alibaba.fastjson.JSONObject; impor ...
- Navicat Premium 12安装、激活
Navicat Premium 12安装 Navicat Premium 12激活
- Go语言 二分查找算法的实现
二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,二分查找算法的前提是传入的序列是有序的(降序或升序),并且有一个目标值. 二分查找的核心思想是将 n 个元素分成大 ...
- linux下的apue.3e安装[Unix环境高级编程]
近期正在看<Unix环境高级编程>一书,目前看了20多页,书中 有几个c的例程,于是想着,也让它运行一下,看看是否跟描述相符,做开发人员本应该这样,于是敲了一部分代码,然后编译,发现并不能 ...