C++训练2

题目1:

给定一个非空数组 A,包含有 N 个整数,起始下标为 0。数组包含有奇数个元素,其中除了唯一一个元素之外,其他每个元素都可以与数组中另一个有相同值的元素配对。

比如,在下面这个数组中:

A[0] = 9 A[1] = 3 A[2] = 9A[3] = 3 A[4] = 9 A[5] = 7A[6] = 9
  • 下标为 0 和 2 的元素的值是 9
  • 下标为 1 和 3 的元素的值是 3
  • 下标为 4 和 6 的元素的值是 9
  • 下标为 5 的元素的值是 7,无法配对

写一个函数:

int solution(vector<int> &A);

对满足上述条件的数组 A,返回数组中无法配对的元素的值。

假定:

  • N is an odd integer within the range [1..1,000,000];
  • 数组 A 每个元素是取值范围 [1..1,000,000,000] 内的 整数 ;
  • all but one of the values in A occur an even number of times.

比如,给定以下数组:

A[0] = 9 A[1] = 3 A[2] = 9A[3] = 3 A[4] = 9 A[5] = 7A[6] = 9

函数应该返回 7,如上述解释。

复杂度:

  • 最坏-情况下,期望的时间复杂度是 O(N);
  • 最坏-情况下,期望的空间复杂度是 O(1), 输入存储除外 (不计输入参数所需的存储空间).

程序:

#include<iostream>
#include<vector>
using namespace std;
//输出容器内所有值
void printf_vector(vector<int> &A)
{
	auto v=A;
	auto it=v.begin();
	while (it != v.end())
	{
		cout<<*it<<" ";
		it++;
	}
	cout<<endl;
}
/*在容器v中,从a处开始,b处结束,寻找c,最后返回c的位置*/
vector<int>::iterator find_vector(vector<int> &A, vector<int>::iterator a, vector<int>::iterator b, int c)
{
	auto it=a;
	auto v=A;
	while (it != b)
	{
		if(*it==c)
			return it;
		it++;
	}
	return it;//没找到,返回A.endl()
}
int solution(vector<int> &A) 
{
	vector<int> v=A;
	auto it=v.begin();
	while (it != v.end())
	{
		//printf_vector(v);
		//auto it_temp=find(it+1,v.end(),*it);//vector自带的find函数
		auto it_temp=find_vector(v,it+1,v.end(),*it);//自定义的find函数
		if(it_temp==v.end())
			return *it;
			else
			v.erase(it_temp);	
		it++;
	}
	return -1;
}
int main()
{
vector<int> v={9,3,9,3,9,7,7,10,9};
cout<<solution(v)<<endl;
getchar();
}

程序虽然能得出正确结,但是当容器内容很大时,效率比较低,还有很大的优化空间,如果有大神有更好的解决办法,望告知,万分感谢!!

题目2:

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

vector<int> solution(vector<int> &A, int K);

that, given a zero-indexed array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

A = [3, 8, 9, 7, 6] K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

For another example, given

A = [0, 0, 0] K = 1

the function should return [0, 0, 0]

Given

A = [1, 2, 3, 4] K = 4

the function should return [1, 2, 3, 4]

Assume that:

  • N and K are integers within the range [0..100];
  • each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

程序:

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

//输出容器内所有值
void printf_vector(vector<int> &A)
{
	auto v = A;
	auto it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

vector<int> solution1(vector<int> &A, int K)//此处K<=N
{
	auto v = A;
	auto count = K;
	vector<int> v1;
	vector<int> v2;
	while (count)
	{
		v1.push_back(*(v.end() - count));//把容器v的最后K个元素保存到容器v1
		count--;
	}
	count = K;
	auto it = v1.begin();
	/*将在v1的元素(即v的后K个元素)保存进v2*/
	while (it != v1.end())
	{
		v2.push_back(*it);
		it++;
	}
	/*容器v中的前面要右移的元素保存进v2*/
	it = v.begin();
	while (it != v.end() - K)
	{
		v2.push_back(*it);
		it++;
	}
	return v2;
}


vector<int> solution(vector<int> &A, int K) 
{	
	if(A.empty())  return A;
	else if((K % (A.end() - A.begin()))==0)  return  A; //K刚好为容器大小的整数倍
	else {
	auto v=solution1(A,K%(A.end() - A.begin()));
	return v;}
}
int main()
{
	vector<int> v={ 1,2,3,1};
	int K=5;
	
	//printf_vector(v);
	vector<int> v1 = solution(v, K);
//	cout << "right shift " << K << endl;
	printf_vector(v1);
	getchar();
}

猜你喜欢

转载自blog.csdn.net/weixin_37058227/article/details/79336971