剑指offer系列21:最小的K个数

从这个题开始,剑指offer进入了第五章,优化时间和时间效率。开始讲代码的优化,到这里就不得不说排序了。这个题可以用快排的思路做。只要看到这个题能联想到用快排做就解出一半了。

 1 #include <iostream>
 2 #include<vector>
 3 using namespace std;
 4 class Solution {
 5 public:
 6     int MoreThanHalfNum_Solution(vector<int> numbers) {
 7         if (numbers.empty())
 8             return 0;
 9         if (numbers.size() == 1)
10             return numbers[0];
11         int high = numbers.size() - 1;
12         int low = 0;
13         int middle = high >> 1;
14         int index = partition(numbers,low,high);
15         while (index != middle)
16         {
17             if (index > middle)
18             {
19                 index = partition(numbers, low, index - 1);
20 
21             }
22             if (index < middle)
23             {
24                 index = partition(numbers, index + 1, high);
25             }
26         }
27         if (WhetherMoreThanHalf(numbers, index))
28             return numbers[index];
29         else
30             return 0;
31 
32     }
33     int partition(vector<int> &vec, int low, int high)//快排的函数,输入一个数组,输出数组的第一个数字(其实是随机取数)在排好序的数组中的下标位置index
34     {
35         if (vec.empty() || low> high|| low < 0 || high>vec.size()-1)
36             return 0;
37         int val = vec[low];
38         while (low < high)
39         {
40             while (low < high&&vec[high] >= vec[low])
41                 high--;//如果高位的数字大,向左靠近
42             vec[low] = vec[high];
43             while (low < high&&vec[low]<=vec[high])
44                 low++;//如果低位的数字小,向右靠近
45             vec[high] = vec[low];
46         }
47         vec[low]=val;
48         return low;
49     }
50     bool WhetherMoreThanHalf(vector<int> vec1, int k)
51     {
52         bool re = true;
53         int len = vec1.size();
54         int count = 0;
55         for (int i = 0; i < len; i++)
56         {
57             if (vec1[i] == vec1[k])
58                 count++;
59         }
60         int half = len >> 1;
61         if (count > half)
62             return true;
63         else
64             return false;
65     }
66 };
67 int main()
68 {
69     Solution so;
70     vector<int> test = { 1,2,3,2,4,2,5,2,3};
71     int result;
72     result=so.MoreThanHalfNum_Solution(test);
73     cout << result << endl;
74     return 0;
75 }

解法二:这个思路是根据这个题目的特点,如果说超过一半的数字,那么其他的数字加起来的结果不会超过它。因此设置一个随机值,并设置它的次数为1.然后循环该数组,如果出现的数字跟预定的一样,就count++,如果不一样就count--。注意不要将count的值变为负值,因为如果是负值就没有意义了。因此当count为0的时候,就设置当前的数字为随机值,重置count为1.这个解法在剑指offer上也有。这个思路比起上一个简单很多,不过不容易想到。

 1 #include <iostream>
 2 #include<vector>
 3 using namespace std;
 4 class Solution {
 5 public:
 6     int MoreThanHalfNum_Solution(vector<int> numbers) {
 7         if (numbers.empty())
 8             return 0;
 9         if (numbers.size() == 1)
10             return numbers[0];
11         int  val = numbers[0], count = 1;
12         for (int i = 1; i < numbers.size(); i++)
13         {
14             if (numbers[i] == val)
15                 count++;
16             else
17             {
18                 /*
19                 if (count == 0)
20                 {
21     
22                 }
23                 else
24                 */
25                     count--;
26             }
27             if (count == 0)
28             {
29                 val = numbers[i];
30                 count = 1;
31             }
32         }
33 
34         if (WhetherMoreThanHalf(numbers,val))
35             return val;
36         else
37             return 0;
38 
39     }
40     bool WhetherMoreThanHalf(vector<int> vec1, int k)
41     {
42         bool re = true;
43         int len = vec1.size();
44         int count = 0;
45         for (int i = 0; i < len; i++)
46         {
47             if (vec1[i] ==k)
48                 count++;
49         }
50         int half = len >> 1;
51         if (count > half)
52             return true;
53         else
54             return false;
55     }
56 };
57 int main()
58 {
59     Solution so;
60     vector<int> test = { 1,2,3,2,2,2,5,4,2};
61     int result;
62     result=so.MoreThanHalfNum_Solution(test);
63     cout << result << endl;
64     return 0;
65 }

昨天好像高考成绩出来了,看到各种新闻上的信息加身边亲戚朋友的关于高考的信息。突然想起来我高考的那一年,其实人生的各种困难跟高考差不多,你以自己的视角来看似乎变数很大,但是宏观看来变数其实很小。平时学习好的高考考差的可能性很小,平时考差的考好的可能性也很小。所以平淡心就好,尽力就好,因为高考的成绩在没考的时候也许就已经见分晓。

原文地址:https://www.cnblogs.com/neverland0718/p/11088621.html