LeetCode *** 137. Single Number II

来源:互联网 发布:杜兰特各赛季数据统计 编辑:程序博客网 时间:2024/05/01 11:16

题目:

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?


分析:

数电里面学过这部分的知识,无奈我都忘得差不多了。画状态转换图画了好久也没对。我对不起我数电老师。


代码:

class Solution {public:    int singleNumber(vector<int>& nums) {                int high=0,low=0;        int size=nums.size();                for(int i=0;i<size;++i){            high=(high^nums[i])&~low;            low=(low^nums[i])&~high;        }        return high;            }};

0 0
原创粉丝点击