码迷,mamicode.com
首页 > 其他好文 > 详细

Single Number II

时间:2015-05-07 12:20:47      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/single-number-ii/

https://leetcode.com/discuss/6632/challenge-me-thx

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?

 1 public class Solution {
 2     public static int singleNumber(int[] nums) {
 3         int len=nums.length;
 4         int one=0;int two=0;
 5         for(int i=0;i<len;i++){
 6             one=(one^nums[i])&~two;
 7             two=(two^nums[i])&~one;
 8         }
 9         return one;
10     }
11     public static void main(String[]args){
12     int[]nums={1,1,1,2,3,3,3};
13     System.out.println(singleNumber(nums));
14     }
15 }

 

Single Number II

标签:

原文地址:http://www.cnblogs.com/qq1029579233/p/4484216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!