概述
有一整数数组,所有数都重复了两次,只有两个数重复了一次,找出这两个数,要求空间复杂度为O(1),时间复杂度O(n)
代码
package com.lilei.spring_boot_db.boot.pack1115; public class single2num { public static void main(String[] args) { int[] array = new int[]{3,3,4,4,9,9,6,5}; get2num(array); } static void get2num(int[] array){ int value = 0; for(int v:array) value = value ^ v; int tmp = 0; while(value % 2 == 0) { value = value / 2; tmp++; } int v1 =0,v2 =0; for(int v:array){ if (((v >> tmp) % 2) == 0) v1 = v1 ^ v; else v2 = v2 ^ v; } System.out.println(v1+","+v2); } }