leetcode 刷题之路 84 Single Number II

Given an array of integers, every element appears three times
except for one. Find that single one.

给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数。

思路,根据数组构成的特点可知,数组中所有数的某一位上1的个数总和为3*n或者3*n+1。

当只出现一次的整数该位为0时,总和为3*n,当只出现一次的整数该位为1时,总和为3*n+1。

因此我们可以计算数组中所有整数的每一位上1的个数,然后进行取模运算就可以得出只出现一次的整数每一个位的值。

AC code:

class Solution {
public:
    int singleNumber(int A[], int n)
    {
        int res=0,numof1=0;
        for(int i=0;i<8*sizeof(int);i++)
        {
            for(int j=0;j<n;j++)
            {
                if((A[j]&1<<i)!=0)
                    numof1++;
            }
            if(numof1%3!=0)
                res|=1<<i;
            numof1=0;
        }
        return res;
    }
};

leetcode 刷题之路 84 Single Number II

时间: 2024-07-29 12:23:22

leetcode 刷题之路 84 Single Number II的相关文章

leetcode 刷题之路 66 Path Sum II

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] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到

#leetcode刷题之路40-组合总和 II

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说明:所有数字(包括目标数)都是正整数.解集不能包含重复的组合. 示例 1:输入: candidates = [10,1,2,7,6,1,5], target = 8,所求解集为:[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]] 示例 2:输入: candidates =

#leetcode刷题之路45-跳跃游戏 II

给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例:输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2. 从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置.说明:假设你总是可以到达数组的最后一个位置. 原文地址:https://www.cnblogs.com/biat/p/10664843.html

leetcode 刷题之路 95 N-Queens I

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. N皇后问题的变种,要求直接输出N皇后的解法数目.这道题可以在N-Queens I的基础上增加计数功能,在每求得一个成功的解时(行数为N时)使计数变量递增即可.题目不要求输出具体的解法,因此可以做一点优化,使用position数组用来表示皇后的位置,p

leetcode 刷题之路 83 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. 输入一个整形数组,数组里有正数也有负数,求所有子数组的和的最大

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

leetcode 刷题之路 92 Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级,求总共有多少总跳法. 分析: 一次最多只能跳两级,那么对于第n级(n>=2)台阶,显然只能从第n-1级跳一级到达或

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组