2.1.24—线性表—Single Number II

来源:互联网 发布:数学画图软件 编辑:程序博客网 时间:2024/06/13 20:05
描述
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?


#include<iostream>using namespace std;int SingleNUmII(int a[], int n){int bits = sizeof(int) * 8;int res = 0;for (int i = 0; i < bits; i++){int temp = 0;for (int j = 0; j < n; j++){temp += (a[j] >> i) & 1;temp %= 4;//changge it 3.}res += temp << i;}return res;}int main(){const int n = 13;int a[n] = { 2, 5, 9, 9, 5, 2, 5, 2, 9, 188,2,9,5 };int res = SingleNUmII(a, n);cout << res << endl;}


原创粉丝点击