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

来源:互联网 发布:信息数据信号的关系 编辑:程序博客网 时间:2024/06/08 17:55


题目就如标题,还要求时间尽可能短。这一题类似于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));}}


0 0
原创粉丝点击