首页 > 学院 > 开发设计 > 正文

Single Number II

2019-11-06 07:11:00
字体:
来源:转载
供稿:网友

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 run time complexity. Could you implement it without using extra memory?

class Solution {public:    int singleNumber(vector<int>& nums) {        int r = 0;        int len = nums.size();        for(int i=0;i<32;i++){            int count = 0;            int mask = (1<<i);            for(int n=0;n<len;n++){                if(nums[n] & mask){                    count += 1;                }            }            if(count%3){                r |= mask;            }        }        return r;    }};


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表