""" Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. 输入一组数字, 每个元素都重复一次, 只有 一个元素是单独出现的, 没有重复, 找出这个单独的元素 使用异或: 异或的特点: 相同为0, 不同为1, “并且异或的顺序不影响结果” 即 A ^ B ^ A ^ B ^ C = A ^ A ^ B ^ B ^ C.. """ deffun(nums): to_return = 0# 之所以初始化为0, 因为 如果 0 与 01010110 异或的话, 原值不变, e.g. 0 ^ 34 = 34 for i inrange(len(nums)): to_return = to_return ^ nums[i] return to_return
// high 64 bits and low 64bits long high = 0; long low = 0;
for (Character c : target.toCharArray()){ // check use high bits or low bits if (c >= 64){ long toBit = 1l << (c-64); if ((high & toBit) != 0){ returnfalse; } // if not return, then update high bits to record a character high |= toBit; } else{ long toBit = 1l << c;