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 I 中使用了异或一个数两次,原值不变的性质,即将出现两次的数值当做没出现;同样,如何将出现三次的数字当做没出现,是本题的思路之一。

解题的方法是,将每个整数看做32位,记录每一位上面的位值出现次数;由于只有一个数出现一次,其余数出现3次;因此当一个位上出现3的倍数次时,证明此位不在single number的位序列中,所有出现3的倍数加1次的位,共同组成了single number。

实现方法1:

使用三个int变量,once、twice、thrice分别记录每一位上位值出现的次数;最终once上余留的值,合起来就是只出现一次的数;

 class Solution {
public:
int singleNumber(int A[], int n) {
int once = , twice = , thrice = ;
for (int i = ; i < n; i++) {
// 当某位上once和A[i]同时为1时,twice此位设置为1,否则不变
twice |= once & A[i];
// 先不考虑thrice的存在,和twice配合,10为2次,11为3次
once ^= A[i];
// 如果once和twice某位均为1,说明已经达到3次,则置thrice为1,否则为0;
thrice = once & twice;
// 反转thrice再和once与,即当thrice中某位为1时,将once的此位置为0
once &= ~thrice;
// 反转thrice再和twice与,即当thrice中某位为1时,将twice的此位置为0
twice &= ~thrice;
}
return once;
}
};

实现方法2:

和方法1类似,用两个int变量合起来表示某位上值出现的次数,即:

00不出现、01出现一次、10出现两次、11出现三次;

当出现三次时,清除此位上的数字;

代码略

【Leetcode】【Medium】Single Number II的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

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

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

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

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

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

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

  5. 【leetcode78】Single Number II

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

  6. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

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

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

  10. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

随机推荐

  1. 关于vue2非表单元素使用contenteditable="true"实现textarea高度自适应

    <template> <div ref="sendContent" contenteditable="true" v-html="s ...

  2. pingUtil

    package unit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR ...

  3. git 项目常用命令

    git remote -v 查看对应的远程仓库 git fetch origin master 将某个远程主机的更新,全部取回本地 git merge origin master 合并分支 git a ...

  4. EntityFramework 建立一对一关系

    前言:本来要使用实体拆分实现一对一,但发现查询时无法单独查询,影响效率,故改用手动建立一对一关系 例: 实体类: public class TestDbContext : DbContext { pu ...

  5. Notepad++编译和运行Java

    首先要让Notepad++编译和运行Java,前提是电脑里已经配置好了Java的环境(这里可以参考我博客里关于Java环境配置的那篇随笔). 在Notepad++上面的选项栏中找到 插件---> ...

  6. PIE SDK打开矢量数据

    1. 功能简介 GIS将地理空间数据表示为矢量数据和栅格数据.矢量数据模型使用点.线和多边形来表示具有清晰空间位置和边界的空间要素,如控制点.河流和宗地等,每个要素被赋予一个ID,以便与其属性相关联. ...

  7. 金融量化分析-python量化分析系列之---使用python获取股票历史数据和实时分笔数据

    财经数据接口包tushare的使用(一) Tushare是一款开源免费的金融数据接口包,可以用于获取股票的历史数据.年度季度报表数据.实时分笔数据.历史分笔数据,本文对tushare的用法,已经存在的 ...

  8. 3.CAS原子操作

    什么是原子性操作,按照官方的解析:原子操作不能在一个中间操作中停止,要么全部成功要么全部失败.(An atomic action cannot stop in the middle: it eithe ...

  9. URL篇之不安全字符处理

    URL是可移植的.它要统一命名因特网上的所有资源,就需要通过各种不同的协议来传送这些资源.这些协议在传输数据时会使用不同的机制,所以,设计URL,使其可以通过任意因特网协议安全地传输是很重要的. UR ...

  10. 图解 I帧,B帧以及P帧

    I‑frame (Intra-coded picture): 即完整的一张图片 P‑frame (Predicted picture): 与前面一张图片的区别的区域 B‑frame (Bidirect ...