Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,3,2]
Output: 3

Example 2:

Input: [0,1,0,1,0,1,99]
Output: 99 Solution:
  因为不能使用额外的空间,不然就可以开辟数组来对数据个数进行统计了
  那么只能使用位运算了,计算32位中,每个数字出现1的次数,一旦个数不为1,那么该位就是那个唯一数字的位数了
 class Solution {
public:
int singleNumber(vector<int>& nums) {
int bits[] = { };
for (int i = ; i < nums.size(); ++i)
{
int c = ;
for (int j = ; j < ; ++j)
{
bits[j] += (c & nums[i]) ? : ;
c = c << ;
}
}
int res = ;
for (int i = ; i < ; ++i)
if (bits[i] % )
res += (int)pow(2.0, i);
return res;
}
};

最新文章

  1. 购物车数字加减按钮HTML+CSS+JS(有需要嫌麻烦的小伙伴拿走不谢)
  2. laravel administrator 一款通用的后台插件(PHP框架扩展)
  3. Listview实现不同类型的布局
  4. SVN服务器搭建和使用(三)(转载)
  5. .net 测试工具类
  6. C语言初学 比较五个整数并输出最大值和最小值2
  7. 通过 bootloader 向其传输启动参数
  8. [Unity]Unity3D编辑器插件扩展和组件扩展
  9. 认识Java WEB应用
  10. js实现刷新
  11. 第一篇-Html标签中head标签,body标签中input系列,textarea和select标签
  12. UINavigationController - BNR
  13. PreparedStatement 与 Statement 的区别
  14. Modbus CRC 16 (C#)
  15. CSS居中的几种方式总结
  16. mysql查看表注释和字段注释的方法
  17. 二. Redis 安全性
  18. HTML5 canvas 内部元素事件响应
  19. 01: socket模块
  20. GIS-007-Terrain跨域访问

热门文章

  1. SQL语句:随机取3条不重复的记录
  2. CF561做题
  3. VMware虚拟机重新挂载共享目录
  4. tfsenflow队列|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners
  5. Scrapy框架: Request回调函数
  6. luoguP1541 乌龟棋 题解(NOIP2010)
  7. matplotlib系列——线的属性
  8. 图片查看器(类似于QQ,另外又加了JARA的下方的图片缩略导航图)
  9. 2018-2-13-win10-uwp-圆角按钮
  10. python 发送请求