题目

题目的意思是,找到第一个不重复的元素。输出它。

一开始用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++版相关推荐

  1. 【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 ...

  2. PAT甲级 -- 1148 Werewolf - Simple Version (20 分)

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  3. 【题意+解析】1041 Be Unique (20 分)_18行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Being unique is so important to people on Mars that even their lo ...

  4. PAT甲级 1027 Colors in Mars (20分)

    1027 Colors in Mars (20分) 题目链接:PAT A 1027 题目大意:给出三个十进制数,都是在[0-168]范围内,要求将他们转化为13进制后按顺序输出. 思路分析:非常简单的 ...

  5. PAT甲级 -- 1041 Be Unique (20 分)

    Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...

  6. 【PAT甲级 约会】1061 Dating (20 分) C++ 全部AC

    题目 这个题要注意的细节很多.因为一个星期有七天,一天有24小时,所以要注意字母范围这个隐藏条件,不符合条件的字母要跳过. 还要注意:第二次查找,要接着第一次找到的位置开始找.这个有点坑,题目里没说明 ...

  7. 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 ...

  8. 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 ...

  9. PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...

最新文章

  1. LeetCode Number of Islands(flood fill)
  2. spring boot actuator服务监控与管理
  3. Java基础day19
  4. 内存泄漏Valgrind
  5. 全民加速节:全站加速在互联网媒体应用上的最佳实践
  6. 微软小冰学会画画了,堪称复活近代画家,还能命题作画
  7. 华为宣布智能计算战略,全栈全场景,当企业智能化转型加速器
  8. mongodb入门基本语法
  9. 精业科技(天津)有限公司 Game Over
  10. Qt制作贪吃蛇小游戏
  11. C语言——函数的调用
  12. 基础学习日志 ━━ 变量、函数、类命名时常用的英文词汇
  13. 华为认证网络工程师系列教程-HCNA
  14. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
  15. 波士顿学院计算机,波士顿学院(Boston College)_快飞留学
  16. 微信小程序动态更改标题栏_微信小程序动态改变导航栏标题和背景颜色
  17. 联想电脑尺寸在哪里看_如何检查联想电脑型号【详细介绍】
  18. Python-docx生成word文档
  19. linux下常用拷贝命令
  20. oracle 获取当月的1号_Oracle 获取当前年、月、日

热门文章

  1. 多维数组(冒泡排序,稀疏数组)
  2. 安装失败java.lang_linux安装jdk出现java/lang/NoClassDefFoundError: java/lang/Object错误的解决方案...
  3. 快速沃尔什变换(FWT)
  4. python3爬虫(7)反反爬虫解决方案
  5. ADO学习(八)源码示例
  6. SQL 性能优化梳理 —— 基本概念、创建时优化、查询时优化
  7. OS- -请求分页系统、请求分段系统和请求段页式系统(一)
  8. C++中类的6个默认成员函数
  9. 音视频技术开发周刊 | 238
  10. Vulkan Video实现GPU加速视频编码/解码