[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 }
相關文章
相關標籤/搜索