LeetCode-Single NumberI II III

I)

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


第一个比较好求解,异或操作就可以了:

    public int singleNumber(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
			nums[0] ^= nums[i];
		}
		return nums[0];
    }

III)

在看第三个,第三个跟第一个的关系比较密切:

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
这个可以跟第一个联想,唯一不同的就是这个有两个数字出现一次。所以现在的目标就是转化到第一个,将数组分成两组,并且将两个数字分开,怎么分呢?

    public int[] singleNumber(int[] nums) {
        int n = 0;
        for (int i = 0; i < nums.length; i++) n ^= nums[i];
        n &= ~(n-1);
        int[] ret = new int[2];
        
        for (int i = 0; i < nums.length; i++) {
            if ((nums[i] & n) == 0) ret[0] ^= nums[i];
            else ret[1] ^= nums[i];
        }
           
        return ret;
    }
代码如上,先求整个数组的异或,则最终等价于两个出现一次的数字异或,然后我们根据异或的结果进行分组,很显然。如果异或的二进制表示中,有一位为1,则表示。这两个数字在此位置的二进制表示是不同的。所以就可以根据这个位来进行分组,然后就把问题转化为第一个问题了!

II)

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

第二个,跟上面的关系不大,尽管都是位操作:

    public int singleNumber(int[] nums) {
    	int cntOne = 0;
    	int number = 0;
    	for (int i = 0; i < 32; i++) {
    		cntOne = 0;
    		for (int j = 0; j < nums.length; j++) {
    			if ((nums[j] & (1 << i)) != 0) cntOne++;
    		}
    		number |= (cntOne %= 3) << i;
    	}
    	return number;
    }

对int的每一位进行遍历判断。此时遍历数组统计每个位1,出现的次数,然后对3取余数就是出现一次的那个数在本位的次数,然后将这个32个0或1组合起来!





LeetCode的第260题中,题目被称为“只有一个公共元素的数组 II”。这是一道关于数组操作的数据结构和算法题目。给定两个已经排序的整数数组 nums1 和 nums2,你需要找出它们之间的一个共同的元素,这个元素在每个数组中都只出现一次。 该问题的目标是找到这样的一个公共元素,并返回它的值。例如,如果nums1 = [1, 2, 2, 4] 和 nums2 = [2, 2, 8, 10],那么唯一的公共单次出现的元素是2。 为了解决这个问题,你可以采用一种类似于双指针的方法。从两个数组的起始位置开始遍历,每次比较当前指针对应的元素。如果元素相等且在各自的数组中都是第一次出现(即前一个出现的位置比当前位置大),就将这个元素添加到结果中并继续向后移动指针。如果遇到不同的元素,则移动较小的那个指针的所在数组的指针。 Python 或 C++ 等语言中,可以实现一个循环或者递归的解决方案,直到找到共同的单次出现元素或遍历完其中一个数组。这里是一个简单的 Python 示例: ```python def singleNumber(nums1, nums2): i, j = 0, 0 count = {} while i < len(nums1) and j < len(nums2): if nums1[i] not in count or nums2[j] not in count: count[nums1[i]] = 1 i += 1 elif nums2[j] not in count: count[nums2[j]] = 1 j += 1 else: del count[nums1[i]] del count[nums2[j]] i += 1 j += 1 # 如果有一个数组没遍历完,剩下的最后一个元素就是公共唯一元素 return list(count.keys())[0] if i < len(nums1) else list(count.keys())[-1] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值