【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
题解:线性时间,O(1)的空间复杂度,只能把整数写成二进制的形式运算了。具体做法如下:
比如有一个数组{5,5,5,3,2,3,3},这些数写成二进制如下:
5:101
5:101
5:101
3:011
2:010
3:011
3:011
我们分别统计这三位的1的个数,然后对3取模,得到每一位的结果结合起来就是010,就是只出现一次的数字2.
所以,我们可以设置一个32位的数组,然后对A中的所有数字,统计这32位中每一位上1的个数,然后对3取模,最后得到的32位的二进制数就是只出现一次的那个数了。
Java代码如下:
public class Solution {
public int singleNumber(int[] A) {
int[] digits = new int[32];
for(int i = 0;i < 32;i++){
for(int j = 0;j < A.length;j++){
digits[i] += (A[j] >> i)&1;
}
}
int result = 0;
for(int i = 0;i < 32;i++){
result += (digits[i]%3) << i;
}
return result;
}
}
【leetcode刷题笔记】Single Number II的更多相关文章
- 【leetcode刷题笔记】Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- php做推送服务端实现ios消息推送
本文部分内容引用于 http://zxs19861202.iteye.com/blog/1532460 准备工作 1.获取手机注册应用的deviceToken(iphone手机注册应用时返回唯一值de ...
- ListView中pointToPosition()方法使用具体演示样例
MainActivity例如以下: package cc.testpointtoposition; import java.util.ArrayList; import java.util.HashM ...
- linux 分区格式查看
Linux分区格式查看 两个文件 /etc/fstab 和/etc/mtab /etc/fstab是用来存放文件系统的静态信息的文件,当系统启动的时候. 系统会自动地从这个文件读取信息,并且会自动将此 ...
- LeetCode: Validate Binary Search Tree [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 微信公众平台开发:进阶篇(Web App开发入门)
本文转载至:http://blog.csdn.net/yual365/article/details/16820805 WebApp与Native App有何区别呢? Native App: 1.开 ...
- 错误命令“if not exist "\Dll" mkdir "\Dll" xcopy "\bin\Debug\*.*" "F:\647\VS项目\EtrolMes2014SY\Framework\Dll" /e /i /y”已退出,代码为 9009
分析错误 第一步:观察错误,发现plugin文件夹中未生成对应的编译文件. 第二步:XCOPY命令无法执行,百度xcopy为何无法执行 第三步,搜索,发现环境变量未配置正确. 就是环境变量path(大 ...
- log4j分级别打印和如何配置多个Logger
log4j.rootLogger=dubug,info,warn,error 最关键的是log4j.appender.[level].threshold=[level] 这个是日志分级别打印的最关 ...
- UESTC 485 Game(康托,BFS)
Today I want to introduce an interesting game to you. Like eight puzzle, it is a square board with 9 ...
- zend studio,操作记录
1.代码字体大小 打开Window->Preferences->General->Apperance-Colors and fonts->Structured Text Edi ...
- pycharm中格式标准化代码
点击之后,可以使代码标准化