zoukankan      html  css  js  c++  java
  • 从长度为n的整形数组中返回m个最大数(不许使用排序)

     1 public static int[] GetTheManyMaxValues(int[] arr, int number)
     2 {
     3     if (arr == null || number <= 0 || arr.Length < number) return null;
     4     List<int> lst = new List<int>();
     5     for (int i = 0; i < number; i++)
     6     {
     7         lst.Add(arr[i]);
     8     }
     9     for (int i = number; i < arr.Length; i++)
    10     {
    11         int min = GetTheMinValue(lst);
    12         if (min < arr[i])
    13         {
    14             lst.Remove(min);
    15             lst.Add(arr[i]);
    16         }
    17     }
    18     return lst.ToArray();
    19 }
     1 public static int GetTheMinValue(List<int> lst)
     2 {
     3     int min = lst[0];
     4     foreach (int number in lst)
     5     {
     6         if (min > number)
     7         {
     8             min = number;
     9         }
    10     }
    11     return min;
    12 }
  • 相关阅读:
    rabbitmq入门
    php7.2 安装redis扩展
    php安装扩展的几种方法
    yum安装php7.2
    相关报错
    [枚举]P1089 津津的储蓄计划
    [DFS]排列的生成
    [枚举]P1085 不高兴的津津
    [模拟]P1047 校门外的树
    [模拟]P1046 陶陶摘苹果
  • 原文地址:https://www.cnblogs.com/panchunting/p/interview_tech_algorithm.html
Copyright © 2011-2022 走看看