/*
题目: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. c语言getipaddrtable获得ip地址与sendArp获得mac地址以及一些字节序问题记录

    https://docs.microsoft.com/zh-cn/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable msdn,有很多c的源码还 ...

  2. Java - 判断字符串是否是回文

    首先,回文是指类似于“12345”,“abcdcba”的形式,即正念和反念都是一样的字符串 判断字符串是否是回文,这边介绍3种办法 将字符串翻转,判断翻转后的字符串和原字符串是否相等 public s ...

  3. AcWing 846. 树的重心

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

  4. Flutter 中的表单

    一.Flutter 常用表单介绍   Flutter 中常见的表单有 TextField 单行文本框,TextField 多行文本框.CheckBox.Radio.Switch  CheckboxLi ...

  5. Go_file操作

    1. FileInfo package main import ( "os" "fmt" ) func main() { /* FileInfo:文件信息 in ...

  6. C 语言实例 - 判断闰年

    用户输入年份,判断该年份是否为闰年. #include <stdio.h> int main() { int year; printf("输入年份: "); scanf ...

  7. yii2时区语言设置

    main.php return [ 'charset' => 'utf-8', 'language' => 'zh-CN', 'timeZone' => 'Asia/Shanghai ...

  8. BFSDFS模板

    BFS模板: private static void bfs(HashMap<Character, LinkedList<Character>> graph,HashMap&l ...

  9. libcurl库的简介(二)

    下面是使用libcurl库实现文件上传的一个实例: void CDataProcess::sendFileToServer(void) { string netIp = strNetUrl + &qu ...

  10. centos7下安装pcre库(pcretest)

    在linux下需要对正则表达式的验证,使用的验证工具是pcretest,这个工具集成在pcre库中,下面是安装教程. 安装环境是centos7. 1)首先去官网下载压缩包文件. 其他的source网站 ...