【LeetCode】SingleNumberII_137
2015-08-24 22:19
309 查看
package com.leetCode; /** * 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? * * @author Zealot * @date 2015年7月25日 下午6:16:12 */ public class SingleNumberII_137 { public int singleNumber(int[] nums) { int ones = 0, twos = 0; for(int i = 0; i < nums.length; i++){ ones = (ones ^ nums[i]) & ~twos; twos = (twos ^ nums[i]) & ~ones; } return ones; } //遍历数组中每一个元素 //计算每一个元素中,2进制中每一位加到一个总的数组里边,都加到一起之后,再与3取余,因为除了一个之外,都是出现了3次,只有那一个之出现了不到3次 //最后数组转成数字 public int singleNumber2(int[] A) { if(A == null || A.length == 0) return 0; int[] a = new int[32]; for(int i = 0; i < A.length; i++) { for(int j = 0; j < 32; j++) { if((A[i] & (1 << j)) != 0) a[j] = (a[j] + 1) % 3; } } int result = 0; for(int i = 0; i < 32; i++) { if(a[i] > 0) result |= (a[i] << i); } return result; } public static void main(String[] args) { SingleNumberII_137 s = new SingleNumberII_137(); int[] nums = {1,2,3,3,3,2,5,2,1,1}; System.out.println(s.singleNumber2(nums)); // System.out.println(77>>1); // System.out.println(4>>1); // System.out.println(Integer.toBinaryString(4).length()); // System.out.println("123456".length()); } }
相关文章推荐
- scala学习之:链式风格及路径依赖
- [Objective-C]类与对象
- case when then else end数据统计
- sklearn特征选择和分类模型
- 第47讲:Scala多重界定代码实战及其在Spark中的应用源码解析学习笔记
- 共享内存例子
- Android中的四种点击事件
- spring的propogation(传播机制)
- Android UI设计——ListView控件与Adapter适配器(一)
- Eclipse 搭建 Maven Web项目
- bash脚本中比较字符串的方法
- 序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
- Windows I/O系统
- 第四 mysql数据库和SQL语句
- hdu1080 Human Gene Functions (DP动态规划,带权最长公共子串)
- Note For Linux By Jes(10)- 例行性工作排程 (crontab)
- ArrayAdapter,SimpleAdapter,BaseAdapter
- 深入理解Linux修改hostname(转)
- Android-ViewPager(FragmentPagerAdapter 和 FragmentStatePagerAdapter)
- linux中getopt用法