题目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 &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
#include
using namespace std;
//输出容器内所有值
void printf_vector(vector &A)
{auto v&#61;A;auto it&#61;v.begin();while (it !&#61; v.end()){cout<<*it<<" ";it&#43;&#43;;}cout<}
/*在容器v中,从a处开始&#xff0c;b处结束&#xff0c;寻找c&#xff0c;最后返回c的位置*/
vector::iterator find_vector(vector &A, vector::iterator a, vector::iterator b, int c)
{auto it&#61;a;auto v&#61;A;while (it !&#61; b){if(*it&#61;&#61;c)return it;it&#43;&#43;;}return it;//没找到&#xff0c;返回A.endl()
}
int solution(vector &A)
{vector v&#61;A;auto it&#61;v.begin();while (it !&#61; v.end()){//printf_vector(v);//auto it_temp&#61;find(it&#43;1,v.end(),*it);//vector自带的find函数auto it_temp&#61;find_vector(v,it&#43;1,v.end(),*it);//自定义的find函数if(it_temp&#61;&#61;v.end())return *it;elsev.erase(it_temp); it&#43;&#43;;}return -1;
}
int main()
{
vector v&#61;{9,3,9,3,9,7,7,10,9};
cout<getchar();
}
程序虽然能得出正确结&#xff0c;但是当容器内容很大时&#xff0c;效率比较低&#xff0c;还有很大的优化空间&#xff0c;如果有大神有更好的解决办法&#xff0c;望告知&#xff0c;万分感谢&#xff01;&#xff01;
题目2&#xff1a;
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 &#61; [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 solution(vector &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 &#61; [3, 8, 9, 7, 6] K &#61; 3the 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 &#61; [0, 0, 0] K &#61; 1the function should return [0, 0, 0]
Given
A &#61; [1, 2, 3, 4] K &#61; 4the 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.
程序&#xff1a;
#include
#include
using namespace std;//输出容器内所有值
void printf_vector(vector &A)
{auto v &#61; A;auto it &#61; v.begin();while (it !&#61; v.end()){cout <<*it <<" ";it&#43;&#43;;}cout <}vector solution1(vector &A, int K)//此处K<&#61;N
{auto v &#61; A;auto count &#61; K;vector v1;vector v2;while (count){v1.push_back(*(v.end() - count));//把容器v的最后K个元素保存到容器v1count--;}count &#61; K;auto it &#61; v1.begin();/*将在v1的元素&#xff08;即v的后K个元素&#xff09;保存进v2*/while (it !&#61; v1.end()){v2.push_back(*it);it&#43;&#43;;}/*容器v中的前面要右移的元素保存进v2*/it &#61; v.begin();while (it !&#61; v.end() - K){v2.push_back(*it);it&#43;&#43;;}return v2;
}vector solution(vector &A, int K)
{ if(A.empty()) return A;else if((K % (A.end() - A.begin()))&#61;&#61;0) return A; //K刚好为容器大小的整数倍else {auto v&#61;solution1(A,K%(A.end() - A.begin()));return v;}
}
int main()
{vector v&#61;{ 1,2,3,1};int K&#61;5;//printf_vector(v);vector v1 &#61; solution(v, K);
// cout <<"right shift " <}