[leetCode]Single Number III
2016-01-06 14:22
363 查看
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
思路:要求返回只出现过一次的两个数。分两步:
1、将这两个数以某种形式挑出来;
2、将以某种形式挑出来的两个数分开;
结合Single Number 1
我们可以通过对整个数组遍历异或,返回只出现一次的两个数的异或值。第二步就是怎么通过这个异或值分开这两个数。
第二步可以通过标志位来实现,异或后的结果中不为0的位均表示这两个只出现过一次的数在这些位上不相同。因此,我们可以以其中一个1为标志位,实现两个数的拆分。
}
[/code]
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
思路:要求返回只出现过一次的两个数。分两步:
1、将这两个数以某种形式挑出来;
2、将以某种形式挑出来的两个数分开;
结合Single Number 1
我们可以通过对整个数组遍历异或,返回只出现一次的两个数的异或值。第二步就是怎么通过这个异或值分开这两个数。
第二步可以通过标志位来实现,异或后的结果中不为0的位均表示这两个只出现过一次的数在这些位上不相同。因此,我们可以以其中一个1为标志位,实现两个数的拆分。
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* singleNumber(int* nums, int numsSize, int* returnSize) { int num = 0; int value = 0;
int firstDiffBit = 1; int categoryNum; int *numsOnce; // malloc memory if ( !(numsOnce = (int*)malloc(sizeof(int) * 2)) ) { printf("malloc Failed!\n"); return NULL; } while(num < numsSize) // 返回两个只出现过一次的数的异或值 value ^= nums[num++]; while( !(value & firstDiffBit) ) firstDiffBit <<= 1; //!< 两个只出现过一次的数的第一位不相同的位置 for(categoryNum = 0; categoryNum < 2; ++categoryNum) { int categoryVal = 0; for(num = 0; num < numsSize; ++num) //遍历两遍 categoryVal ^= ( !(firstDiffBit & nums[num]) == categoryNum) ? nums[num]:0; numsOnce[categoryNum] = categoryVal; } if (returnSize) { *returnSize = 2; } return numsOnce;
}
[/code]
相关文章推荐
- UML类图几种关系的总结
- linux 内存管理 一 概念
- 最简单的基于FFmpeg的推流器(以推送RTMP为例)
- Android 网络框架学习之Retrofit,androidretrofit
- SQL Server时间粒度系列----第1节时间粒度概述
- [html][javascript]父子窗体传值
- 怎么理解Android版本兼容???
- Linux 防火墙管理
- html之网页中的图片
- DEDE列表调用有缩图显示缩图,无缩图则不显示缩图
- gcc编译链接库并调用链接库的方法
- 计算数据库中各个表的数据量和每行记录所占用空间
- 2016年的几个突破点
- 优化MYSQL数据库的方法(网络回答)
- 理性对待香烟
- 图书管理系统(C语言)
- C#中使用泛型对比使用通用基础类型效率降低近一倍
- tomcat解析之简单web服务器(图)
- bootstrap弹出框居中
- python count(), lower(), ord(), chr(), isalpha()