Leetcode 260. Single Number III JAVA语言

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?

题意:一个数组中的元素都出现了两次,只有俩数字出现了一次!!!!找出来!!!!!!

public class Solution {
    public int[] singleNumber(int[] nums) {
        int[] ret=new int[2];
        if(nums==null || nums.length<4)return nums;
        int norresult=0;
        for(int i=0;i<nums.length;i++){
            norresult^=nums[i];
        }
        ///找到1的索引位置
        int indexof1=index1(norresult);
        for(int i=0;i<nums.length;i++){
            if(is1(nums[i],indexof1)==1){
            ////一个只与是1的做异或
                ret[0]^=nums[i];
            }
        }
        ret[1]=norresult^ret[0];
        return ret;
    }
    public static int is1(int num,int index){
    ////判断第index位是不是1
        num=num>>index;
        return num&1;
    }
    public static int index1(int num){
    /////判断第几位是1 
        int index=0;
        while((num&1)==0){
            num=num>>1;
            index++;
        }
        return index;
    }
}

PS:这个可以参考single number1 的异或思想。不过此时需要分成两部分来找了。参见剑指offer。

时间: 2024-10-10 15:30:13

Leetcode 260. Single Number III JAVA语言的相关文章

[LeetCode] 260. Single Number III 单独数 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. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is

Leetcode 137. Single Number II JAVA语言

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题意:一个数组中除

Leetcode 260 Single Number III 亦或

在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&(-a)就是计算出这个数,可以参考树状数组. 最后就是注意(nums[i] & a) == 0要加().注意符号运算优先级 1 class Solution { 2 public: 3 vector<int> singleNumber(vector<int>& nu

[LeetCode] 260. Single Number III(位运算)

传送门 Description 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].

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. 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? Subscribe to see which co

[LeetCode][JavaScript]Single Number III

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]

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

260. Single Number III [medium] (Python)

题目链接 https://leetcode.com/problems/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