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 ...
随机推荐
- Abp.Core运行时提示XX没有实现,或者没有依赖注入的问题,或者调试时提示做出更改的问题
因为abp的web层对application层有项目引用,但是对domain层并不存在项目引用,而是bin目录下直接引用的dll文件,所以当domain层修改后不会自动将dll文件同步过去.所以有时候 ...
- ES+VBA 实现批量添加网络图片
需求:通过自动读取相对应列的图片网址,自动添加到图片列,从而完成添加图片 案例:需要将备注列的图片网址添加到图片列的内容 关键代码 '引入API Private Declare Function UR ...
- 第二十一篇 Linux中的环境变量简单介绍
环境变量之 PATH 定义解释器搜索用户执行命令的路径 获取PATH变量的值: echo $PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/us ...
- 68 for循环2 for循环最简单的用法
#include <stdio.h> int main (void) { int i ; ; ; i<; i+=) //i+=2 等价于 i= i+2: { sum = sum + ...
- Django框架之图书管理系统(二)
该篇文章介绍多对多的关系介绍 一.数据库设计 一个作者对应多个书籍 一个书籍对应多个作者 总结也就是多对多之间的关系 通过模型类创建多对多之间的关系表的时候,Django框架通过ORM创建三个表,分别 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 K. Center
这题对于能加入最多边缘点的center点,这个点就是最优的center ,对于center点,总共是n^2的,顶多也就1e6,所以直接双重循环就行了, 然后map<pair,set >映射 ...
- mysql测试点
前言 性能测试过程中,数据库相关指标的监控是不可忽视的,在这里我们就MySQL的监控配置及重点涉及性能的一些参数进行说明. 在笔者的日常性能测试过程中,重点关注了这些参数,但不代表仅仅只有这些参数对性 ...
- ASCII码排序 题解
1. while(scanf("%c%c%c%*c",&a,&b,&c)!=EOF) 这里需要注意 输入多组语句 while后面不能加分号: 2.%*c& ...
- SSIS部署后执行失败,无法将保护的XML节点解密
将包属性中的 protectionLevel 设置成DontSaveSensitive 即可.
- Coursera-吴恩达机器学习课程笔记-Week3
logistic regression Binary classification problems logistic regression 是一个分类算法 Hypothesis function d ...