当前位置:网站首页>删除数组中的重复项(保留最后一次出现的重复元素并保证数组的原有顺序)
删除数组中的重复项(保留最后一次出现的重复元素并保证数组的原有顺序)
2022-07-22 20:30:00 【斯沃福德】
链接
第21题;

方法一:栈
逆向遍历,遍历时用contain方法判断是否已存在;(Stack继承于Vector类;Vector实现了Iterable和Collection接口)
接收再弹出给新的数组,最后返回新数组;
结果:通过6/8,运行超时
public int[] removeDuplicate (int[] array) {
// 暴力
//最后一次出现,那就逆向遍历
Stack<Integer> s=new Stack<>();
for(int i=array.length-1;i>=0;i--){
if(!s.contains(array[i])){
s.push(array[i]);
}
}
//放入新数组
int n=s.size();
int[] r=new int[n];
//弹栈,正好顺序颠倒过来
for(int j=0;j<n;j++){
r[j]=s.pop();
}
return r;
}
方法二:Set判断是否重复 + 快慢指针
先逆序遍历,使用Set判断是否重复,重复就置为0;
使用快慢指针删除0;
将长度为slow的所有元素放至新数组并返回;
public int[] removeDuplicate (int[] array) {
int n=array.length;
Set<Integer> s=new HashSet<>();
// 倒序遍历,将重复的置为0
for(int i=n-1;i>=0;i--){
if(s.contains(array[i])){
array[i]=0;
}else{
s.add(array[i]);
}
}
// 快慢指针删除数组中的0
int slow=0;
int fast=0;
while(fast<n){
if(array[fast]==0){
fast++;
}else{
array[slow]=array[fast];
slow++;
fast++;
}
}
//0~slow移到新数组
int[] r=new int[slow];
for(int i=0;i<slow;i++){
r[i]=array[i];
}
return r;
}
结果:通过6/8
边栏推荐
- Cloudwego's design practice in the background platform of flybook management
- dispatch_once 的秘密
- 国泰君安证券股票开户怎么样安全吗
- Image processing solution veimagex technology evolution Road
- What key should I press to release the computer from sleep mode when the computer is standby
- ES6新增的class类
- LAN SDN technology hard core insider 13 II from LAN to Internet
- 局域网SDN技术硬核内幕 - 15 三 从物到人 园区用户漫游的MPLS实现
- How to delete non system files on disk C. summary of files that can be deleted when disk C is popular
- Microsoft, how can't we upload photos? What's the matter? The solution of twinsornot photos not being transmitted
猜你喜欢

基于API调用管理的SDN应用层DDoS攻击防御机制

Nftscan and ATEM network have reached strategic cooperation in the field of NFT data

支持多数不规则用户的隐私保护联邦学习框架
![[FAQ] common reasons and solutions for the failure of in app payment services to pull up the payment page](/img/9c/64553c1be85f2ee9baf1156d48c594.png)
[FAQ] common reasons and solutions for the failure of in app payment services to pull up the payment page

ES6解决异步问题

“外卖员的时间没有程序员值钱”:读过书就把自己当人上人?得电

每日刷题记录 (三十一)

Zhimeng dedecms forgot to manage the background password retrieval method

-bash: wget: 未找到命令

Esphone's self-made infrared remote control is connected to ha to control lights, switches, etc. any remote control can be used
随机推荐
postman “status“: 500, “error“: “Internal Server Error“, “message“: “The request was rejecte“
AE常用表达式汇总「建议收藏」
如何制作优质的VR全景?有简单基础的能拍吗?
在项目开发中的Logback日志框架技术
How to restore the computer screen display zoom tutorial when the computer screen becomes larger
Inside the hard core of LAN SDN technology - 14 three from things to people - SDN enters the campus network
excel 将图片的链接URL 显示为图片 转
UNO/ESP8266 for TCA9548A模块双通道驱动2块SH1106 1.3“显示
聪明人的游戏提高篇:第三章第三课例题:素数的秘密(prime)
LAN SDN technology hard core insider 13 II from LAN to Internet
Unable to open the proxy server. What should I do if the proxy server is not set to full access?
postman “status“: 500, “error“: “Internal Server Error“, “message“: “The request was rejecte“
深度学习模型的版权保护研究综述
【随笔】再见IE浏览器,一个时代的印记从此消失
SQL Server中的STRING_SPLIT函数的实现
低代码服务商ClickPaaS与毕普科技完成战略合并,共同打造工业数字化底座
VIM text editor
How to do if the control panel program cannot be uninstalled? Compulsory uninstallation software tutorial
Fileinputformat.setinputpaths multipath read rule
Esphone's self-made infrared remote control is connected to ha to control lights, switches, etc. any remote control can be used