`
hcx2013
  • 浏览: 88677 次
社区版块
存档分类
最新评论

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].

 

public class Solution {
    public int[] singleNumber(int[] nums) {
        int a = 0;
        int b = 0;
        for (int i : nums) {
			a ^= i;
		}
        int rightOne = a & (~a + 1);
        for (int i : nums) {
			if ((rightOne & i) != 0) {
				b ^= i;
			}
		}
        return new int[]{b, a^b}; 
    }
}

 

0
6
分享到:
评论
2 楼 hcx2013 2015-11-02  
mircle 写道
Given nums = [1, 2, 1, 3, 2, 5,3], return [5, 0]

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

我没做输入数组的校验,你这个输入序列有问题,谢谢提醒
1 楼 mircle 2015-11-02  
Given nums = [1, 2, 1, 3, 2, 5,3], return [5, 0]

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

相关推荐

    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<int> singleNumber(vector<int>& nums) { int xor_two = nums[0]; int last_bit = 0; vector<int> 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