力扣刷题总结 -- 数组17

49. 按奇偶排序数组(简单)

题目要求:

给定一个整数数组nums,将nums中的的所有偶数元素移动到数组的前面,后跟所有奇数元素。

返回满足此条件的任一数组作为答案。

题目解答:

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


class Solution
{
public:
	vector<int> sortArrayByParity(vector<int>& nums)
	{
		int n = nums.size();
		vector<int> res(n);  // 新建相同大小的数组

		int left = 0, right = n - 1;  // 获取数组左端和右端的指针

		for (auto & num : nums)
		{
			if (num % 2 == 0)
			{
				res[left++] = num;  // 偶数放左边
			}
			else
			{
				res[right--] = num;  // 奇数放右边
			}
		}

		return res;
	}

};


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

	cout << "原数组为:";
	for (auto & num : nums)
	{
		cout << num << ", ";
	}
	cout << endl;

	vector<int>res = s.sortArrayByParity(nums);

	cout << "按奇偶排序后为:";
	for (auto & num : res)
	{
		cout << num << ", ";
	}
	cout << endl;

	system("pause");
	return 0;
}

50. 按奇偶排序数组II(简单)

题目要求:

给定一个非负整数数组numsnums中一半整数是奇数,一半整数是偶数

对数组进行排序,以便当 nums[i] 为奇数时,i 也是奇数;当 nums[i] 为偶数时, i 也是偶数

可以返回任何满足上述条件的数组作为答案

题目解答:

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


class Solution
{
public:
	vector<int> sortArrayByParityII(vector<int>& nums)
	{
		int n = nums.size();
		vector<int> ans(n);

		int i = 0;
		for (int x : nums)  
		{
			if (x % 2 == 0)  // 当x为偶数时,放在偶数位
			{
				ans[i] = x;
				i += 2;
			}
		}

		i = 1;
		for (int x : nums)
		{
			if (x % 2 == 1)  // 当x为偶数时,放在奇数位
			{
				ans[i] = x;
				i += 2;
			}
		}

		return ans;
	}

	vector<int> sortArrayByParityII_2(vector<int>& nums)
	{
		int n = nums.size();
		int j = 1;

		for (int i = 0; i < n; i += 2)
		{
			if (nums[i] % 2 == 1)  // 当偶数位元素为奇数时进入循环
			{
				while (nums[j] % 2 == 1)  // 在奇数位找到偶数则标记下标
				{
					j += 2;
				}

				swap(nums[i], nums[j]);  // 交换两个元素
			}
		}

		return nums;
	}

};


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

	cout << "原数组为:";
	for (auto & num : nums)
	{
		cout << num << ", ";
	}
	cout << endl;

	vector<int> res = s.sortArrayByParityII_2(nums);

	cout << "排序后数组为:";
	for (auto & num : res)
	{
		cout << num << ", ";
	}
	cout << endl;

	system("pause");
	return 0;
}

51. 独特的电子邮件地址(简单)

题目要求:

每个 有效电子邮件地址 都由一个本地名和一个域名组成,以 ‘@’ 符号分隔。除小写字母之外,电子邮件地址还可以含有一个或多个'.''+'

例如,在 alice@leetcode.com中, alice 是 本地名 ,而 leetcode.com 是 域名 。
如果在电子邮件地址的 本地名 部分中的某些字符之间添加句点(‘.’),则发往那里的邮件将会转发到本地名中没有点的同一地址。请注意,此规则不适用于域名

例如,"alice.z@leetcode.com” 和 “alicez@leetcode.com” 会转发到同一电子邮件地址。
如果在 本地名 中添加加号(‘+’),则会忽略第一个加号后面的所有内容。这允许过滤某些电子邮件。同样,此规则不适用于域名

例如 m.y+name@email.com 将转发到 my@email.com。
可以同时使用这两个规则。

给定一个字符串数组 emails,我们会向每个 emails[i] 发送一封电子邮件。返回实际收到邮件的不同地址数目。

题目分析:

相当于域名中有任何的标点符号都无所谓,本地名中有两种情况,出现’+‘时’+‘后面的元素全部忽略,出现’.‘时忽略’.'。

题目解答:

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


class Solution
{
public:
	unordered_set<string> numUniqueEmails(vector<string>& emails)
	{
		unordered_set<string> emailSet;  // 初始化容器用于存放邮件地址

		for (auto &email : emails)
		{
			string local;  // 初始化存放邮件地址的字符串

			for (char c : email)
			{
				if (c == '+' || c == '@')  // 如果地址中存在字符'+'或者'@',跳出循环
				{
					break;
				}
				if (c != '.')  // 如果地址中存在字符'.',直接忽略
				{
					local += c;
				}
			}

			emailSet.emplace(local + email.substr(email.find('@')));  // 将筛选后的地址放入set容器中
		}

		return emailSet;
	}
};


int main()
{
	vector<string>emails = { "test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com" };
	Solution s;

	cout << "所有email地址展示如下:" << endl;
	for (auto &email : emails)
	{
		cout << email << endl;
	}

	cout << "========================" << endl;

	unordered_set<string> res = s.numUniqueEmails(emails);
	cout << "有效且不重复的地址有" << res.size() << "个,分别为:" << endl;
	for (auto &email : res)
	{
		cout << email << endl;
	}


	system("pause");
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值