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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-hiyyessc-ku.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

If no integer occurs once, return -1.github

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:spa

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

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

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

示例 1:blog

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

示例 2:get

输入:[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 }
相关文章
相关标签/搜索