funplus客户端笔试复盘-编程第一题

题目:给定一个数组,求出数组中出现次数最多的k个元素(出现次数相同,则按出现的先后顺序)

笔试时间不够,笔完稍微复盘下

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>

using namespace std;

//定义数据结构
struct Node
{
	int index;
	int value;
	int count;

//重载比较符号
public:
	bool operator==(const Node& other)
	{
		return this->value==other.value;
	}
};

//重载sort排序方法
static bool Compare(const Node& a, const Node& b)
{
	//先按次数
	if (a.value != b.value)
	{
		return a.count>b.count;
	}
	//再按顺序
	else
	{
		return a.index<b.index;
	}
}

vector<int> GetMaxCountsValue(vector<int>& nums, int k)
{
	vector<int> res(k,0);
	vector<Node> temp;
	unordered_map<int,int> st;//查询map
	int order=0;
	for (int i = 0; i < nums.size(); i++)
	{
		if (st.find(nums[i]) == st.end())
		{
			st.insert({nums[i],order});
			Node var;
			var.index=order;
			var.value=nums[i];
			var.count=1;
			order++;
			temp.push_back(var);
		}
		else
		{
			temp[st[nums[i]]].count++;
		}
	}
	sort(temp.begin(),temp.end(),Compare);
	for (int i = 0; i < k&&i<temp.size(); i++)
	{
		res.push_back(temp[i].value);
		cout<<temp[i].value<<" ";
	}
	return res;
}

int main()
{
	vector<int> test{2,2,3,1,3,2,1,4,4,4,5,5,5,6};
	vector<int> finalRes=GetMaxCountsValue(test,3);
	return 0;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Python绘制一个简单的表格,并统计2011年到2022年英雄联盟全球总决赛的历届冠军队名、队员游戏ID和FMVP的示例代码: ```python import pandas as pd # 创建数据 data = { '年份': [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022], '冠军队名': ['Fnatic', 'Taipei Assassins', 'SK Telecom T1', 'Samsung White', 'SK Telecom T1', 'SK Telecom T1', 'Samsung Galaxy', 'Invictus Gaming', 'FunPlus Phoenix', 'DAMWON Gaming', 'DAMWON Gaming', 'TBD'], '上单': ['sOAZ', 'Stanley', 'Impact', 'Looper', 'MaRin', 'Duke', 'CuVee', 'TheShy', 'GimGoon', 'Nuguri', 'Nuguri', '-'], '打野': ['Cyanide', 'Lilballz', 'Bengi', 'Dandy', 'bengi', 'Blank', 'Ambition', 'Ning', 'Tian', 'Canyon', 'Canyon', '-'], '中单': ['xPeke', 'Toyz', 'Faker', 'PawN', 'Faker', 'Faker', 'Crown', 'Rookie', 'Doinb', 'ShowMaker', 'ShowMaker', '-'], 'ADC': ['Lamia', 'bebe', 'Piglet', 'Imp', 'Bang', 'Bang', 'Ruler', 'JackeyLove', 'Lwx', 'Ghost', 'Ghost', '-'], '辅助': ['Shushei', 'Mistake', 'PoohManDu', 'Mata', 'Wolf', 'Wolf', 'CoreJJ', 'Baolan', 'Crisp', 'BeryL', 'BeryL', '-'], 'FMVP': ['-', '-', 'Faker', 'Looper', 'Faker', 'Faker', 'Ruler', 'TheShy', 'Doinb', 'Canyon', 'Ghost', '-'] } # 创建DataFrame df = pd.DataFrame(data) # 打印表格 print(df) ``` 运行以上代码,你将会得到一个包含年份、冠军队名、上单、打野、中单、ADC、辅助和FMVP的表格,其中 '-' 表示该信息暂未公布。你可以根据实际情况进行修改和完善。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值