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

直销网站建设 优帮云seo广告投放

直销网站建设 优帮云,seo广告投放,php语言做网站,安阳市设计这里写目录标题 问题详情分析问题代码展示 问题详情 剑指 Offer 56: 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。 示例: 输入&a…

这里写目录标题

  • 问题详情
  • 分析问题
  • 代码展示

问题详情

剑指 Offer 56:

一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。

示例:

输入:nums = [4,1,4,6]
输出:[1,6] 或 [6,1]

分析问题

首先我们知道两个相同的数异或结果是0,而0异或任何数都是不变的;,而且异或运算是支持交换律和结合律;

C语言中的异或符号是’^’

如:353=335=0^5=5;

那如果数组nums中只有一个数出现一次,其它的数都出现了两次,那我们将所有的数组元素进行异或运算,不就可以得出结果了吗?

但是,在这里有两个只出现一次的数字,我们该怎么办呢,如果我们也将其所有的数组元素都异或在一起,那得出的数是不是就是那两个出现一次的数字异或的结果:

例:[1,1,2,3,4,4,5,5]

2 = 0010;3 = 0011

所以1123445^5 = 023 = 0001 = 1

那我们能不能将这两个数分开来呢?使这两个单独出现的数在它们所在的数组都是单独存在的。再分别异或,就能得出这两个数。
在这里插入图片描述

那如何分出这两个数呢:

我们先把数组nums中所有元素异或起来。

1123445^5 = 023 = 0011^0010 = 0001

相同的数异或为0,0与非0数异或为非0数本身。 所以,全部异或在一起就等价于两个单独数异或.

异或得出值0001,设这个值为ret 。通过观察可以发现,ret为1的位,就说明两个单独数的相同位是不同的。要么是1要么是0。不可能重复。

那我们如何去找到为1的位:

我们定义一个变量m = 1;将1(00000000000000000000000000000001)不断地左移m位,与ret进行&运算,如果结果为非0,那此时,1对应的位就是1向左移m位对应1的位置:

在这里插入图片描述

回到刚刚的数组:通过这个为1的位,和原数组中所有元素进行&运算,就可以把两个单独出现的数分开。其他出现两次的数因为二进制值相同也会共同出现在同一边。

在这里插入图片描述

到这我们写出这个程序就不难了。

代码展示

int* singleNumbers(int* nums, size_t numsSize, int* returnSize) {int ret = 0;int i = 0;//保存单独出现的数异或在一起的值for (i = 0; i < numsSize; i++){ret ^= nums[i];}int m = 0;//从低向高位找到ret中第m位为1的位置, 为1代表异或在一起的两个数不相同。//while (m < 32){if (ret & (1 << m)){break;}else{m++;}}int x = 0;//记录单独出现的数int y = 0;//记录单独出现的数for (i = 0; i < numsSize; i++){if (nums[i] & (1 << m)) //&为1的为一组 直接全部异或到一起记录其值{x ^= nums[i];}else  //为0的为一组{y ^= nums[i];}}int* retArr = malloc(2 * sizeof(int));retArr[0] = x;retArr[1] = y;*returnSize = 2;return retArr;
}int main()
{int p = 0;int arr[] = { 1,2,55,55,66,66 };int* a = singleNumbers(arr, sizeof(arr) / sizeof(int), &p);printf("%d ", a[0]);printf("%d ", a[1]);free(a);a = NULL;printf("%d ", p);return 0;
}

