您的位置:首页 > 其它

阿里2015笔试附加题-一个数组中存在一组数字,其中有一个数字重复3遍,其他2遍,在O(1)空间找到那个重复3次的数

2015-08-24 09:54 447 查看
题目就如标题,还要求时间尽可能短。这一题类似于LeetCode上的SingleNum那题,思路是将所有数字进行异或操作,最后剩下的那个数字就是重复3遍的。

代码如下:

package test2;

public class test2 {

	public static int getNum(int[] array){
        
        int result = 0;
        for(int i = 0; i < array.length;i ++){
          result ^= array[i];
        }
        return result;
      
      }
	
	public static void main(String[] args){
		int[] array = {88, 459, 5262, 88, -17, 677, 88, 677, -17, 459, 5262};
		System.out.print(getNum(array));
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: