题目:

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?

解题思路:

这题比Single Number稍难些,不能用异或解决,但排序和bitmap还是可以的,只是时间复杂度和空间复杂度要多些

这里我用另一种方式实现,根据所给数组中元素的规律,可利用每一bit位上1的个数进行解决,直接看代码吧

实现代码:

#include <iostream>

using namespace std;
/*
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(int A[], int n) {
int once = ;
for(int i = ; i < ; i++)
{
int one_num = ;//bit为第i位1的个数
for(int j = ; j < n; j++)
if((A[j] >> i) & == )
one_num++;
//因为数组中只有一个数出现一次,其他数都出现三次,
//所以除非要找数的当前bit位为1,否则one_num为3的倍数
if(one_num % )
once += ( << i); }
return once; }
}; int main(void)
{
int arr[] = {,,,,,,,,,};
int len = sizeof(arr) / sizeof(arr[]);
Solution solution;
int once = solution.singleNumber(arr, len);
cout<<once<<endl;
return ;
}

LeetCode137:Single Number II的更多相关文章

  1. leetcode 之 Single Number II

    问题来源:Single Number II 问题描述:给定一个整数数组,除了一个整数出现一次之外,其余的每一个整数均出现三次,请找出这个出现一次的整数. 大家可能很熟悉另一个题目(Single Num ...

  2. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

  3. LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

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

  4. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  5. 【题解】【位操作】【Leetcode】Single Number II

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

  6. Single Number,Single Number II

    Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...

  7. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  8. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  9. 【LeetCode】137. Single Number II (3 solutions)

    Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...

随机推荐

  1. quartz2.2.1 web配置

    首先到http://www.quartz-scheduler.org/downloads/catalog下载工具包 将下载包中lib目录下的所有jar包导入到项目中 因为quartz依赖log4j,需 ...

  2. hdoj1078(介绍记忆化搜索及其模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 思路: 这是一道典型的记忆化搜索模板题. 先介绍记忆化搜索,本质是搜索+DP. 一般说来,动态规 ...

  3. OpenCV(图像处理)—访问像素的三种方法

    方法一:用指针访问像素 #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include < ...

  4. [leetcode]269. Alien Dictionary外星字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  5. discuz回贴通知插件实现-发送邮件

    通过discuz的sendmail()来发送邮件 //引入发送邮件的函数文件 include libfile('function/mail'); //设置收件人地址,标题,内容发送邮件 sendmai ...

  6. C#控制台进度条(Programming Progress bar in C# Consle application)

    以下代码从Stack Overflow,觉得以后会用到就收藏一下,我是辛勤的搬运工,咿呀咿呀哟- 1.showing percentage in .net console application(在. ...

  7. socket编程介绍

    Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...

  8. Codeforces 689B. Mike and Shortcuts SPFA/搜索

    B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...

  9. spring 注解 注入属性 和 注解完成bean定义

    1. 使用 @Autowired 和 @Resource 注解来注入属性 2. 使用 @Component.@Repository.@Service.@Controller 注解,就将该类定义为一个B ...

  10. C++对象拾遗

    #include <iostream.h> #include <string.h> //using namespace std; class A { public:     A ...