日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

260. Single Number III

發(fā)布時間:2024/9/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 260. Single Number III 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:

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?
  • 鏈接:?http://leetcode.com/problems/single-number-iii/

    題解:

    昨晚卡到現(xiàn)在。但昨晚睡覺前google了一下,看到了幾篇文章以后才理解。還是需要加強(qiáng)bit manipulation。 歸根結(jié)底是大學(xué)時數(shù)字電路沒學(xué)好 -_____-!! 我不堪的本科生涯...略去一億字。

    這里我們主要是先異或所有數(shù)字,得到 a^b, 因?yàn)閍和b不相等,所以 a^b里至少有一個bit位k,k = 1。 然后我們就可以用這個第k位來進(jìn)行判斷, 因?yàn)閍和b里只有一個數(shù)第k位 = 1,我們對輸入數(shù)組所有第k位為1的數(shù)字進(jìn)行異或,就可以找到第k位為1的僅出現(xiàn)一次的數(shù)字, 再用這個數(shù)字a和 a^b進(jìn)行異或,我們也就求得了b。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {public int[] singleNumber(int[] nums) {int[] res = {-1, -1};if(nums == null || nums.length == 0) {return res;}int a_XOR_b = 0; for(int i = 0; i < nums.length; i++) { // find a ^ ba_XOR_b ^= nums[i];}int k = 0; // find one bit in a^b == 1for(int i = 0; i < 31; i++) { // that means only one num in a, b has 1 on kth bitif(((a_XOR_b >> i) & 1) == 1) {k = i;break;}}int a = 0;for(int i = 0; i < nums.length; i++) { // since only one num in a, b has 1 on kth bitif(((nums[i] >> k) & 1) == 1) { // we can XOR all nums has 1 on kth bita ^= nums[i]; // duplicate nums will be evened out }}int b = a ^ a_XOR_b;res[0] = a;res[1] = b;return res;} }

    ?

    二刷:

    也是用一刷一樣的方法,比較tricky

    Java:

    public class Solution {public int[] singleNumber(int[] nums) {int[] res = {-1, -1};if (nums == null || nums.length == 0) {return res;}int a_XOR_b = 0;for (int i : nums) {a_XOR_b ^= i;}int k = 0;for (int i = 0; i < 32; i++) {if (((a_XOR_b >> i) & 1) == 1) {k = i;break;}}res[0] = 0;for (int i : nums) {if (((i >> k) & 1) == 1) {res[0] ^= i;}}res[1] = a_XOR_b ^ res[0];return res;} }

    ?

    題外話: ?

    其實(shí)本科時,學(xué)校開設(shè)的課程挺不錯的,老師也努力務(wù)實(shí),都怪自己沒把心思放在學(xué)習(xí)上。雖然專業(yè)是EE,但像什么基數(shù)排序,霍夫曼編碼,游程碼,最短路徑之類的東西,老師們也都多少介紹過。信息量很大,作業(yè)和考試也難。真要四年扎扎實(shí)實(shí)學(xué)下來的話,也不至于現(xiàn)在一把年紀(jì)了還在刷題。 過去的都過去了,接下來好好努力吧。

    ?

    三刷:

    這次就比較熟練了,嘗試一些不同的編程風(fēng)格。 ?4/4/2016。

    Java:

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {public int[] singleNumber(int[] nums) {int[] res = new int[2];if (nums == null || nums.length < 2) return res;int a_xor_b = 0;for (int num : nums) { a_xor_b ^= num; }int diffIndex = 0;while (diffIndex < 32) {if (((a_xor_b >> diffIndex) & 1) == 1) { break; }diffIndex++;}int num1 = 0;for (int num : nums) {if (((num >> diffIndex) & 1) == 1) { num1 ^= num; }}res[0] = num1;res[1] = a_xor_b ^ num1;return res;} }

    ?

    Update:

    public class Solution {public int[] singleNumber(int[] nums) {if (nums == null || nums.length < 2) return new int[] {-1, -1};int a_XOR_b = 0;for (int num : nums) a_XOR_b ^= num;int k = 0;while (k < 31) {if (((a_XOR_b >> k) & 1) == 1) break;k++;}int a = 0;for (int num : nums) {if (((num >> k) & 1) == 1) a ^= num;}return new int[] {a, a ^ a_XOR_b};} }

    ?

    ?

    Reference:

    https://groups.google.com/forum/#!topic/pongba/9KCA7b484OE

    https://groups.google.com/forum/#!topic/pongba/drV6dytcoJE

    http://www.matrix67.com/blog/archives/511

    ?

    與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的260. Single Number III的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。