Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何在整数数组中查找最大非重复数?_Algorithm_C++ - Fatal编程技术网

Algorithm 如何在整数数组中查找最大非重复数?

Algorithm 如何在整数数组中查找最大非重复数?,algorithm,c++,Algorithm,C++,假设我有一个未排序的整数数组{3,-1,4,5,-3,2,5},我想找到最大的非重复数(在本例中为4)(5在重复时无效)。我怎样才能做到这一点 按降序排列数组 从顶部元素开始,将其存储为变量,例如max 用max检查下一个元素,如果它们相同,重复直到 您将找到下一个max,否则,您将发现max不重复 号码 时间复杂度:O(nlogn) 实施,基于我的: 为了获得最大的乐趣,一定要以你的(不同的)方法为基础,并相应地加以修改。使用无序图来计算每个元素的频率。(作为一种优化,跟踪遇到的最大元素并跳

假设我有一个未排序的整数数组{3,-1,4,5,-3,2,5},我想找到最大的非重复数(在本例中为4)(5在重复时无效)。我怎样才能做到这一点

  • 按降序排列数组
  • 从顶部元素开始,将其存储为变量,例如
    max
  • max
    检查下一个元素,如果它们相同,重复直到 您将找到下一个
    max
    ,否则,您将发现max不重复 号码
  • 时间复杂度:O(nlogn)

    实施,基于我的:



    为了获得最大的乐趣,一定要以你的(不同的)方法为基础,并相应地加以修改。

    使用无序图来计算每个元素的频率。(作为一种优化,跟踪遇到的最大元素并跳过低于该值的元素。)然后,扫描地图以找出频率正好等于1的最大元素

    template <typename T>  // numeric T
    pair<T, bool> FindMaxNonRepeating(vector<T> const& vec) {
      unordered_map<T, int> elem2freq;
      for (auto const& elem : vec) {
        elem2freq[elem] += 1;
      }
    
      T largest_non_repetitive = std::numeric_limits<T>::min();
      bool found = false;
      for (auto const& item : elem2freq) {
        if (item.first > largest_non_repetitive && item.second == 1) {
          largest_non_repetitive = item.first;
          found = true;
        }
      }
    
      return {largest_non_repetitive, found};
    }
    
    template//numeric T
    配对FindMaxNoRepeating(向量常量和向量){
    无序地图elem2freq;
    用于(自动常量和元素:vec){
    elem2freq[elem]+=1;
    }
    T最大非重复=标准::数值限制::最小();
    bool-found=false;
    用于(自动常数和项目:elem2freq){
    如果(item.first>最大的非重复性和&item.second==1){
    最大非重复项=项目第一;
    发现=真;
    }
    }
    返回{maximust_non_repeative,found};
    }
    

    这需要时间复杂度O(n)和空间复杂度O(n)。

    对数组(O(n log n))进行排序,然后从数组末尾开始迭代,当我看到三个不同的元素(O(n))时,取中间的元素。只要列表中至少有3个条目,这应该可以工作。你试过了吗?它没有正常工作?我很确定它能工作,但以清晰度为代价(必须从外部处理0,1,2数组大小),但更重要的是,它可以在不到O(n log n)的时间内完成。排序数组需要时间O(n log n)到位或O(n)不到位。啊,该死的阿伦,我在吃午饭时就这样想的!当然,这需要更多的空间,正如你提到的,+1。
    C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall -std=c++0x  main.cpp 
    C02QT2UBFVH6-lm:~ gsamaras$ ./a.out 
    Before sorting : 
    3 -1 4 5 -3 2 5 
    
    After sorting : 
    5 5 4 3 2 -1 -3 
    Max non-repeated element: 4
    
    template <typename T>  // numeric T
    pair<T, bool> FindMaxNonRepeating(vector<T> const& vec) {
      unordered_map<T, int> elem2freq;
      for (auto const& elem : vec) {
        elem2freq[elem] += 1;
      }
    
      T largest_non_repetitive = std::numeric_limits<T>::min();
      bool found = false;
      for (auto const& item : elem2freq) {
        if (item.first > largest_non_repetitive && item.second == 1) {
          largest_non_repetitive = item.first;
          found = true;
        }
      }
    
      return {largest_non_repetitive, found};
    }