力扣刷题总结 -- 数组8

22. 键盘行(简单)

题目要求:

给定一个字符串数组 words ,只返回可以使用英文键盘同一行的字母打印出来的单词。

题目解答:

#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>  // 大小写转换引入头文件


class Solution
{
public:
	vector<string> findWords(vector<string>& words)
	{
		vector<string> ans;
		string rowIdx = "12210111011122000010020202";  // 按照26个字母的顺序列出每个字母在键盘上的行号,从0开始

		for (auto & word : words)
		{
			bool isValid = true;  // 单词中每个字母是否在同一行的标志
			char idx = rowIdx[tolower(word[0]) - 'a'];  // 返回单词中首字母对应rowIdx中的行号,tolower转大写为小写

			for (int i = 1; i < word.size(); i++)  // 遍历单词中的其余字母
			{
				if (rowIdx[tolower(word[i]) - 'a'] != idx)  // 判断其余字母和首字母是否在同一行
				{
					isValid = false;
					break;
				}
			}

			if (isValid)
			{
				ans.emplace_back(word);  // 将单词插入ans容器
			}
		}

		return ans;
	}
};


int main()
{
	vector<string> words = { "Hello", "Alaska", "Dad", "Peace" };
	vector<string> res;

	Solution s;
	res = s.findWords(words);

	for (auto & word : words)
	{
		cout << word << ", ";
	}
	cout << endl;

	for (auto & word : res)
	{
		cout << word << ", ";
	}

	cout << endl;

	system("pause");
	return 0;
}

23. 相对名次(简单)

题目要求:

给定一个长度为n的整数数组score,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都互不相同

运动员将根据得分 决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况:

名次第 1 的运动员获金牌 “Gold Medal” 。
名次第 2 的运动员获银牌 “Silver Medal” 。
名次第 3 的运动员获铜牌 “Bronze Medal” 。
从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 “x”)。
使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。

题目分析:

先排序,后颁奖

题目解答:

#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>


class Solution
{
public:
	vector<string> findRelativeRanks(vector<int>& score)
	{
		int n = score.size();
		string medal[3] = {"Gold Medal", "Silver Medal", "Bronze Medal"};
		vector<pair<int, int>> arr;  // 使用pair结构体存放排序前获奖者编号和对应奖项
		vector<string> ans(n);  // 初始化大小为n的容器

		for (int i = 0; i < n; i++)
		{
			arr.emplace_back(make_pair(-score[i], i));  // score[i]设置为负使得sort从大到小排序
		}

		sort(arr.begin(), arr.end());  // 按照-score[i]进行排序
		
		// 在ans中索引到每个获奖者排序前的相同位置,赋上相应的获奖名称

		for (int i = 0; i < n; i++)
		{
			if (i >= 3)
			{
				ans[arr[i].second] = to_string(i + 1); 
			}
			else
			{
				ans[arr[i].second] = medal[i];
			}
		}

		return ans;
	}

};


int main()
{
	vector<int> score = { 10,3,8,9,4 };
	int n = score.size();
	vector<string> res(n);

	Solution s;
	res = s.findRelativeRanks(score);

	for (auto & medal : res)
	{
		cout << medal << ", ";
	}
	cout << endl;


	system("pause");
	return 0;
}

24. 数组拆分(简单)

题目要求:

给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和最大。

返回该最大总和 。

题目分析:

将数组升序排序后从头到尾分组,每组的第一个数之和即为最大总和。

题目解答:

#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>


class Solution
{
public:
	int arrayPairSum(vector<int>& nums)
	{
		sort(nums.begin(), nums.end());
		int ans = 0;

		for (int i = 0; i < nums.size(); i += 2)
		{
			ans += nums[i];
		}

		return ans;
	}

};


int main()
{
	vector<int> nums = { 6,2,6,5,1,2 };
	Solution s;

	int res = s.arrayPairSum(nums);
	cout << res << endl;

	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值