zoukankan      html  css  js  c++  java
  • 数组中出现次数超过一半的数字

    数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

    例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。

    如果不存在则输出0。

    思路一:    先排序,排好序之后所有相同的数都在一起了,统计相同的数的个数,并判断个数是吧超过一半

    时间复杂度O(nlogn)不是最好的

    代码:

    class Solution {
    public:
        // 先排序,再用计数器统计相同的元素
        int MoreThanHalfNum_Solution(vector<int> numbers) {
    
    
            int len = numbers.size();
            if (len <= 0)
                return 0;
            else if (len == 1)
                return numbers[0];
            // 时间复杂度是o(nlogn)并不是最优的
            sort(numbers.begin(), numbers.end());
            int count = 1;
            for(int i = 0; i < len-1; i++)
            {
                if (numbers[i] == numbers[i+1])
                {
                    count++;
                }
                else
                    count = 1;
                if (count > len/2)
                    return numbers[i];
            }
            return 0;
        }
    }

    思路二: 时间复杂度是O(n)
    如果有符合条件的数字,那么它出现的次数要比其他数字出现的和还要多
    所以遍历数组时保存两个值,一个是key为数组中的数字,另一个是times出现的次数
    当我们遍历数组时如果下一个数字和之前保存的key值相同则次数加一,
    不同时次数减一,当次数为0时,保存当前数字的值,count为1
    遍历结束后,保存的数字就是所求的

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 class Solution {
     7 public:
     8 
     9 //     思路二: 时间复杂度是O(n)
    10 //      如果有符合条件的数字,那么它出现的次数要比其他数字出现的和还要多
    11 //     所以遍历数组时保存两个值,一个是key为数组中的数字,另一个是times出现的次数
    12 //     当我们遍历数组时如果下一个数字和之前保存的key值相同则次数加一,
    13 //     不同时次数减一,当次数为0时,保存当前数字的值,count为1
    14 //     遍历结束后,保存的数字就是所求的
    15 
    16         int MoreThanHalfNum_Solution(vector<int> numbers) {
    17         int len = numbers.size();
    18         if (len <= 0)
    19             return 0;
    20         int key = numbers[0];
    21         int times = 1;
    22         for(int i = 1; i < len; ++i)
    23         {
    24             if (key == numbers[i])
    25                 ++times;
    26             else
    27                 --times;
    28             if (times == 0)
    29             {
    30                 key = numbers[i];
    31                 times = 1;
    32             }
    33         }
    34         int count = 0;  // 统计key出现的次数
    35         for (int i = 0; i < len; i++)
    36         {
    37             if(key == numbers[i])
    38                 count++;
    39         }
    40         if (count > len/2)
    41             return key;
    42         else
    43             return 0;
    44     }
    45 };
    46 int main()
    47 {
    48     Solution s;
    49     vector<int> vec ={1,2,3,2,2};
    50     int t = s.MoreThanHalfNum_Solution(vec);
    51     cout << t;
    52     return 0;
    53 }
  • 相关阅读:
    如何增强Linux和Unix服务器系统安全性
    FTP连接不上的解决方法
    PHP获取当前服务器详细信息
    要想提高电脑开机速度首先需要设置这几个功能
    Centos7下安装iptables防火墙
    centos下LVM配置与管理
    基于LNMP环境的ssh2扩展
    60个开发者不容错过的免费资源库
    MySql命令的基本操作
    存储过程 分页【NOT IN】和【>】效率大PK 千万级别数据测试结果
  • 原文地址:https://www.cnblogs.com/xiaokang01/p/12442525.html
Copyright © 2011-2022 走看看