首页 > 其他 > 详细

Single Number II

时间:2015-05-07 12:18:44      阅读:168      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!