【Single Num II】cpp

题目

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 result = 0;
            const int W = sizeof(int)*8;
            int *count = new int[W]();
            for (size_t i = 0; i < nums.size(); ++i)
            {
                for (size_t j = 0; j < W; ++j)
                {
                    count[j] += (nums[i] >> j) & 1;
                }
            }
            for (size_t i = 0; i < W; ++i)
            {
                if ( count[i]%3!=0 )
                {
                    result += ( 1 << i);
                }
            }
            delete []count;
            return result;
    }
};

Tips:

1. 算法原理:由于都是int型的,只需要记录每个bit上1出现的次数;如果1出现的次数不是3的倍数,则将该位置置为1,并赋值到result中。

这道题主要是熟悉下位运算的各种操作,有时巧用位运算,可以大大提升代码效率。

时间: 2024-11-05 13:29:05

【Single Num II】cpp的相关文章

【Unique Paths II】cpp

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

【Word Break II】cpp

题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats&q

【Jump Game II 】cpp

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

【Path Sum II】cpp

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 代码: /** * Definition f

【Spiral Matrix II】cpp

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 代码: class Solution { public: vector<vector<

【palindrome partitioning II】cpp

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa&qu

【Pascal&#39;s Triangle II 】cpp

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码: class Solution { public: vector<int> getRow(int rowIndex) { vector

【大话存储II】学习笔记(15章),文件级集群系统

[大话存储II]学习笔记(15章),块级集群存储系统里面分析的主要是块集群系统,同样文件级存储也可以集群化. 因为NAS系统的前端网络是以太网,速度比较低,导致NAS主要用于一些非关键业务中,比如文件共享.但是一些特殊应用也需要多主机同时访问某个大文件,比如3D渲染集群等.如果我们使用块集群存储系统,则会存在一个问题,需要在应用程序上引入文件锁,而NAS的文件系统一般都自带有文件锁机制,所以还不如把NAS存储系统进行集群化. 在谈怎么样把NAS系统进行集群化之前,我们说说集群文件系统的架构.集群

【Pascal&#39;s Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector