当前位置:网站首页>Delete the duplicate items in the array (keep the last duplicate elements and ensure the original order of the array)
Delete the duplicate items in the array (keep the last duplicate elements and ensure the original order of the array)
2022-07-23 07:36:00 【Swarford】
link
The first 21 topic ;

Method 1 : Stack
Backward Traversal , For traversal contain Method to determine whether it already exists ;(Stack Inherited from Vector class ;Vector Realized Iterable and Collection Interface )
Receive and pop up to the new array , Finally, a new array is returned ;
result : adopt 6/8, Run timeout
public int[] removeDuplicate (int[] array) {
// violence
// For the last time , Then reverse traversal
Stack<Integer> s=new Stack<>();
for(int i=array.length-1;i>=0;i--){
if(!s.contains(array[i])){
s.push(array[i]);
}
}
// Put the new array
int n=s.size();
int[] r=new int[n];
// Bomb stack , Just reverse the order
for(int j=0;j<n;j++){
r[j]=s.pop();
}
return r;
}
Method 2 :Set To judge whether or not to repeat + Speed pointer
First traverse in reverse order , Use Set To judge whether or not to repeat , Repeat and set as 0;
Use the speed pointer to delete 0;
The length will be slow Put all the elements of the new array and return ;
public int[] removeDuplicate (int[] array) {
int n=array.length;
Set<Integer> s=new HashSet<>();
// Reverse traversal , Set the repeated to 0
for(int i=n-1;i>=0;i--){
if(s.contains(array[i])){
array[i]=0;
}else{
s.add(array[i]);
}
}
// The speed pointer deletes 0
int slow=0;
int fast=0;
while(fast<n){
if(array[fast]==0){
fast++;
}else{
array[slow]=array[fast];
slow++;
fast++;
}
}
//0~slow Move to new array
int[] r=new int[slow];
for(int i=0;i<slow;i++){
r[i]=array[i];
}
return r;
}
result : adopt 6/8
边栏推荐
- gnu 伪指令定义函数
- 阿里云安全中心之漏洞修复最佳实践
- Codeforces Round #809 (Div. 2) A - D1
- “外卖员的时间没有程序员值钱”:读过书就把自己当人上人?得电
- Digital collections start the 100 billion level market
- C language file operation (including all knowledge points, detailed explanation of related functions and codes)
- LeetCode 757 设置交集大小至少为2[排序 贪心] HERODING的LeetCode之路
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- 从BIO到实现简单多人聊天室功能--IO模型
- dispatch_once 的秘密
猜你喜欢

类和对象(1)

VR panoramic zoo, a zoo business card with different achievements

Research on security situation awareness method of Internet of things based on evidence theory

10个Live Demo都展示了啥?看过没看过的都值得再看一遍

Talk about repaintboundary in fluent

小程序毕设作品之微信酒店预订小程序毕业设计(6)开题答辩PPT

Are most programmers eliminated after the age of 45? The truth chilled everyone's heart

Application of the latest version of Ontrack easyrecovery computer data recovery software

Clever use of curl

分析伦敦银的实时行行发展方法
随机推荐
VR全景动物园,成就不一样的动物园名片
从BIO到实现简单多人聊天室功能--IO模型
Attack and defense scheme based on Ethereum status database
小程序毕设作品之微信校园二手书交易小程序毕业设计成品(5)任务书
说说Flutter中的RepaintBoundary
2022就业季惊喜来袭!正版Adobe软件,终于能正经白嫖一把了
Uno/esp8266 for tca9548a module dual channel drive 2 sh1106 1.3 "displays
【无标题】
Digital collections start the 100 billion level market
《postgresql指南--内幕探索》第一章 数据库集簇、数据库和数据表
Unity 笔记——Addressables的使用
园区招商难在“哪”?产业园区招商引资困点难点问题盘点
rs485通信OSI模型网络层
开发过程中的总结 BaseService 为所有的 Controller或Service 提供一个公共获取 Service 的文件,减少重复注入
一文理解分布式开发中的服务治理
A web server where browser users access server files
Redis common basic configuration files
常用机械设备安全虚拟仿真系统的应用场景及方案
对比学习下的跨模态语义对齐是最优的吗?---自适应稀疏化注意力对齐机制 IEEE Trans. MultiMedia
删除数组中的重复项(保留最后一次出现的重复元素并保证数组的原有顺序)