为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1133. 最大唯一数 | Largest Unique Number

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11258418.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an array of integers A, return the largest integer that only occurs once.

If no integer occurs once, return -1.

Example 1:

Input: [5,7,3,9,4,9,8,3,1]
Output: 8
Explanation: 
The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it's the answer.

Example 2:

Input: [9,9,8,8]
Output: -1
Explanation: 
There is no number that occurs only once.

Note:

  1. 1 <= A.length <= 2000
  2. 0 <= A[i] <= 1000

给你一个整数数组 A,请找出并返回在该数组中仅出现一次的最大整数。

如果不存在这个只出现一次的整数,则返回 -1。

示例 1:

输入:[5,7,3,9,4,9,8,3,1]
输出:8
解释: 
数组中最大的整数是 9,但它在数组中重复出现了。而第二大的整数是 8,它只出现了一次,所以答案是 8。

示例 2:

输入:[9,9,8,8]
输出:-1
解释: 
数组中不存在仅出现一次的整数。

提示:

  1. 1 <= A.length <= 2000
  2. 0 <= A[i] <= 1000

80 ms

 1 class Solution {
 2     func largestUniqueNumber(_ A: [Int]) -> Int {
 3         var M:[Int:Int] = [Int:Int]()
 4         var ans:Int = -1
 5         for num in A
 6         {
 7             M[num,default:0] += 1
 8         }
 9         for (key,val) in M
10         {
11             if val == 1
12             {
13                  ans = max(ans,key)
14             }
15         }
16         return ans
17     }
18 }

 

posted @ 2019-07-28 12:02  为敢技术  阅读(685)  评论(0编辑  收藏  举报