力扣刷题总结 -- 数组32

94. 一维数组转变成二维数组(简单)

题目要求:

给定一个下标从 0 开始的一维整数数组 original 和两个整数 m 和 n 。使用 original 中所有元素创建一个 m 行 n 列的二维数组。

original 中下标从 0 到 n - 1 (都 包含 )的元素构成二维数组的第一行,下标从 n 到 2 * n - 1 (都 包含 )的元素构成二维数组的第二行,依此类推。

请返回一个 m x n 的二维数组。如果无法构成这样的二维数组,请返回一个空的二维数组。

题目分析:

首先计算m和n的乘积是否等于original的大小,如果不等,直接返回空数组。接着遍历original,每次取n个元素插入结果数组的末尾,以此类推。

题目解答:

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


class Solution
{
public:
	vector<vector<int>> construct2DArray(vector<int>& original, int m, int n)
	{
		vector<vector<int>> ans;
		if (original.size() != m * n)  // 如果original的大小不等于m x n,则不可能转换成对应大小的二维数组
		{
			return ans;
		}

		for (auto it = original.begin(); it != original.end(); it += n)
		{
			ans.emplace_back(it, it + n);  // 遍历数组中的所有元素,每次取n个元素插入ans的末尾,作为ans的一个元素
		}

		return ans;
	}

};


int main()
{
	vector<int> original = { 1,2,3,4 };
	int m = 2, n = 2;

	cout << "原一维数组为:";
	for (int e : original)
	{
		cout << e << ", ";
	}
	cout << endl;

	cout << "需要将该一维数组转换为" << m << "行 " << n << "列的二维数组" << endl;

	Solution s;
	vector<vector<int>> res = s.construct2DArray(original, m, n);

	cout << "转换后的二维数组为:" << endl;
	for (auto it = res.begin(); it != res.end(); it++)
	{
		for (auto vit = (*it).begin(); vit != (*it).end(); vit++)
		{
			cout << *vit << ", ";
		}
		cout << endl;
	}
	cout << endl;

	system("pause");
	return 0;
}

95. 基于排列构建数组(简单)

题目要求:

给定一个 从 0 开始的排列 nums(下标也从 0 开始)。请你构建一个同样长度的数组 ans ,其中,对于每个 i(0 <= i < nums.length),都满足 ans[i] = nums[nums[i]] 。返回构建好的数组 ans 。

从 0 开始的排列 nums 是一个由 0 到nums.length - 1(0 和 nums.length - 1 也包含在内)的不同整数组成的数组。

题目解答:

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


class Solution
{
public:
	vector<int> buildArray(vector<int>& nums)
	{
		int n = nums.size();
		vector<int> ans;
		for (int i = 0; i < n; i++)
		{
			ans.push_back(nums[nums[i]]);
		}

		return ans;
	}

};


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

	cout << "原一维数组为:";
	for (int e : nums)
	{
		cout << e << ", ";
	}
	cout << endl;

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

	cout << "基于排序构建的数组为:";
	for (int e : res)
	{
		cout << e << ", ";
	}
	cout << endl;

	system("pause");
	return 0;
}

96. 使数组中所有元素等于0(简单)

题目要求:

给定一个非负整数数组nums。在一步操作中,你必须:
选出一个正整数x,x 需要小于或等于nums 中最小非零元素。
nums 中的每个正整数都减去 x。
返回使 nums 中所有元素都等于 0 需要的最少操作数。

题目分析:

由于每次操作都将数组中的所有非零元素减少一个相同的值,因此数组中的相等元素减少到 0 的操作数相等,不相等元素减少到 0 的操作数不相等。
由于每次操作都会将当前数组中最小的非零元素减少到0,最少的操作数就是数组中非零元素的个数

题目解答:

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


class Solution
{
public:
	int minimumOperations(vector<int>& nums)
	{
		unordered_set<int> hashSet;
		for (int num : nums)
		{
			if (num > 0)
			{
				hashSet.emplace(num);  // nums中非零元素的个数就是最小操作数
			}
		}

		return hashSet.size();
	}
};


int main()
{
	vector<int> nums = { 1,5,0,3,5 };

	cout << "原一维数组为:";
	for (int e : nums)
	{
		cout << e << ", ";
	}
	cout << endl;

	Solution s;
	int res = s.minimumOperations(nums);
	cout << "使nums中所有元素减少到0的最小操作数为:" << res << endl;

	system("pause");
	return 0;
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值