`

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?

给定一个数组,里面有两个数字出现了一次,其他都出现了两次,找出这两个元素,要求在线性时间内完成。我们用位运算来完成,首先将数组中所有的元素进行异或运算得到一个值helper,然后用helper & (~(helper - 1)) 这样就得到了一个某一位为1其他位全为0的数tell,并且1所在的位上两个单独出现的数肯定不同。我们通过tell将数组中的元素分为两部分,分别与tell进行位与运算,最终得到两个单独的数。代码如下:
public class Solution {
    public int[] singleNumber(int[] nums) {
        if(nums == null || nums.length < 2) return new int[0];
        int helper = 0;
        for(int i = 0; i < nums.length; i++) {
            helper ^= nums[i];
        }
        int tell = helper & (~(helper - 1));
        int single1 = 0;
        int single2 = 0;
        for(int i = 0; i < nums.length; i++) {
            if((nums[i] & tell) == 0) {
                single1 ^= nums[i];
            } else {
                single2 ^= nums[i];
            }
        }
        int[] result = {single1, single2};
        return result;
    }
}
0
2
分享到:
评论

相关推荐

    Single Number调试用demo

    《Single Number算法详解与调试演示》 在编程领域,算法是解决问题的核心工具,而Single Number问题则是一个典型的算法题目。这个题目要求在一个整数数组中,找出唯一出现一次的数字,而其他数字都出现了两次。这是...

    leetcode简单+中等题目参考答案java版

    public int singleNumber(int[] nums) { int res = 0; for (int n : nums) { res = res ^ n; } return res; } ``` **解析:** 这段代码巧妙地利用了异或运算的特性来解决问题。异或运算有以下特点: 1. 任何数...

    C++:异或运算符大全

    vector&lt;int&gt; singleNumber(vector&lt;int&gt;& nums) { int xor_two = nums[0]; int last_bit = 0; vector&lt;int&gt; result = {0,0}; for(int i = 1; i (); i++) xor_two ^= nums[i]; last_bit = xor_two & (~(xor_two ...

    leetcode Single Number II - 位运算处理数组中的数 - 代金桥 - 博客园1

    《位运算处理数组中的数——以LeetCode Single Number II为例》 在计算机科学中,位运算是一种高效且灵活的数据处理手段,尤其在处理数组中特定数值的问题时,它能展现出强大的能力。LeetCode上的Single Number II...

    颜色分类leetcode-leetcode.etc:OJ、leetcode等解决方案

    Number(落单的数) 、 / Medium Single Number II(落单的数 II) 、 Medium Single Number III(落单的数 III) Medium Hash Function(哈希函数) Easy Space Replacement(空格替换) Easy Insert Interval Easy Two ...

    手稿_V1.097

    标题 "手稿_V1.097" 涉及的是一个编程问题,该问题源自 LeetCode 上的 "Single Number III" 题目。在 C++ 的背景下,我们需要找到一个整数数组中只出现一次的两个元素,而其他元素都出现两次。描述中的代码给出了一...

    python-leetcode题解之136-Single-Number

    python python_leetcode题解之136_Single_Number

    LeetCode最全代码

    260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same ...Single Number ...Number ...Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P

    js-leetcode题解之136-single-number.js

    javascript js_leetcode题解之136-single-number.js

    python-leetcode题解之137-Single-Number-II

    python python_leetcode题解之137_Single_Number_II

    RandomNumber

    在Xcode中,开发者可以创建一个新的项目,选择Single View App模板,然后在主视图控制器中编写代码来实现随机数的生成。 在Objective-C中,我们可以使用`arc4random_uniform()`函数来生成指定范围内的随机数。这个...

    leetcode338-LeetCode:力码

    只出现一次的数字 III(Single Number III)**:这是一道关于数组和位操作的题目,要求找出数组中唯一一个只出现一次的数字,而其他数字都出现两次。可以利用异或操作实现。 7. **338. 计数质数(Counting Bits)**:...

    B04_2841_EX08.zip_2841_Number Ten

    As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve...

    Make3D: Learning 3D Scene Structure from a Single Still Image

    We consider the problem of estimating detailed 3D structure from a single still image of an unstructured environment. Our goal is to create 3D models that are both quantitatively accurate as well as ...

    只出现一次的数字(java代码).docx

    - `Solution` 类包含了一个名为 `singleNumber` 的方法,该方法接收一个整型数组 `nums` 作为参数。 - 在 `singleNumber` 方法内部,定义了一个变量 `result` 初始化为 0。 - 使用增强型 for 循环遍历数组 `nums`,...

    three_hidden_regression_1.rar_Regression number_desired _voice c

    Deep Mixture density network for voice conversion. Single layer Multi Layer Perceptron Neural Network with desired number of mixure components.

    C语言实验作业

    As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve...

    PAT甲级 1024 Palindromic Number

    PAT甲级 1024 Palindromic Number A number that will be the same when it is written... All single digit numbers are palindromic numbers. Non-palindromic numbers can be paired with palindromic ones via a se

Global site tag (gtag.js) - Google Analytics