题目地址:https://leetcode-cn.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/

题目描述

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
    1.0 <= A[i] <= 1000

题目大意

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

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

解题方法

桶排序

类似于桶排序的方式,计算每个数字出现了多少次,从最大数字开始向左遍历,找到第一个出现次数为1的数。

C++代码如下:

class Solution {
public:
int largestUniqueNumber(vector<int>& A) {
vector<int> count(1010, 0);
for (int a : A) {
count[a]++;
}
for (int i = 1000; i >= 0; i --) {
if (count[i] == 1) {
return i;
}
}
return -1;
}
};

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】1133. Largest Unique Number 解题报告(C++)的更多相关文章

  1. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  2. 【LeetCode】263. Ugly Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...

  3. 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  4. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】509. Fibonacci Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  6. 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  8. 【LeetCode】246. Strobogrammatic Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  9. 【LeetCode】1056. Confusing Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

随机推荐

  1. Python基础笔记4

    模块 模块是一组Python代码的集合,一个.py文件就称之为一个模块(Module),按目录来组织模块称为包(Package).优点:提高了代码的可维护性:避免函数名和变量名冲突. mycompan ...

  2. Python编译工具Anaconda(含有spyder+jupyter)

    Anaconda的下载和安装 官方的下载地址:https://www.anaconda.com/distribution/ 安装程序为一个可执行程序文件,下载完成后双击执行程序即可完成安装.安装过程一 ...

  3. GIFS服务的使用

    1.安装Samba服务 登录192.168.200.20虚拟机,首先修改主机名,命令如下: [root@nfs-client ~]# hostnamectl set-hostname samba [r ...

  4. c#表格序号列

    <asp:BoundField HeaderText="序号" /> OnRowCreated="gridview_RowCreated" prot ...

  5. Ubuntu 14.04 升级到 Ubuntu16.04

    Ubuntu 14.04 升级到 Ubuntu16.04 1). 更改source.list 源 (24条消息) Ubuntu16.04 source.list更改源_dylan的博客-CSDN博客_ ...

  6. CR LF 的含义

    可以参考: 转载于:https://www.cnblogs.com/babykick/archive/2011/03/25/1995977.html

  7. Linux学习 - 权限管理命令

    一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod  [(ugoa) ...

  8. VScode 使用 CMake 入门

    参考 CMake 入门实战 在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下: 编写 CMake 配置文件 CMakeLists.txt . 执行命令 cmake PA ...

  9. Element-ui 中对表单进行验证

    Element-ui 中对表单(Form)绑定的对象中的对象属性进行校验 如果是直接绑定属性,是可以的,但是绑定对象中的属性就需要特别处理,需要在rules中添加双引号 " "或者 ...

  10. react-native安卓运行报错:The number of method references in a .dex file cannot exceed 64K.

    错误原因:App里面方法数超过64K解决方法:在android/app/build.gradle中添加implementation 'com.android.support:multidex:1.0. ...