Q5: Remove Duplicates from Sorted Array

时间:2022-10-29 10:55:33

问题描述:

Given a sorted array, 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 in place with constant memory.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

解决原理:

(若输入数组无序,则先对数组进行排序,则相同值相邻)

两个游标len,i

0~len是元素不重复的子数组,即len是目标子数组的最后一个元素的索引

游标i遍历数组

若A[i]!=A[len],则为目标子数组增添一个元素

代码:

 class Solution {
public:
int removeDuplicates(int A[], int n) {
int len = ;
if(n == ) return ;
//sort(A,A+n);
for(int i = ; i < n; i++){
if(A[i] != A[len]){
len++;
A[len] = A[i];
}
}
return len+;
}
};

Q5: Remove Duplicates from Sorted Array的更多相关文章

  1. &lbrack;LeetCode&rsqb; Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. &lbrack;LeetCode&rsqb; Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 26&period; Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. 50&period; Remove Duplicates from Sorted Array &amp&semi;&amp&semi; Remove Duplicates from Sorted Array II &amp&semi;&amp&semi; Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  7. LeetCode&colon;Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  8. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  9. leetCode 26&period;Remove Duplicates from Sorted Array&lpar;删除数组反复点&rpar; 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. trunk 的坑

    三层交换机A和三层交换机B之间原本配置有trunk口,如再用trunk连二层交换机会导致环路,网络不通.将L3 A和L3-B之间断开正常.

  2. 一次性搞清楚equals和hashCode

    前言 在程序设计中,有很多的“公约”,遵守约定去实现你的代码,会让你避开很多坑,这些公约是前人总结出来的设计规范. Object类是Java中的万类之祖,其中,equals和hashCode是2个非常 ...

  3. fork()详解

    参照: http://blog.csdn.net/jason314/article/details/5640969 http://coolshell.cn/articles/7965.html

  4. What is the difference between position&colon; static&comma;relative&comma;absolute&comma;fixed

    What is the difference between static,relative, absolute,fixed we can refer to this link: expand

  5. Ubuntu 安装vsftp软件&lpar;已测试&rpar;

    首先开启Ftp端口 使用apt-get命令安装vsftp #apt-get install vsftpd -y 添加ftp帐号和目录 先检查一下nologin的位置,通常在/usr/sbin/nolo ...

  6. MVC初学1

    MVC - Model ,View, Control 主要的程序思想:约定优于配置 百度下载程序:百度一下 密码:654321 百度视频地址:百度一下

  7. js学习总结:DOM节点一(选择器,节点类型)

    DOM:document object model 文档对象模型 DOM就是整个HTML文档的关系图谱(代表整个HTML文档),可以理解为下图: 一.查看元素节点 1.document.getElem ...

  8. JS实现购物车01

    需求 使用JS实现购物车功能01 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  9. Flex动画效果的用法--Resize

    Flex动画效果的用法--Resize FlexAdobeXML  <?xml version="1.0" encoding="utf-8"?> & ...

  10. json-server&plus;mockjs 模拟REST接口

    前言: 项目开发中,影响项目进程的常常是由于在前后端数据交互的开发流程中停滞,前端完成静态页面的开发后,后端迟迟未给到接口.而现在,我们就可以通过根据后端接口字段,建立一个REST风格的API接口,进 ...