LeetCode题解:Single Number II

来源:互联网 发布:100以内手指算法视频 编辑:程序博客网 时间:2024/05/21 09:35

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?

题意:给定整数数组,除了某个数,其余数都出现3次。找出那个数。要求O(n)时间,O(1)空间

思路:看不懂……据说通过电路分析和卡诺图可以得到式子

代码:

public int singleNumber(int[] A) {    int ones = 0, twos = 0;    for(int i = 0; i < A.length; i++){        ones = (ones ^ A[i]) & ~twos;        twos = (twos ^ A[i]) & ~ones;    }    return ones;}
0 0
原创粉丝点击