国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Single Number III

lanffy / 2939人閱讀

摘要:去掉最后一個(gè)保留最后一個(gè)保留最后一個(gè)保留第一個(gè)這道題在論壇里參考了和兩位仁兄的解法。思想是將中所有的數(shù)進(jìn)行異或運(yùn)算。不同的兩個(gè)數(shù)異或的結(jié)果,一定至少有一位為。最后,將和存入數(shù)組,返回。

Problem

Given 2*n + 2 numbers, every numbers occurs twice except two, find them.

Example

Given [1,2,2,3,4,4,5,3] return 1 and 5

Challenge

O(n) time, O(1) extra space.

Note

n & (n-1): 去掉最后一個(gè)1;
n & (-n): 保留最后一個(gè)1;
n & (~(n-1)): 保留最后一個(gè)1;
Integer.highestOneBit(n): 保留第一個(gè)1;

這道題在LC論壇里參考了huangjingshu和zhiqing_xiao兩位仁兄的解法。思想是:
將A中所有的數(shù)進(jìn)行異或運(yùn)算。不同的兩個(gè)數(shù)異或的結(jié)果diff,一定至少有一位為1。我們只保留diff中的一個(gè)1,可以是第一個(gè)1,也可以是最后一個(gè)1,假設(shè)這個(gè)1在第i位好了!
然后再次將A中所有的數(shù)numdiff相與:
num & diff的結(jié)果為diff,說明這些num的第i位是1:將numres0異或后存入res0,這樣最后異或出來的結(jié)果就是兩個(gè)single number中第i位是1的那個(gè);
同理,若num & diff的結(jié)果為0,說明這些num的第i位是0:將numres1異或后存入res1,這樣最后異或出來的結(jié)果就是兩個(gè)single number中第i位是1的那個(gè)。
最后,將res1res0存入res數(shù)組,返回。

Solution
public class Solution {
    public List singleNumberIII(int[] A) {
        int diff = 0;
        for (int num: A) {
            diff ^= num;
        }
        //diff &= -diff;
        diff &= ~(diff-1);
        int res0 = 0, res1 = 0;
        for (int num: A) {
            if ((diff & num) == diff) res0 ^= num;
            else res1 ^= num;
        }
        List res = new ArrayList();
        res.add(res0);
        res.add(res1);
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/65846.html

相關(guān)文章

  • [LintCode/LeetCode] Single Number I & II [位運(yùn)算]

    摘要:整個(gè)過程相當(dāng)于,直接在和里去掉既是又是的。所以最后返回的,一定是只出現(xiàn)過一次的,而出現(xiàn)兩次的都在里,出現(xiàn)三次的都被消去了。 Single Number I Problem Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1,2,2,1,3,4,3], return...

    Drinkey 評論0 收藏0
  • [LintCode/LeetCode] Combination Sum I & II

    摘要:和唯一的不同是組合中不能存在重復(fù)的元素,因此,在遞歸時(shí)將初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...

    ThreeWords 評論0 收藏0
  • [LintCode/LeetCode] House Robber III

    摘要:解法真的非常巧妙,不過這道題里仍要注意兩個(gè)細(xì)節(jié)。中,為時(shí),返回長度為的空數(shù)組建立結(jié)果數(shù)組時(shí),是包括根節(jié)點(diǎn)的情況,是不包含根節(jié)點(diǎn)的情況。而非按左右子樹來進(jìn)行劃分的。 Problem The thief has found himself a new place for his thievery again. There is only one entrance to this area,...

    macg0406 評論0 收藏0
  • [LintCode/LeetCode] Contains Duplicate III

    Problem Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute dif...

    MageekChiu 評論0 收藏0
  • [LintCode/LeetCode] Binary Tree Maximum Path Sum

    摘要:調(diào)用函數(shù)更新路徑和的最大值,而函數(shù)本身需要遞歸,返回的是單邊路徑和。所以函數(shù)要返回的是,主函數(shù)中返回的卻是最上一層根節(jié)點(diǎn)處和的較大值,與之前遍歷過所有路徑的最大值之間的最大值。 Problem Given a binary tree, find the maximum path sum. The path may start and end at any node in the tre...

    cnTomato 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<