/*
题目:2.1.1 Remove Duplicates from Sorted Array
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]
*/
/*
分析:数组中元素去重问题
要求:不能申请新空间,需要在原来的数组空间上操作
思路:1、用两个指针指向前后两个元素
2、比较前后两个元素是否相同
3、不同则将后一个元素写入前一个指针所表示的数组中
4、直到数据末尾 注:该方法只适合已排好顺序的数组
*/
#include <stdio.h>
#include <stdlib.h> //写一个类,类里面的public成员中放这些函数(注意类的书写规范:1、类名大写 2、中括号后有分号)
class Solution{
public:
//类内实现函数(区分类内定义,类外实现。作用域限定符的使用)
int removeDuplicates(int A[],int n){ //数组作为函数参数如何传递?
if(n == ) return ; //数组长度为零的情况 int index = ;
for(int i = ;i < n;i++){
if(A[index] != A[i])
A[++index] = A[i];
}
return index + ;
}
};
int main()
{
int A[] = {,,,,,,,,};
Solution s1;
int n = s1.removeDuplicates(A,);//数组传参,只用传递数组名 printf("个数:%d\n",n);
for(int j =;j < n;j++)
printf("%d ",A[j]); system("pause");
return ;
}

2.1.1Remove Duplicates from Sorted Arr的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted Array I II

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

  2. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  5. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  6. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  7. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  8. Remove Duplicates From Sorted Array

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

  9. 【leetcode】Remove Duplicates from Sorted Array II

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

随机推荐

  1. java学生成绩管理系统

                                                       信1805-1 20183590 田庆辉             石家庄铁道大学 2019 年秋季 ...

  2. Spring - 周边设施 - H2 数据库启动时写入数据

    1. 概述 之前讲到了 H2 的引入 这下我想说说 H2 启动时的 数据导入 2. 场景 需求 启动项目后, H2 启动起来 环境数据会自动注入 H2 数据库 可以验证是否成功 3. 环境 os wi ...

  3. 如何安装和使用Maven

    今天我们来学习一下如何安装Maven,把步骤分享给大家,希望能对大家有帮助! 我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/11996550 ...

  4. Java开发中使用模拟接口moco响应中文时乱码

    场景 在开发中需要依赖一些接口,比如需要请求一个返回Json数据的接口,但是返回Json数据的接口要么是没搭建,要么是交互比较复杂. 此时,就可以使用moco来模拟接口返回接口数据,以便开发和测试工作 ...

  5. 负环--spfa

    洛谷板子题 负环?是有负权边的环还是一个边权之和为负的环? 还没有准确的定义(那就先忽略吧qwq 判断负环的方法: 暴力枚举/spfa/mellman—ford/奇怪的贪心/超神的搜索 可惜我只会sp ...

  6. sqli-libs(11-22关)

    Less_11 点开11关出现登录页面: 1.可以随便输密码和账号,submit之后出现错误,可以看到查询username和password用and连接放在同一个SQL语句中 打开使用burp sui ...

  7. PHP转换oracle数据库的date类型

    今天圣诞节啊,圣诞节快乐啊! 最近遇到一个很纠结的事,就是我在plsql里面查的是这样的,很正常, 但是我用程序查出来就是这样的,啊啊啊,真是崩溃啊 但是我传数据需要上面那种格式,而且我对oracle ...

  8. qq音乐解析API

    文档:www.tjit.net 开放的接口:api88.net 个人代码: input2(event){ //将字符转化为encodeURL编码,才能进行正确请求,这是这个接口要求的 //js自带的转 ...

  9. Floyd-Warshall

    Description 第一行四个数为n,m,n表示顶点个数,m表示边的条数. 接下来m行,每一行有三个数t1.t2 和t3,表示顶点t1到顶点t2的路程是t3.请注意这些t1->t2是单向的. ...

  10. 好用的px转rem插件cssrem

    下载本项目,比如:git clone https://github.com/flashlizi/cssrem 进入packages目录:Sublime Text -> Preferences - ...