1 /*
 2 题目:2.1.1 Remove Duplicates from Sorted Array
 3 Given a sorted array, remove the duplicates in place such that each element appear only once
 4 and return the new length.
 5 Do not allocate extra space for another array, you must do this in place with constant memory.
 6 For example, Given input array A = [1,1,2],
 7 Your function should return length = 2, and A is now [1,2]
 8 */
 9 /*
10 分析:数组中元素去重问题
11 要求:不能申请新空间,需要在原来的数组空间上操作
12 思路:1、用两个指针指向前后两个元素
13     2、比较前后两个元素是否相同
14     3、不同则将后一个元素写入前一个指针所表示的数组中
15     4、直到数据末尾
16
17 注:该方法只适合已排好顺序的数组
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 //写一个类,类里面的public成员中放这些函数(注意类的书写规范:1、类名大写 2、中括号后有分号)
23 class Solution{
24 public:
25     //类内实现函数(区分类内定义,类外实现。作用域限定符的使用)
26     int removeDuplicates(int A[],int n){  //数组作为函数参数如何传递?
27         if(n == 0) return 0; //数组长度为零的情况
28
29         int index = 0;
30         for(int i = 1;i < n;i++){
31             if(A[index] != A[i])
32                 A[++index] = A[i];
33         }
34         return index + 1;
35     }
36 };
37 int main()
38 {
39     int A[] = {1,2,2,3,5,7,7,8,};
40     Solution s1;
41     int n = s1.removeDuplicates(A,8);//数组传参,只用传递数组名
42
43     printf("个数:%d\n",n);
44     for(int j =0;j < n;j++)
45         printf("%d ",A[j]);
46
47     system("pause");
48     return 0;
49 }

转载于:https://www.cnblogs.com/Star-Lit/p/8504011.html

2.1.1Remove Duplicates from Sorted Arr相关推荐

  1. leetCode-数组:Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素 考察数组的基本操作: class Solution {public int removeDupli ...

  2. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  3. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

  4. 2016.5.57—— Remove Duplicates from Sorted List

    Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...

  5. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  6. 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++> 给出排序好的 ...

  7. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

  8. LeetCode26. Remove Duplicates from Sorted Array

    问题链接:LeetCode26. Remove Duplicates from Sorted Array 注意点: 1.数组中可能是0个元素: 2.C++程序中,循环变量声明不能写在for语句中(编译 ...

  9. [leetcode]83.Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

最新文章

  1. SQL Server 2008高可用性系列:数据库快照
  2. 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include “pch.h“”?
  3. 为了在全球“买买买”,阿里工程师自研了一套海外HR系统
  4. javafx性能_对JavaFX Mobile应用程序进行性能分析
  5. Java分布式篇5——FastDFS
  6. AAAI2021 大运动“视频超分辨”中的对偶子网与多阶通信上采样方案
  7. SpringBoot : BeanFactory和FactoryBean的区别
  8. 简单python代码实例_求简洁优美的python代码例子、片段、参考资料
  9. BCD与ASCII码互转-C语言实现
  10. 深入css布局 (3)完结 — margin问题与格式化上下文
  11. 简书bug(已修复):网络不佳时安卓1.11.3呈现的空白页面如图
  12. SharePoint2010内容类型剖析(三)
  13. 如何使用 google 搜索
  14. 使用163邮箱+Python3.6 发送邮件/批量发送邮件
  15. WIN7 鼠标右键反应慢如何处理
  16. 申请微信小程序流程步骤
  17. 计算机连接宽带还需要登陆用户,每次电脑启动都需要手动连接宽带怎么办? 爱问知识人...
  18. 【入门】广电行业DNS、DHCP解决方案详解(三)——DNS部署架构及案例
  19. Spicy Restaurant (暴力多源bfs)
  20. Android 性能监测工具,优化内存、卡顿、耗电、APK的方法

热门文章

  1. 航飞原始影像外方位元素_【技术】无人机倾斜摄影建模技术在虚拟现实中的应用...
  2. codis配置_codis 源码理解
  3. 永磁直驱风力发电机结构图_风机越来越大,国内首台10兆瓦海上风力发电机研制成功...
  4. php代码在线base64解码,Base64在线编码解码实现代码 演示与下载
  5. linux系统网络对时,Linux系统网络优化【转】
  6. linux进程管理fork,Linux -- 进程管理之 fork() 函数
  7. 位域 内存 字节序_C语言中的位域、字节序、比特序、大小端(转)
  8. pkl格式数据 训练集_Detectron2训练自己的数据集手把手指导
  9. 下载人脸认证助手_认证助手最新版
  10. 华为手机怎么隐藏按键图标_华为手机隐藏技巧,一键简单设置,让沟通更加便捷...