`
leonard1853
  • 浏览: 85699 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

LeetCode 260. Single Number III

阅读更多

升级版:

 

260. 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:

  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?

 

分析:和前两篇的成对出现找出一个单独的数不同,本题需要找出两个单独的数,这里就有一个坑:若是成对的数出现,相减等于0,这没有问题;但相减不等于0 的两个数,一定就是要找的那两个单独的数吗?

 

答案肯定不是,因为这两个单独出现的数可以出现在排序后的头、尾或者中间,简单的用步长2相减,只可能找到一个,但下一个步长就乱了。

解决方案:1、排序;

                  2、若是两个数相减等于0,步长+2

                  3、若是两个数相减不等于0,步长+1,并记录第一个数,即第一个孤独数

                  4、若是最后还有数剩余,即为第二个孤独数。

                  5、返回结果

 

public class Solution {
    public int[] singleNumber(int[] nums) {
        Arrays.sort(nums);
        
        List<Integer> result = new ArrayList<Integer>();
        int index = 0;
        while(index < nums.length - 1) {
            if (nums[index] - nums[index + 1] == 0) {
                index += 2;
            } else {
                result.add(nums[index]);
                index += 1;
            }
        }
        if (index < nums.length) { // 收尾
            result.add(nums[index]);
        }
        
        Integer[] ret = (Integer[])result.toArray(new Integer[0]);
        
        return new int[]{ret[0].intValue(), ret[1].intValue()};
    }
}

 

分享到:
评论

相关推荐

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

    颜色分类leetcode leetcode.etc My solutions of the problems in Online judge website, leetcode, lintcode, etc. leetcode: 13 problems solved lintcode: 49 problems solved Title URL Solution Difficulty ...

    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 Single Number II - 位运算处理数组中的数 - 代金桥 - 博客园1

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

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

    在这段代码中,我们首先定义了一个名为`singleNumber`的函数,接受一个数组`nums`作为参数。我们初始化一个名为`result`的变量为0,这将作为我们进行异或运算的累积结果。然后,我们遍历数组中的每一个数字,并且...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ ...Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of Binary Tree Same Tree

    Leetcode book刷题必备

    根据提供的文件信息,我们能提炼出一系列IT相关知识点,主要是围绕LeetCode这本电子书的主题——即编程面试题目解答。此电子书内容丰富,涵盖了算法和数据结构中常见的问题及其解决方案,非常适合准备技术面试的读者...

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

    在这些题目中,"Single Number II"(题目编号137)是一个在编程面试中经常出现的题目,它要求找出数组中唯一的数字,该数字在数组中出现三次,其余每个数字都出现一次。 Python作为一种简洁高效的编程语言,被广泛...

    python-leetcode题解之136-Single-Number

    2. LeetCode平台:LeetCode是一个提供算法练习和面试准备的在线平台,提供各种难度的编程题目,题型涵盖数组、字符串、链表、树、图等数据结构和动态规划、回溯、贪心算法等算法问题。 3. 单一数字问题:在LeetCode...

    C++:异或运算符大全

    3. LeetCode No260. Single Number III 问题描述:给定一个数组,其中有两个数字只出现一次,其他数字出现两次,找到这两个只出现一次的数字。 解决方案:首先,对数组中的所有元素进行异或操作,得到这两个单数的...

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

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

    php-leetcode题解之只出现一次的数字.zip

    function singleNumber($nums) { $result = 0; foreach ($nums as $num) { $result ^= $num; } return $result; } ``` 这段代码中,`$result`初始值为0,然后遍历数组`$nums`,对每个元素执行异或操作,最终...

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

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 冒泡排序 快速排序 ##正好看见LeetCode可以刷Swift的题目 开始慢慢刷 swift有playground 做起来还是相当方便的 已完成题目 ----2016.9.30 两...

    python-leetcode面试题解之第136题只出现一次的数字-题解.zip

    def singleNumber(nums): single_num = 0 for num in nums: single_num ^= num return single_num ``` 在这个代码中,`single_num`初始值为0,然后遍历整个数组`nums`,对每个元素执行异或操作。由于每个元素...

    Leetcode部分试题解析

    3. **Single Number**:在一个只包含两个重复数字的整数列表中找到唯一的单个数字。利用Python的位运算,可以实现快速查找单数。 4. **Same Tree**:判断两棵树是否结构相同且对应节点值相等。这涉及到深度优先搜索...

    leetcode切割分组-leetcode:leetcode

    136_single_number.py # 位操作:异或(xor)操作 x ^ 0 = x; x ^ x = 0 sum 001_two_sum.py # 求list中能加和成指定值的两个位置 015_3_sum**.py # 求list中能加和成0的三个值 数列 004_median_of_two_sorted_arrays....

    leetcode解题笔记1

    int singleNumber(vector&lt;int&gt;& nums) { int result = 0; for (int num : nums) { result ^= num; } return result; } ``` 2. **使用异或查找字符串中的新增字符**: 类似于找到数组中唯一数字的方法,...

    js-leetcode题解之有序数组的单个元素-题解.zip

    function singleNumber(nums) { let i = 0, j = nums.length - 1; while (i ) { if (nums[i] === nums[j]) { i++; j--; } else { return nums[i] !== nums[++i] ? nums[i] : nums[j]; } } return nums[i];...

    算法-leetcode-剑指offer上的题很多

    - **单一数字问题(Single Number)**: 找出数组中唯一的不重复的数字。 - **快乐数(Happy Number)**: 判断一个数字是否快乐。 - **二进制中的1的个数(Count 1 in Binary)**: 计算一个整数的二进制表示中有多少个1。 ...

    LeetCode2 Add Two Numbers

    The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading ...

Global site tag (gtag.js) - Google Analytics