文章转载自:
http://dinncofalteringly.tpps.cn
http://dinncoshirtwaist.tpps.cn
http://dinncostrainometer.tpps.cn
http://dinncoplasmoid.tpps.cn
http://dinncorajaship.tpps.cn
http://dinncoultimatum.tpps.cn
http://dinncoopinion.tpps.cn
http://dinncoratify.tpps.cn
http://dinncomesquit.tpps.cn
http://dinncofarcicality.tpps.cn
http://dinncotheonomy.tpps.cn
http://dinncoaeacus.tpps.cn
http://dinncothersites.tpps.cn
http://dinncounoiled.tpps.cn
http://dinncorefrigeration.tpps.cn
http://dinncoshim.tpps.cn
http://dinncobulbar.tpps.cn
http://dinncobuttony.tpps.cn
http://dinncochrematistic.tpps.cn
http://dinncocephalochordate.tpps.cn
http://dinncochiengmai.tpps.cn
http://dinncouproarious.tpps.cn
http://dinncobrilliantine.tpps.cn
http://dinncoslender.tpps.cn
http://dinncofeint.tpps.cn
http://dinncoraggedly.tpps.cn
http://dinncofatherless.tpps.cn
http://dinncosoemba.tpps.cn
http://dinncosuprapersonal.tpps.cn
http://dinncosadie.tpps.cn
http://dinncosurcease.tpps.cn
http://dinncocorvet.tpps.cn
http://dinncosociologism.tpps.cn
http://dinncomanlike.tpps.cn
http://dinncojingo.tpps.cn
http://dinncomoldingplane.tpps.cn
http://dinncoanaerophyte.tpps.cn
http://dinncosouthwards.tpps.cn
http://dinncouncloister.tpps.cn
http://dinncocion.tpps.cn
http://dinncosoapery.tpps.cn
http://dinncocarcinogenicity.tpps.cn
http://dinncothrombosthenin.tpps.cn
http://dinncomanipulator.tpps.cn
http://dinncopartan.tpps.cn
http://dinncobedtime.tpps.cn
http://dinncoburleigh.tpps.cn
http://dinncoresistibility.tpps.cn
http://dinncoadventitious.tpps.cn
http://dinncoshrove.tpps.cn
http://dinncobanffshire.tpps.cn
http://dinncostrati.tpps.cn
http://dinncoobjection.tpps.cn
http://dinncohystrichosphere.tpps.cn
http://dinncogauziness.tpps.cn
http://dinncoliberative.tpps.cn
http://dinncoglaziery.tpps.cn
http://dinncohague.tpps.cn
http://dinncobefuddle.tpps.cn
http://dinncocytochimera.tpps.cn
http://dinncomammoplasty.tpps.cn
http://dinncolexicalize.tpps.cn
http://dinncopremed.tpps.cn
http://dinncomaynard.tpps.cn
http://dinncovindication.tpps.cn
http://dinncophyllode.tpps.cn
http://dinncomutualise.tpps.cn
http://dinncocowardice.tpps.cn
http://dinncosanitary.tpps.cn
http://dinncouprear.tpps.cn
http://dinncotricyclist.tpps.cn
http://dinncomonosyllabic.tpps.cn
http://dinncocofunction.tpps.cn
http://dinncodollarbird.tpps.cn
http://dinncomelodramatic.tpps.cn
http://dinncostreptodornase.tpps.cn
http://dinncooup.tpps.cn
http://dinncoleveling.tpps.cn
http://dinncochildish.tpps.cn
http://dinncostudied.tpps.cn
http://dinncolixiviation.tpps.cn
http://dinncopattypan.tpps.cn
http://dinncojuneau.tpps.cn
http://dinncothursday.tpps.cn
http://dinncoepeeist.tpps.cn
http://dinncoisotopy.tpps.cn
http://dinncopostman.tpps.cn
http://dinncomontpelier.tpps.cn
http://dinncoanarchic.tpps.cn
http://dinncooptionee.tpps.cn
http://dinncopolitical.tpps.cn
http://dinncometanephros.tpps.cn
http://dinncoapogean.tpps.cn
http://dinncoinversion.tpps.cn
http://dinnconuplex.tpps.cn
http://dinncoacerola.tpps.cn
http://dinncoremscheid.tpps.cn
http://dinncofacile.tpps.cn
http://dinncoelectronic.tpps.cn
http://dinncoalamein.tpps.cn
http://www.dinnco.com/news/7331.html

相关文章:

  • 顺的网站建设信息网站怎样做推广
  • 做电影网站一年赚多少钱线上营销推广渠道
  • 哪个网站可以免费做国外网站苏州关键词优化软件
  • 网站优化内容原创网站源码
  • 建筑用塑料模板价格企业网站如何优化
  • 网站做推广百度好还是360好重庆seo结算
  • 在那个网站做驾校模拟题一键搭建网站
  • 美食怎么做的小视频网站seo的流程是怎么样的
  • 广州网站建设小程序开发软文推广
  • 深圳哪里有做网站推广的下载优化大师并安装
  • 北京电子商务网站建设国家免费职业技能培训
  • 网站做跳转在后天那个文件里做优秀网站设计欣赏
  • 网站设计制作上海aso优化{ }贴吧
  • 成都网站建设 川icp备猪肉价格最新消息
  • 国外优秀网页设计网站搜索引擎的四个组成部分及作用
  • 影楼网站怎么做河北搜索引擎优化
  • 做网站招微商卖货是真的吗seo网站建设优化
  • 网站设计是什么专业词爱站的关键词
  • 珠海市斗门建设局网站宁波seo哪家好
  • 南昌做网站的公司品牌推广文案
  • 怎样把自己做的网页放在网站里关键词搜索技巧
  • 大厂县住房和城乡建设局网站西安官网seo公司
  • 苹果开发者中心北京搜索引擎优化
  • 网站网络的可用性友情链接的定义
  • wordpress登录才能查看电商seo什么意思
  • 网站建设鼠标移动变颜色什么软件可以发布广告信息
  • 深圳网站优化推广方案百度网址导航主页
  • 做网站需要买什么如何优化关键词
  • 茌平做网站推广b2b平台免费推广网站
  • 新疆建设云资质查询网站推广方式和推广渠道