http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/

这一题如果没空间要求就没那么麻烦了

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. #include <string>
  7. #include <fstream>
  8. #include <map>
  9. using namespace std;
  10.  
  11. void more(int arr[], int n, int k) {
  12. map<int, int> S;
  13. for (int i = ; i < n; i++) {
  14. if (S.find(arr[i]) != S.end()) S[arr[i]]++;
  15. else if (S.size() == k) {
  16. for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) {
  17. if (it->second == ) S.erase(it->first);
  18. else it->second--;
  19. }
  20. }
  21. else S[arr[i]] = ;
  22. }
  23. for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) {
  24. int tmp = ;
  25. for (int i = ; i < n; i++) {
  26. if (arr[i] == it->first) tmp++;
  27. }
  28. if (tmp > n / k) cout << it->first << " ";
  29. }
  30. }
  31.  
  32. int main() {
  33. int arr[] = {, , , , , , };
  34. more(arr, , );
  35. return ;
  36. }

Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times的更多相关文章

  1. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  2. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

  4. 面试总结之数据结构(Data Structure)

    常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...

  5. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  6. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  7. sklearn中报错ValueError: Expected 2D array, got 1D array instead:

    from sklearn.linear_model import LinearRegression lr = LinearRegression() print(tr_x.shape,tr_y.shap ...

  8. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

  9. [已解决]报错:ValueError: Expected 2D array, got scalar array instead

    报错代码: new_x = 84610 pre_y = model.predict(new_x) print(pre_y) 报错结果: ValueError: Expected 2D array, g ...

随机推荐

  1. 企业级监控工具Cacti安装配置全过程

      Cacti 在英文中的意思是仙人掌的意思,Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具.它通过 snmpget来获取数据,使用 RRDtool绘画 ...

  2. c语言字符串赋值

    char *p="asdf";能运行 定义一个字符指针,并用它指向常量字符串"asdf"的首地址 char *p;p="asdf"; 能运行 ...

  3. ”ftp使用dos命令“

    ftp不能使用dos命令,ftp有专用的命令. 在批处理文件中,如果用到dos命令获取信息(比如:系统日期),将用获取的信息,输出到ftp脚本文件中,然后执行ftp脚本文件. set yyyy=%DA ...

  4. group by having和connect by

    --使用group by 子句对数据进行分组:对group by 子句形成的组运行聚集函数计算每一组的值:最后用having 子句去掉不符合条件的组.--having 子句中的每一个元素也必须出现在s ...

  5. VBA小功能集合-判断列内是否有重复值

    1.判断列内是否有重复值: Dim arrT As Range Dim rng As Range Set arrT = Range("A:A")'判读A列单元格 For Each ...

  6. Atitit.struts排除url 的设计and 原理 自定义filter 排除特定url

    Atitit.struts排除url 的设计and 原理 自定义filter 排除特定url 1.1. 原理流程1 2. Invoke1 3. StrutsX2 1.1. 原理流程 读取struts配 ...

  7. 专用于高并发的map类-----Map的并发处理(ConcurrentHashMap)

    oncurrentModificationException 在这种迭代方式中,当iterator被创建后集合再发生改变就不再是抛出ConcurrentModificationException, 取 ...

  8. hadoop生态系统学习之路(八)hbase与hive的数据同步以及hive与impala的数据同步

    在之前的博文中提到,hive的表数据是能够同步到impala中去的. 一般impala是提供实时查询操作的,像比較耗时的入库操作我们能够使用hive.然后再将数据同步到impala中.另外,我们也能够 ...

  9. find 多文件查找需要单引号

    [root@db01 local]# find  -name '*.com'|xargs egrep "qq"./tt.com:qq[root@db01 local]# find  ...

  10. 【Mac系统 + Python + Django】之开发一个发布会系统【Django视图(二)】

    此学习资料是通过虫师的python接口自动化出的书学习而来的,在此说明一下,想学习更多的自动化的同学可以找虫师的博客园,非广告,因为我python+selenium自动化也是跟虫师学的,学习效果很好的 ...