企业建立网站的必要性,陕西榆林市信息网站建设公司,有一个网站叫浪什么,公司网站开发策划书力扣#xff0c;https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/description/ 题目#xff1a;一个整型数组nums里除两个数字之外#xff0c;其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n)#xff0c;空间…力扣https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/description/ 题目一个整型数组nums里除两个数字之外其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n)空间复杂度是O(1)。 注意相同的数异或为0不同的异或为1。0和任何数异或等于这个数本身。 思路
class Solution {public int[] sockCollocation(int[] sockets) {int x 0, y 0, n 0, m 1;for (int val : sockets) {n ^ val;}while ((n m) 0) {m 1;}for (int val : sockets) {if ((val m) 0) {x ^ val;} else {y ^ val;}}return new int[] {x, y};}
}