Java for LeetCode 137 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?
解题思路一:
一个int的长度是32,因此可以开一个长度为32的数组,表示nums中所有元素各位1的个数和,然后%3即可得到结果。
JAVA实现如下:
public int singleNumber(int[] nums) {
int[] bitnum = new int[32];
int res = 0;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < nums.length; j++)
bitnum[i] += (nums[j] >> i) & 1;
res += (bitnum[i] % 3) << i;
}
return res;
}
解题思路二:
分别用三个变量bit0、bit1、bit2表示nums元素中1个数为0、1、2的分布,最后返回bit1即可,JAVA实现如下:
public int singleNumber(int[] nums) {
int bit0 = ~0, bit1 = 0, bit2 = 0, oldTwo;
for (int i = 0; i < nums.length; i++) {
oldTwo = bit2;
bit2 = (bit1 & nums[i]) | (bit2 & ~nums[i]);
bit1 = (bit0 & nums[i]) | (bit1 & ~nums[i]);
bit0 = (oldTwo & nums[i]) | (bit0 & ~nums[i]);
}
return bit1;
}
Java for LeetCode 137 Single Number II的更多相关文章
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- [LeetCode] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- [LeetCode] 137. Single Number II 单独的数字之二
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- 详解LeetCode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly ...
- leetcode 137. Single Number II ----- java
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Java [Leetcode 137]Single Number II
题目描述: Given an array of integers, every element appears three times except for one. Find that single ...
- LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数
Given an array of integers, every element appears three times except for one, which appears exactly ...
随机推荐
- freemarker中include与import的区别
在inc1.ftl与inc2.ftl中的内容分别是: <#assign username="刘德华">与<#assign username="张学友&q ...
- Batch update returned unexpected row count from update [0];
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested ...
- JAVA Eclipse开发Android如何让超出界面的部分自动显示滚动条
在原有布局的最外围添加一层ScrollView,注意原有布局的声明也要删了 <ScrollView xmlns:android="http://schemas.android.com/ ...
- Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)
导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...
- MFC开发小技巧总结
1.在类向导里面可以为对话框添加方法. 2.如要添加变量,直接右击添加变量即可. 3.若对某个控件添加方法或者称之为消息处理函数,直接右击添加事件处理程序即可.
- Oracle 修改带数据的字段类型
http://www.cnblogs.com/LDaqiang/articles/1157998.html由于需求变动,现要将一个类型NUMBER(8,2)的字段类型改为 char.大体思路如下: ...
- SQL存在一个表而不在还有一个表中的数据
select a.id,a.oacode,a.custid,a.custname,a.xsz,a.salename,a.communicationtheme,a.communicationproper ...
- AWS上的游戏服务:Lumberyard + Amazon GameLift + Twitch
开发一款世界级的游戏是一个非常困难,耗时和昂贵的过程.如今的游戏玩家要求越来越苛刻,他们希望既能够通过各种不同的终端设备来进行游戏 ,又要求游戏具有社交的功能. 因为此类游戏的开发期和推广期都非常长. ...
- HDOJ2084数塔问题
数塔问题 题目要求从顶层走究竟层.若每一步仅仅能走到相邻的结点,求经过的结点的数字之和最大值. 非常经典的DP,能够这样考虑,要求从塔顶到塔底最大路径之和.计算时能够考虑自底向上,走最后一步所选的数一 ...
- windows下rsync部署安装
windows下rsync部署安装 2012-06-05 12:06:13| 分类: 系统 | 标签:rsync windows |字号 订阅 rsync在windows与windows ...