【PAT甲级 LinkedHashMap】1041 Be Unique (20 分) Java、C++版
题目
题目的意思是,找到第一个不重复的元素。输出它。
一开始用Java做,试了好多种方法都超时。后来换用C++通过了。
AC题解:C++版
用Java尝试了各种方式,都是后两个测试点过不去,只好换C++
这个算法挺巧妙的,注意记录输入顺序的方式
#include <iostream>
using namespace std;
int main()
{int num[100001], count[10001];int n;cin >> n;for ( int i = 0; i < n; i++ ){cin >> num[i];count[num[i]]++;}for ( int i = 0; i < n; i++ ){if ( count[num[i]] == 1 ){cout << num[i];return(0);}}cout << "None";return(0);
}
超时解法1:使用LinkedHashMap
提问:java中如何统计数组中出现相同元素的个数?
回答:你可以用map类型,思路大概是这样的:
- 把数组从第0个开始 保存在<key,value>里
- 作一个对比过程,其中key表示元素,存的过程作一个对比,如果相同,则其相应的value值+1
- 最后输出key:value
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int total = sc.nextInt();// 使用map存储Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();for (int i = 0; i < total; i++) {int num = sc.nextInt();Integer v = map.get(num);if (v != null) {map.put(num, v + 1);} else {map.put(num, 1);}}// 遍历mapIterator<?> iter = map.entrySet().iterator();while (iter.hasNext()) {Map.Entry<Integer, Integer> entry = (Entry<Integer, Integer>) iter.next();if ((Integer) entry.getValue() == 1) {System.out.println(entry.getKey());return;}}System.out.println("None");}
}
超时解法2:同样的算法,C++不超时
- 读取数据时,
BufferedReader
比使用Scanner
快一些 - 使用
Scanner
要花120多ms,BufferedReader
大约80ms
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String aa = in.readLine();String[] arr = aa.split(" ");int n = Integer.parseInt(arr[0]);int[] num = new int[100001];int[] count = new int[100001];for (int i = 0; i < n; i++) {num[i] = Integer.parseInt(arr[i + 1]);count[num[i]]++;}for (int i = 0; i < n; i++) {if (count[num[i]] == 1) {System.out.println(num[i]);return;}}System.out.println("None");}
}
使用Scanner
代替BufferedReader
:超时更严重
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int[] num = new int[100001];int[] count = new int[100001];for (int i = 0; i < n; i++) {num[i] = sc.nextInt();count[num[i]]++;}for (int i = 0; i < n; i++) {if (count[num[i]] == 1) {System.out.println(num[i]);return;}}System.out.println("None");}
}
超时解法3:两层for循环
暴力O(n^2)
循环嵌套,耗时也不过如此嘛,虽然超时了,但是居然比Map
还快一点点
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int total = sc.nextInt();int[] arr = new int[total];for (int i = 0; i < total; i++) {arr[i] = sc.nextInt();}// 找第一个不重复的int i, j;for (i = 0; i < total; i++) {for (j = 0; j < total; j++) {if(i==j)continue;if (arr[i] == arr[j]) {break;}}if (j == total) {System.out.println(arr[i]);return;}}System.out.println("None");}
}
【PAT甲级 LinkedHashMap】1041 Be Unique (20 分) Java、C++版相关推荐
- 【PAT (Advanced Level) Practice】1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- PAT甲级 -- 1148 Werewolf - Simple Version (20 分)
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- 【题意+解析】1041 Be Unique (20 分)_18行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Being unique is so important to people on Mars that even their lo ...
- PAT甲级 1027 Colors in Mars (20分)
1027 Colors in Mars (20分) 题目链接:PAT A 1027 题目大意:给出三个十进制数,都是在[0-168]范围内,要求将他们转化为13进制后按顺序输出. 思路分析:非常简单的 ...
- PAT甲级 -- 1041 Be Unique (20 分)
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- 【PAT甲级 约会】1061 Dating (20 分) C++ 全部AC
题目 这个题要注意的细节很多.因为一个星期有七天,一天有24小时,所以要注意字母范围这个隐藏条件,不符合条件的字母要跳过. 还要注意:第二次查找,要接着第一次找到的位置开始找.这个有点坑,题目里没说明 ...
- PAT甲级 -- 1005 Spell It Right (20 分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- 18年春季第一题 PAT甲级 1144 The Missing Number (20分) 上限感很重要
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...
最新文章
- LeetCode Number of Islands(flood fill)
- spring boot actuator服务监控与管理
- Java基础day19
- 内存泄漏Valgrind
- 全民加速节:全站加速在互联网媒体应用上的最佳实践
- 微软小冰学会画画了,堪称复活近代画家,还能命题作画
- 华为宣布智能计算战略,全栈全场景,当企业智能化转型加速器
- mongodb入门基本语法
- 精业科技(天津)有限公司 Game Over
- Qt制作贪吃蛇小游戏
- C语言——函数的调用
- 基础学习日志 ━━ 变量、函数、类命名时常用的英文词汇
- 华为认证网络工程师系列教程-HCNA
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
- 波士顿学院计算机,波士顿学院(Boston College)_快飞留学
- 微信小程序动态更改标题栏_微信小程序动态改变导航栏标题和背景颜色
- 联想电脑尺寸在哪里看_如何检查联想电脑型号【详细介绍】
- Python-docx生成word文档
- linux下常用拷贝命令
- oracle 获取当月的1号_Oracle 获取当前年、月、日
热门文章
- 多维数组(冒泡排序,稀疏数组)
- 安装失败java.lang_linux安装jdk出现java/lang/NoClassDefFoundError: java/lang/Object错误的解决方案...
- 快速沃尔什变换(FWT)
- python3爬虫(7)反反爬虫解决方案
- ADO学习(八)源码示例
- SQL 性能优化梳理 —— 基本概念、创建时优化、查询时优化
- OS- -请求分页系统、请求分段系统和请求段页式系统(一)
- C++中类的6个默认成员函数
- 音视频技术开发周刊 | 238
- Vulkan Video实现GPU加速视频编码/解码