【leetcode79】Single Number 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:

The order of the result is not important. So in the above example, [5, 3] is also correct.

Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

思路:

  • 考虑使用位运算

    -那数组分为两组,每个组只有一个出现一次的数字,单独进行异或处理,考虑用全部异或的方式

  • 假设得到异或的结果是A,由于两个数字不同。A肯定有一个位是1,找到那个位,然后用来把这个数组分为两组
  • 其中一个组的异或结果是B,输出【B,A ^ B】

代码:

public class Solution {
    public int[] singleNumber(int[] nums) {
        int xOne = 0;
        for (int x : nums) {
            xOne ^= x;
        }

        // 获取第一个位1的索引
        int k = 0;
        for (k = 0; k < Integer.SIZE; ++ k) {
            if (((xOne >>> k) & 1) == 1) {
                break;
            }
        }

        int xTwo = 0;
        for (int x : nums) {
            if (((x >>> k) & 1) == 1) {
                xTwo ^= x;
            }
        }
        return new int[] {xTwo, xOne ^ xTwo};
    }
}

更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

时间: 2024-08-24 09:38:30

【leetcode79】Single Number III的相关文章

【LeetCode】Single Number (2 solutions)

Single Number 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? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Single Number

原文: 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? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

【LeetCode】Single Number I &amp; II

Single Number 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? Solution: 解法不少,贴一种: 1 cla

【leetcode78】Single Number 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 m

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So

【LeetCode】Single Number 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? 思路: 用 ones, twos, threes 分别记录数字在二

【leetcode】Single Number (Medium) ☆

题目: 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? 要求O(n)算法,还不能有辅助空间.开始用了各种O(n^2)的方法,都超时,后来

【leetcode77】Single Number

题目描述: 给定一个数组,只有一个数字出现两次,判断那个数字 思路: 不断取出数据进行异或,最后一个数字,因为相同的数字会抵消 代码: public class Solution { public int singleNumber(int[] nums) { int left = nums[0]; for(int i =1; i< nums.length; i++) { left = left ^ nums[i]; } return left; } } 更多leetcode题目,请看我的leet

【Leetcode长征系列】Single Number 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? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除