力扣算法——137SingleNumberII【M】
2024-09-02 08:23:34
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;
}
};
最新文章
- 购物车数字加减按钮HTML+CSS+JS(有需要嫌麻烦的小伙伴拿走不谢)
- laravel administrator 一款通用的后台插件(PHP框架扩展)
- Listview实现不同类型的布局
- SVN服务器搭建和使用(三)(转载)
- .net 测试工具类
- C语言初学 比较五个整数并输出最大值和最小值2
- 通过 bootloader 向其传输启动参数
- [Unity]Unity3D编辑器插件扩展和组件扩展
- 认识Java WEB应用
- js实现刷新
- 第一篇-Html标签中head标签,body标签中input系列,textarea和select标签
- UINavigationController - BNR
- PreparedStatement 与 Statement 的区别
- Modbus CRC 16 (C#)
- CSS居中的几种方式总结
- mysql查看表注释和字段注释的方法
- 二. Redis 安全性
- HTML5 canvas 内部元素事件响应
- 01: socket模块
- GIS-007-Terrain跨域访问
热门文章
- SQL语句:随机取3条不重复的记录
- CF561做题
- VMware虚拟机重新挂载共享目录
- tfsenflow队列|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners
- Scrapy框架: Request回调函数
- luoguP1541 乌龟棋 题解(NOIP2010)
- matplotlib系列——线的属性
- 图片查看器(类似于QQ,另外又加了JARA的下方的图片缩略导航图)
- 2018-2-13-win10-uwp-圆角按钮
- python 发送请求