1. 程式人生 > >Single Number II問題及解法

Single Number II問題及解法

問題描述:

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?

問題分析:

陣列中的只有一個元素出現了一次,其他的元素均出現了三次,對於這些整數的每一位來說,它的位值也會相應的出現3次,即對3取餘問為0.

過程詳見程式碼:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for (int i = 0; i < 32; ++i) {
            int sum = 0;
            for (int j = 0; j < nums.size(); ++j) {
                sum += (nums[j] >> i) & 1;
            }
            res |= (sum % 3) << i;
        }
        return res;
    }
};


相關推薦

Single Number問題解法

問題描述: Given an array of integers, every element appearstwiceexcept for one. Find that single one. 問題

Single Number II問題解法

問題描述: Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Single Number II的位操作解法

看了很多篇這個問題的位操作解法,感覺都是一句帶過,自己模擬了下才真正明白。 leetcode原題,大意就是一個數組只有一個數出現了一次,其他數都出現3次,設計演算法找出這個出現一次的數。 位操作解法如下,時間複雜度O(n)。 int singleNumber(int A[

leetcode中Single Number II的一個通用解法

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algori

Single Number II

什麽 -a 操作 javascrip word www keyword java 異或運算 題目描寫敘述 鏈接地址 解法 算法解釋 題目描寫敘述 Given 3*n + 1 numbers, every numbers occu

【Leetcode】137. Single Number II

val tar urn arr ber and mos calc cal 題目: Given an array of integers, every element appears three times except for one. Find that single o

[LeetCode] 137. Single Number II (位運算)

清零 一次 .com blog 適用於 soft hat ever rip 傳送門 Description Given an array of integers, every element appears three times except for one, w

lintcode83- Single Number II- midium

特征 except find class n+2 urn 出現一次 情況 write Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Given [1,1,2,3,

137.Single Number II

cnblogs 耗時 get html -i scrip details 每一個 [0 題目鏈接:https://leetcode.com/problems/single-number-ii/description/ 題目大意:給出一串數,每個數都出現三次,只有一個數只出現

[LeetCode] 137. Single Number II 單獨數 II

input .cn targe 單獨 sin logs eve tput controls Given a non-empty array of integers, every element appears three times except for one, whic

LeetCode 137. Single Number II

without 當前 with 結果 example println ould 等於 記錄 分析 參考https://cloud.tencent.com/developer/article/1131945 我們想要達到的效果其實是——            a b 初

[Swift]LeetCode137. 只出現一次的數字 II | Single Number II

Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one

Leetcode137 Single Number II

136題的加強版,有三個相同數字出現了。 class Solution { public int singleNumber(int[] nums) { if(nums.length==1) return nums[0]; int ones=0,twos=0;

#Leetcode# 137. Single Number II

https://leetcode.com/problems/single-number-ii/   Given a non-empty array of integers, every element appears three times except

LeetCode137:Single Number II

Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that singl

python leetcode 136. Single Number 137. Single Number II

136. Single Number class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int

【LeetCode】#137只出現一次的數字II(Single Number II)

【LeetCode】#137只出現一次的數字II(Single Number II) 題目描述 給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現了三次。找出那個只出現了一次的元素。 說明: 你的演算法應該具有線性時間複雜度。 你可以不使用額外空間來實現嗎? 示

LeetCode Single Number II

Problem Given an array of integers, every element appears three times except for one, which appears exactly once. Find tha

[LeetCode] Single Number II 單獨的數字之二

Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime com

Happy Number問題解法

問題描述: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with