LeetCode——Single Number(II)

Single Number

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

Single Number II

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?

思路:

这种类型的题目有两种思路:1、Map映射。2、位运算。

1、Map映射

可以使用使用map统计所有数值出现的次数,然后最后返回满足条件的值。

代码:

Single Number:

class Solution {
public:
    int singleNumber(int A[], int n)
    {
        unordered_map<int, bool> ump_ii;
        for (int i = 0; i < n; i++)
        {
            if (!ump_ii.count(A[i])) ump_ii[A[i]] = true;
            else ump_ii.erase(A[i]);
        }
        return ump_ii.begin()->first;
    }
};

Single Number II:

class Solution {
public:
	int singleNumber(int A[], int n) {
		unordered_map<int, int> m;
		for (int i = 0; i < n; i++) {
			if (m.count(A[i]) == 0) {
				m[A[i]] = 1;
			} else if (m[A[i]] == 1) {
				m[A[i]] = 2;
			} else if (m[A[i]] == 2) {
				m.erase(A[i]);
			}
		}
		return m.begin()->first;
	}
};

2、位运算

Single Number:两个相同数的异或(^)值为0,Single Number中可以将所有的数求异或运算,那么值为所要求的Single Number。

所以代码为:

class Solution {
public:
    int singleNumber(int A[], int n) {
        int num=A[0];
        for(int i=1;i<n;i++){
            num^=A[i];
        }
        return num;
    }
};

Single Number II:对于除了出现一次之外的所有的整数,其二进制表示中每一位1出现的次数是3的整数倍,将所有这些1清零,剩下的就是最终的数。用ones记录到当前计算的变量为止,二进制1出现“1次”的数位。用twos记录到当前计算的变量为止,二进制1出现“2次”的数位。当ones和twos中的某一位同时为1时表示二进制1出现3次,此时需要清零。即用二进制模拟三进制计算。最终ones记录的是最终结果

代码:

class Solution {
public:
	int singleNumber(int A[], int n)
    {
        int ones=0,twos=0,threes=0;
        for(int i=0;i<n;i++){
            twos|=(ones&A[i]);
            ones=ones^A[i];
            threes=(ones&twos);
            twos&=~threes;
            ones&=~threes;
        }
        return ones;
    }
};

当然进行位运算的时候我们可以将整数(int 32)一位一位运算。

代码:

class Solution {
public:
	int singleNumber(int A[], int n)
    {
        int ans = 0;
        for (int i = 0; i < 32; i++)
        {
            int c = 0, d = 1<<i;
            for (int j = 0; j < n; j++)
                if (A[j] & d) c++;  

            if (c%3) ans |= d;
        }
        return ans;
    }
};

LeetCode——Single Number(II)

时间: 2024-08-02 04:43:03

LeetCode——Single Number(II)的相关文章

LeetCode Single Number I II Python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. def singleNumber(self, A): l = len(A) if l < 2: return A[0] A.sort() for i in range(0,l-1,2): if A[i] != A[i+1]: return A[i] return A[l-1] 思路:

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz

LeetCode: Single Number I &amp;&amp; II

I title: Given an array of integers, every element appears twice 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

LeetCode&mdash;&mdash;Single Number(找出数组中只出现一次的数)

问题: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?   分析: 数组中的数除了一个只出现了一次之外,其它都出现了两次, 要找出只出

leetCode: Single Number II [137]

[题目] 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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded