Single Number II leetcode java
问题描述:
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?
提示:bit manipulation(位操作)
参考:http://www.acmerblog.com/leetcode-single-number-ii-5394.html?utm_source=tuicool&utm_medium=referral
分析:由于所有数字都是出现奇数次,所以,并不是简单的异或运算。
考虑所有数字用二进制表示,如果把第i个位置上所有数字的和对3取余,那么只会有两种结果,0或1。因此,取余的结果就是那个single number。
java 代码:
一、一个直接的实现就是用大小为 32的数组来记录所有位上的和。
public int singleNumber(int[] nums){
       //所有数字都使用32位二进制表示,初始为0
       int[] count = new int[32];
       int result = 0; //singlenumber
       for(int i = 0; i < 32; i++){
           for(int j = 0; j < nums.length; j++){
                  int key = (nums[j] >> i) & 1;
                  if (key == 1) { //右移,获得第i个bit,统计1的个数
                      count[i] ++ ;
                  }
           }
           //第i位左移,然后将所有位相或,最终得到singlenumber
           result |= ((count[i] % 3) << i);
       }
      return result;
 }
二、使用掩码,改进算法一
ones代表第ith 位只出现一次的掩码变量twos代表第ith 位只出现两次的掩码变量threes代表第ith 位只出现三次的掩码变量
public int singleNumber(int[] nums){
      int ones = 0, twos = 0, threes = 0;
      for(int i = 0; i < nums.length; i++){
            twos = twos | ( ones & nums[i]);
            ones = ones ^ nums[i]; //异或3次 和 异或 1次的结果是一样的
            threes = ones & twos;  
            //对于ones 和 twos, 把出现了3次的位置设置为0 (取反之后1的位置为0)
            ones = ones & ~threes;
            twos = twos & ~threes;
      }
      return ones;
}
Single Number II leetcode java的更多相关文章
- Single Number III leetcode java
		
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
 - Ugly Number II leetcode java
		
问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...
 - Single Number II - LeetCode
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - 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)
		
LeetCode 137. Single Number II(只出现一次的数字 II)
 - LeetCode(137) Single Number II
		
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
 - 【LeetCode】137. Single Number II 解题报告(Python)
		
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
 - 【leetcode】Single Number && Single Number II(ORZ 位运算)
		
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
 - 【题解】【位操作】【Leetcode】Single Number II
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 
随机推荐
- JPA原理与实践、多数据源配置
			
参考博客: https://segmentfault.com/a/1190000015047290?utm_source=Weibo&utm_medium=shareLink&utm_ ...
 - HTTP协议请求类型介绍
			
HTTP协议中共定义了八种方法或者叫"动作"来表明对Request-URI指定的资源的不同操作方式,具体介绍如下: OPTIONS: 返回服务器针对特定资源所支持的HTTP请求方法 ...
 - sql -- 移除数据中的换行符和回车符
			
https://blog.csdn.net/jcx5083761/article/details/40185795 --移除回车符 update master_location SET street_ ...
 - Facebook ads_Business Manager
			
xmind 下载链接
 - java泛型的作用和好处
			
转载于:https://blog.csdn.net/u012760183/article/details/52092692 之前参加面试的时候遇到面试官问泛型的作用,只是说了大概自己的理解, 在此转载 ...
 - 6、tcp_wrapper
			
iptables的链接跟踪表最大容量为/proc/sys/net/ipv4/ip_conntrack_max,链接碰到各种状态的超时后就会从表中删除. 所以解決方法一般有两个: (1) 加大 ip_c ...
 - css的postion属性
			
在实际项目中,发现postion这个属性经常使用而且常常很重要,所以总结整理一下知识点 css中postion属性有以下可选值,分别是:static,absolute, fixed, relative ...
 - Java常见异常:Exception in thread "main" java.lang.NoClassDefFoundError
			
在某一路径下执行编译好的class文件出错. 异常如下: E:\liwy>java Test98 Exception in thread "main" java.lang.N ...
 - vue运行报错--dependency
			
ERROR Failed to compile with 1 errors 11:17:27 This dependency was not found: 解决方法:把报错所缺少的依赖都装上 如 xx ...
 - JCF
			
点我 补两个红黑树原理:https://www.cnblogs.com/yyxt/p/4983967.html https://www.cnblogs.com/skywang12345/p/324 ...