LeetCode 137. 只出现一次的数字 II 中等

题目 - 点击直达

  • [1. 137. 只出现一次的数字 II 中等](#1. 137. 只出现一次的数字 II 中等)
    • [1. 题目详情](#1. 题目详情)
      • [1. 原题链接](#1. 原题链接)
      • [2. 题目要求](#2. 题目要求)
      • [3. 基础框架](#3. 基础框架)
    • [2. 解题思路](#2. 解题思路)
      • [1. 思路分析](#1. 思路分析)
      • [2. 时间复杂度](#2. 时间复杂度)
      • [3. 代码实现](#3. 代码实现)

1. 137. 只出现一次的数字 II 中等

1. 题目详情

1. 原题链接

LeetCode 137. 只出现一次的数字 II 中等

2. 题目要求

给你一个整数数组 n u m s nums nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。

你必须设计并实现线性时间复杂度的算法且使用常数级空间来解决此问题。

示例 1:

输入:nums = 2,2,3,2

输出:3
示例 2:

输入:nums = 0,1,0,1,0,1,99

输出:99

提示:
1 < = n u m s . l e n g t h < = 3 ∗ 104 1 <= nums.length <= 3 * 104 1<=nums.length<=3∗104
− 231 < = n u m s i < = 231 − 1 -231 <= numsi <= 231 - 1 −231<=numsi<=231−1
n u m s nums nums 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次

3. 基础框架

● Cpp代码框架

cpp 复制代码
class Solution {
public:
    int singleNumber(vector<int>& nums) {
    }
};

2. 解题思路

1. 思路分析

( 1 ) (1) (1) 哈希映射的思想,把 n u m s nums nums数组中出现的数唯一映射到 u n o r d e r e d m a p < i n t , i n t > unordered_map<int, int> unorderedmap<int,int>中,第二个参数记录其出现的次数;
( 2 ) (2) (2) 遍历 n u m s nums nums数组,并记录每个元素出现的次数;
( 3 ) (3) (3) 再次遍历 n u m s nums nums数组,判断每个元素在 u n o r d e r e d m a p < i n t , i n t > unordered_map<int, int> unorderedmap<int,int>中出现的次数:

  • 如果元素出现的次数是1,则返回该元素;
  • 反之则继续判断下一个元素,直到 n u m s nums nums末尾为止。

2. 时间复杂度

O ( N ) O(N) O(N)

遍历了两遍数组 n u m s nums nums,每次循环内进行的操作都是常数次,总体是 2 ∗ N 2*N 2∗N

3. 代码实现

哈希映射

cpp 复制代码
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        // 哈希映射
        unordered_map<int, int> m;
        for(auto& e : nums){
            m[e]++;
        }
        for(auto& e : nums){
            if(m[e] == 1){
                return e;
            }
        }
        return -1;
    }
};

暴力模拟

cpp 复制代码
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int one = 0, two = 1, three = 2;
        while(one < nums.size()){
            if(one == nums.size() - 1 
            || nums[one] != nums[two]){
                return nums[one];
            }
            one += 3;
            two += 3;
            three += 3;
        }
        return -1;
    }
};

通用模拟计数

cpp 复制代码
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int first = 0;
        int second = 0;
        int len = 0;
        while(second < nums.size()){
            if(nums[first] != nums[second]){
                if(len == 1){
                    break;
                }
                len = 0;
                first = second;
            }
            second++;
            len++;
        }
        return nums[first];
    }
};

T h e The The E n d End End

相关推荐
Yyyyyy~10 小时前
[c++]数组
c++
知识分享小能手11 小时前
C++ 学习教程,从入门到精通,C++内存模型和名称空间 — 详细知识点总结(9)
开发语言·c++·学习
sogw-三叶草️11 小时前
LeetCode 989. 数组形式的整数加法
数据结构·算法
2601_9622186111 小时前
c++中多重继承与虚继承的实现
开发语言·c++
沫璃染墨11 小时前
《Qt从零入门系列(十一):Qt事件机制详解——从QEvent到鼠标、键盘与定时器事件》
c++·qt·ui·硬件工程·交互·个人开发·qt5
维克兜率天11 小时前
【维克】均值回归:跌多了会涨,涨多了会跌
开发语言·笔记·python·算法·均值算法·回归·量化
2601_9622186111 小时前
C++中分配器allocator的实现
开发语言·c++
yi01111 小时前
DAY 14: LeetCode 394. 字符串解码|递归和栈到底怎么处理嵌套?
数据结构·笔记·python·算法·leetcode
P.D.Wei12 小时前
[Leetcode 1614] 括号的最大嵌套深度
算法·leetcode·职场和发展
单片机设计·······12 小时前
【dz-1162】汽车前玻璃雨刮器设计
c++·单片机·嵌入式硬件