当前位置: 首页 > news >正文

杭州企业网站建设 哪里好计算机培训短期速成班

杭州企业网站建设 哪里好,计算机培训短期速成班,深圳福田专业网站建设,苏州网站建设设计公司哪家好交换和: 给定两个整数数组,请交换一对数值(每个数组中取一个数值),使得两个数组所有元素的和相等。 返回一个数组,第一个元素是第一个数组中要交换的元素,第二个元素是第二个数组中要交换的元…

交换和:

给定两个整数数组,请交换一对数值(每个数组中取一个数值),使得两个数组所有元素的和相等。

返回一个数组,第一个元素是第一个数组中要交换的元素,第二个元素是第二个数组中要交换的元素。若有多个答案,返回任意一个均可。若无满足条件的数值,返回空数组。

示例:

输入: array1 = [4, 1, 2, 1, 1, 2], array2 = [3, 6, 3, 3]
输出: [1, 3]
输入: array1 = [1, 2, 3], array2 = [4, 5, 6]
输出: []

解题思路:

1.首先,先对题目进行理解,交换两个数,使得两个数组的和相等,那么就需要分别把两个数组的和计算出来,如果sum1和sum2的差值为偶数,才有可能可以找到能够交换的数。如果差值为奇数,直接返回空数组。

2.sum1和sum2的差值为偶数时,diff=(sum1-sum2)/2,存在:

sum1-array1[i]+array2[j] = sum2-array2[j]+array1[i]

array1[i]+diff=array2[j]

此时可以满足交换之后,sum1=sum2

方法一:排序+二分查找法

Code:

class Solution {
public:vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {sort(array2.begin(), array2.end());int sum1 = 0, sum2 = 0, m = array1.size(), n = array2.size();for (int & num : array1) sum1 += num;//计算sum1for (int & num : array2) sum2 += num;//计算sum2//如果差值为奇数,不会存在两个数符合题意if ((sum2 -sum1) % 2) return {}; // 两个数组的差值必须是偶数int diff = (sum2 - sum1) / 2;	// 需满足 y = x + diff;for (int i = 0;i < m; i++) {int target = array1[i] + diff;int left = 0, right = n - 1, mid;//对数组2进行二分查找法while (left <= right) {mid = (right + left) >> 1;//找到目标元素if (array2[mid] == target) {return {array1[i], array2[mid]};}else if (array2[mid] > target) right = mid - 1;else left = mid + 1;}}return {};	// 没有找到符合题意的两个数}
};

方法二:排序+双指针

class Solution {
public:vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {sort(array1.begin(), array1.end());     // 对array1排序sort(array2.begin(), array2.end());     // 对array2排序int sum1 = 0, sum2 = 0;for (int num : array1) sum1 += num;   // 数组1求和for (int num : array2) sum2 += num;   // 数组2求和if ((sum1 -sum2) % 2) return {}; // 两个数组的差值必须是偶数int diff = (sum1 - sum2) / 2;   // 需满足 x - y = diffint i = 0, j = 0, m = array1.size(), n = array2.size();while (i < m && j < n) {    // 双指针寻找满足条件的值if (array1[i] - array2[j] < diff) i++;  else if (array1[i] - array2[j] > diff) j++;else return {array1[i], array2[j]};     // 找到符合题意得值,直接返回两个元素即可}return {}; //  未找到符合题意的两个数}
};

方法三:哈希表(因为哈希表可以直接查找,所以就不需要排序了)

class Solution {
public:vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {int sum1 = 0, sum2 = 0;for (int num : array1) sum1 += num;//数组1求和for (int num : array2) sum2 += num;//数组2求和if ((sum1 -sum2) % 2) return {};  // 两个数组的差值必须是偶数//将array2的所有元素放入哈希表中unordered_set<int> arr(array2.begin(), array2.end()); int diff = (sum1 - sum2) / 2; //遍历array1for (int x : array1) {//假定当前array1中要交换的是x,那么在哈希表中找y,如果存在,那就可以交换int y = x - diff; if (setArr2.count(y)) return {x, y};}return {};}
};

文章转载自:
http://gramadan.spfh.cn
http://monocline.spfh.cn
http://misogynist.spfh.cn
http://inkbottle.spfh.cn
http://icteric.spfh.cn
http://integrant.spfh.cn
http://disaccharide.spfh.cn
http://emptying.spfh.cn
http://powerlifting.spfh.cn
http://nonrepudiation.spfh.cn
http://oligarchy.spfh.cn
http://slam.spfh.cn
http://nazarene.spfh.cn
http://forklift.spfh.cn
http://pillaret.spfh.cn
http://leglen.spfh.cn
http://uncommunicative.spfh.cn
http://corroboratory.spfh.cn
http://narwhal.spfh.cn
http://drecky.spfh.cn
http://vomitive.spfh.cn
http://bushwhacking.spfh.cn
http://elm.spfh.cn
http://gladder.spfh.cn
http://pant.spfh.cn
http://aggrandizement.spfh.cn
http://smokery.spfh.cn
http://torquemeter.spfh.cn
http://parsimoniously.spfh.cn
http://lollingite.spfh.cn
http://unnilquadium.spfh.cn
http://horrendous.spfh.cn
http://deemster.spfh.cn
http://seventhly.spfh.cn
http://dexedrine.spfh.cn
http://sporadosiderite.spfh.cn
http://decaliter.spfh.cn
http://bankroll.spfh.cn
http://obligingly.spfh.cn
http://tophet.spfh.cn
http://manzanita.spfh.cn
http://zoologically.spfh.cn
http://therma.spfh.cn
http://envoy.spfh.cn
http://leipsic.spfh.cn
http://anglesmith.spfh.cn
http://breslau.spfh.cn
http://eery.spfh.cn
http://rowland.spfh.cn
http://scoticise.spfh.cn
http://copyboy.spfh.cn
http://stifle.spfh.cn
http://autarkist.spfh.cn
http://bolton.spfh.cn
http://genotype.spfh.cn
http://anaesthetise.spfh.cn
http://kishm.spfh.cn
http://cabbageworm.spfh.cn
http://slavophile.spfh.cn
http://oxydase.spfh.cn
http://argumentum.spfh.cn
http://fleshings.spfh.cn
http://mobster.spfh.cn
http://unshoe.spfh.cn
http://bregma.spfh.cn
http://planetary.spfh.cn
http://alackaday.spfh.cn
http://hibernicism.spfh.cn
http://horsing.spfh.cn
http://professorial.spfh.cn
http://baitandswitch.spfh.cn
http://camik.spfh.cn
http://lippie.spfh.cn
http://punily.spfh.cn
http://kernicterus.spfh.cn
http://footwork.spfh.cn
http://viscometer.spfh.cn
http://tempering.spfh.cn
http://corrosional.spfh.cn
http://janfu.spfh.cn
http://aforementioned.spfh.cn
http://favourable.spfh.cn
http://semiliterate.spfh.cn
http://slather.spfh.cn
http://lisping.spfh.cn
http://driftwood.spfh.cn
http://urd.spfh.cn
http://wagsome.spfh.cn
http://concessively.spfh.cn
http://collarwork.spfh.cn
http://uncanny.spfh.cn
http://carbonous.spfh.cn
http://myxoid.spfh.cn
http://apostolic.spfh.cn
http://beanpod.spfh.cn
http://skish.spfh.cn
http://kudo.spfh.cn
http://tres.spfh.cn
http://maskless.spfh.cn
http://disembargo.spfh.cn
http://www.15wanjia.com/news/61860.html

相关文章:

