2.1.1Remove Duplicates from Sorted Arr
/*
题目: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的更多相关文章
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- Java - 集合 - 定义和分类
Java集合框架主要包括两种类型的容器:Collection和Map 层级结构图:evernote:///view/27699174/s49/d9aaf84e-e218-40a0-89c1-358 ...
- 2.4 【配置环境】TestNG安装
两种方法可以安装TestNG Eclipse插件: (来源:http://blog.csdn.net/hongchangfirst/article/details/7679849/) 第一种,离线安 ...
- 解决teamviewer试用期到期的方法
Teamviewer是一款远程控制软件,使用过程中系统弹出“Teamviewer试用版已到期”的提示, 需要用户购买许可证或延长试用期才能继续使用,解决teamviewer试用期到期问题步骤如下: 出 ...
- hadoop学习笔记(五)hadoop伪分布式集群的搭建
本文原创,如需转载,请注明作者和原文链接 1.集群搭建的前期准备 见 搭建分布式hadoop环境的前期准备---需要检查的几个点 2.解压tar.gz包 [root@node01 ~]# ...
- Python3问题TypeError: object() takes no parameters
1. Python中关键字变量和特殊函数,都是以__xxx__来表示的 初学Python的朋友,需要注意其中变量名中前后是有两个下划线(_)的,如果不注意,调用内部关键字变量和特殊函数时,将会出现错误 ...
- 攻防世界 misc Exercise 刷题记录
1.base64stego 1.zip伪加密 2. base64文件隐写,在网上找一个脚本
- Tarjan-有向图
(我到底是咕了多少知识点啊) 在有向图中tarjan主要用来求强连通分量并缩点 一.定义 强连通:如果两个顶点可以相互通达,则称两个顶点 强连通 强连通分量:如果有向图G的每两个顶点都 强连通,称G是 ...
- C语言与汇编的嵌入式编程:main中模拟函数的调用(两数交换)
编写一个两数交换函数swap,具体代码如下: #include<stdio.h> void swap(int *p1,int *p2) { int temp; temp = *p1; *p ...
- Cookie API介绍
一.Java提供的操作Cookie的API Java中的javax.servlet.http.Cookie类用于创建一个Cookie Cookie类的主要方法 No. 方法 类型 描述 Cookie( ...
- Xcode 编译运行旧项目报错解决之路
运行几年前做的项目,发现各种编译报错,一个一个解决记录下: 1.Xcode(Xcode9)编译运行报错,但是在 issue navigatior 栏看不到错误信息: 解决方案:在 show repor ...