衡水精品网站建设价格,网站维护一年一般多少钱?,如何选择一个好的优质网站建设公司,医院关于建设官方网站的请示// 有1到N共 n-1个数#xff0c;问少了哪个数 // 有序数组(如果是无序数组那么将a[i] 移动至 a[a[i]] 这样子就成功排序了) //其实可以采用byte数组的来做#xff0c;感觉会更快 //当然#xff0c;这个更多的是用在1-n 共n1个数#xff0c;问多出来的那个数是多少#xff…// 有1到N共 n-1个数问少了哪个数 // 有序数组(如果是无序数组那么将a[i] 移动至 a[a[i]] 这样子就成功排序了) //其实可以采用byte数组的来做感觉会更快 //当然这个更多的是用在1-n 共n1个数问多出来的那个数是多少这个时候可以不确认一下N是多少然后连续的每4个数异或出来的结果为0这样即省去了数组的开销 public static void lessOne(int[] a) {int fruit 0;int count 0;for (int i 0; i a.length; i) {count;fruit ^ a[i];if (count 4) {if (fruit 0) {count 0;} else {System.out.println(Less is: i);break;}}}
} 下面为测试代码 int[] a new int[100];
for (int i 0; i a.length; i) {if (i 67)continue;a[i] i;
}
//也可以使用已提供的方法进行排序但不建议
//Arrays.sort(a);
lessOne(a); // 有一组数中间有两个数的出现次数为奇数次问是哪两个数 public static void lesstwoj() {
int[] a { 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8 };int sum 0;for (int i : a) {sum ^ i;}int count 0;int temp sum;while ((temp 1) 0) {temp 1;count;}int one 0;for (int i : a) {if (((i count) 1) ! 0) {one ^ i;}}int sec sum ^ one;System.out.println(one: one);//第一个数System.out.println(sec: sec);//第二个数
} // 采用异或运算来进行交换位置 //不借助于第三个数 public static void swap(int a, int b) {if (a ! b) {a a ^ b;b a ^ b;a a ^ b;}System.out.println(one-- a: a b: b);
} 转载于:https://www.cnblogs.com/ahhy/p/4383668.html