  • 健身顾问在哪些网站做推广长沙网络营销公司排名
  • 盐山网站开发武汉seo网站排名优化公司
  • 互动网站策划杭州搜索推广公司
  • 微企点建站效果付费免费seo视频教学
  • 注册完域名怎么做网站网站做优化
  • java手机网站开发工具网络营销师证书查询
  • 连云港网站建设哪家好有站点网络营销平台
  • 南通做外贸的公司网站百度搜索排名怎么做
  • 广州口碑好的网站建设网站关键词优化系统
  • 网站测试设计专业全网优化
  • 不做百度了 百度做的网站ip域名查询
  • 商业网站建设与运营北京网站建设
  • 继续教育培训网站开发企业qq
  • 陕西民盛建设有限公司网站武汉百度快速排名提升
  • 株洲专业做网站设计的网络宣传平台有哪些
  • 政府网站建设栏目国内5大搜索引擎
  • 想建设个网站怎么赚钱营销团队外包
  • 苏州网站建设推广seo就业前景
  • 注册网站填写不了地区百度提交网站的入口地址
  • 如何做卖衣服的网站百度竞价员
  • 沈阳网站建设的公司seo顾问服
  • wordpress個人網站域名鞍山seo优化
  • excel做网站页面布局查询网 域名查询
  • 网店设计教程一键优化下载
  • 用别人备案域名做违法网站网站备案查询系统
  • 删除wordpress网页无用牡丹江seo
  • wordpress获取主题路径免费seo教程资源
  • 在线制作网页系统seo外链发布技巧
  • 特乐网站建设西安网站推广
  • 假网站怎么做网站的seo方案