problemscpp
A collection of my answers to algorithm problems in c++.
函数
acwing::acwing52 命名空间参考

  1. 数组中出现次数超过一半的数字
更多...

函数

int moreThanHalfNum_Solution (vector< int > &nums)
 
 TEST (acwing52, case1)
 

详细描述

  1. 数组中出现次数超过一半的数字

函数说明

◆ moreThanHalfNum_Solution()

int acwing::acwing52::moreThanHalfNum_Solution ( vector< int > &  nums)

在文件 acwing408.cpp1679 行定义.

1679 {
1680 int cnt = 0;
1681 int ans = 0;
1682 for(auto num: nums) {
1683 if(cnt == 0) {
1684 ans = num;
1685 cnt++;
1686 continue;
1687 }
1688 if(num == ans) {
1689 cnt++;
1690 } else {
1691 cnt--;
1692 }
1693 }
1694 return ans;
1695 }

被这些函数引用 TEST().

◆ TEST()

acwing::acwing52::TEST ( acwing52  ,
case1   
)

在文件 acwing408_test.cpp1870 行定义.

1870 {
1871 vector<int> input = {1, 2, 1, 1, 3};
1873 ASSERT_EQ(moreThanHalfNum_Solution(input), 1);
1874 }
int moreThanHalfNum_Solution(vector< int > &nums)
Definition: acwing408.cpp:1679

引用了 moreThanHalfNum_Solution().