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 ...
随机推荐
- redis安装与基本使用
什么是Redis 什么是NoSQL 介绍Redis之前,先了解下NoSQL (Not noly SQL)不仅仅是SQL 属于非关系型数据库:Redis就属于非关系型数据库 传统的Mysql ,orac ...
- HBase 启动后HMaster进程自动消失
原因分析 1.hadoop 与 hbase 版本不兼容,导致的异常. 2.log日志信息显示 org.apache.hadoop.hbase.TableExistsException: hbase:n ...
- 【visio】数据可视化 - 链接到外部数据
编辑数据的时候,纯粹一个一个地手动输入,效率过低,visio提供了连接外部数据的功能,以加快数据导入,该功能也同时能够针对外部数据的改变,更新visio内部数据. 1. 创建外部源数据 visio支持 ...
- QQ发起聊天
QQ推广 网址: http://shang.qq.com/v3/widget.html 一键加群 实例: <a target="_blank" href="//sh ...
- 数据库程序接口——JDBC——API解读第二篇——执行SQL的核心对象
结构图 核心对象 Statement Statement主要用来执行SQL语句.它执行SQL语句的步骤为: 第一步:创建statement对象. 第二步:配置statement对象,此步骤可以忽略. ...
- GEE引擎假人系统自定义教程
现如今传奇游戏玩家数量日渐减少.为了给服务器增加人气,很多GM在服务端中增加了自动登录和自动打怪的假人系统.由于该系统登录的假人可以自动练功,自动攻城和实现简单的对话.完全可以做到以假乱真的地步!所以 ...
- pyqt:布局删除小部件
参照:https://stackoverflow.com/questions/5899826/pyqt-how-to-remove-a-widget import sip layout.removeW ...
- Centos7编译安装kafka-manager-2.0.0.2
一.kafka-manager简介 项目地址为:https://github.com/yahoo/kafka-manager 为了简化开发者和服务工程师维护Kafka集群的工作,yahoo构建了一个叫 ...
- php和redis怎么实现消息队列
把瞬间服务器的请求处理换成异步处理,缓解服务器的压力,实现数据顺序排列获取.本文主要和大家分享php和redis如何实现消息队列,希望能帮助到大家. redis实现消息队列步骤如下: 1).redis ...
- Spring Boot Json 之 Jackjson Fastjson
Json 是目前互联网应用使用最为广泛的信息交换格式之一.Spring Boot 内置了 Jackson .Json 在应用中主要体现在以下功能: 序列化 反序列化 字段格式化 验证自动化 目前长用的 ...