LeetCode OJ-- Single Number II **@

有一列数,其中有1个数出现了1次,其它数都出现了3次,求这个数。

class Solution {
public:
    int singleNumber(int A[], int n) {
        if(n <= 0 )
            return 0;

        int ans = 0;

        for(int i = 0; i < 32; i++)
        {
            int q = 0;
            for(int j = 0; j < n; j++)
            {
                int p = A[j] >> i & 1;
                q = (q + p ) % 3;
            }
            ans = ans | q <<i;
        }
        return ans;
    }
};
时间: 2024-08-01 05:11:59

LeetCode OJ-- Single Number II **@的相关文章

[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int bits = sizeof(int)*8; 5 int result=0; 6 for(int i=1; i<=bits; i++) 7 { 8 int w=0; 9 int t=1; 10 11 for(int j=0; j<n; j++) 12 w += (A[j]>>(i-1))&t; 13 result+= (w%3)<

[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int i,j; 5 for(i=0; i<n; i++) 6 { 7 for(j=i+1; j<n; j++) 8 { 9 if(A[j]==A[i]) 10 { 11 int temp = A[i+1]; 12 A[i+1] = A[j]; 13 A[j] = temp; 14 i++; 15 break; 16 } 17 } 18 if(j==n) 19

[LeetCode][JavaScript]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 algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/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 co

LeetCode 137 Single Number II(只出现一次的数字 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 complexity. Could you im

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So

LeetCode 137 Single Number II(仅仅出现一次的数字 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 complexity. Could you

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】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 complexity. Could you implement it without using extra memory? 思路: 用 ones, twos, threes 分别记录数字在二

Java [Leetcode 137]Single Number II

题目描述: Given an array of integers, every element appears three times except for one. Find that single one. 解题思路: 具体参考Detailed explanation and generalization of the bitwise operation method for single numbers 讲的实在是很全面,而且拓展到了一般的情况,膜!! 代码如下: public class