【本文链接】

http://www.cnblogs.com/hellogiser/p/single-number-of-array-with-other-three-times.html

【题目】

  int类型数组中除了一个数出现一次或两次以外,其他数都出现三次,求这个数。

【分析】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
 
int singleNumber(int *a, int n)
{
    ;
    ; i < n; i++)
    {
        ones = (ones ^ a[i]) & (~twos);
        twos = (twos ^ a[i]) & (~ones);
    }
    // ones
    return ones;
}

  当a出现一次的时候,ones能保存a。当a出现两次的时候,twos能保存a。当a出现三次的时候,ones和twos都清零。所以,如果一个数值中所有的数都通过这个循环的话,出现三次的数都清零了,有一个数如果出现一次,它保存在ones中;如果出现两次的话保存在twos中。

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
// 75_OneTwoNumber_WithOther3Times.cpp : Defines the entry point for the console application.
//
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/18
*/

#include "stdafx.h"
#include "iostream"
using namespace std;

void print(int *a, int n)
{
    ; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}

//==============================================================
// single number    1,1,1,2========>2
//==============================================================
/*
x 1st, ones =x, twos =0
x 2st, ones =0, twos =x
x 3st, ones =0, twos =0
*/
int singleNumber(int *a, int n)
{
    ;
    ; i < n; i++)
    {
        ones = (ones ^ a[i]) & (~twos);
        twos = (twos ^ a[i]) & (~ones);
    }
    // ones
    return ones;
}

//==============================================================
// double number    1,1,1,100,100========>100
//==============================================================
int doubleNumber(int *a, int n)
{
    ;
    ; i < n; i++)
    {
        ones = (ones ^ a[i]) & (~twos);
        twos = (twos ^ a[i]) & (~ones);
    }
    // twos
    return twos;
}

void test_base_single()
{
    };
    ;
    cout << "array is: ";
    print(a, n);
    cout << "single number " << singleNumber(a, n) << endl;
}

void test_base_double()
{
    };
    ;
    cout << "array is: ";
    print(a, n);
    cout << "double number " << doubleNumber(a, n) << endl;
}

void test_main()
{
    test_base_single();
    test_base_double();
}

int _tmain(int argc, _TCHAR *argv[])
{
    test_main();
    ;
}
/*
array is: 1 1 1 2
single number 2
array is: 1 1 1 100 100
double number 100
*/

75 int类型数组中除了一个数出现一次或两次以外,其他数都出现三次,求这个数。[2行核心代码]的更多相关文章

  1. 在一个升序数组中添加最少的数字,使得从1--n之间所有的数都能用数组中几个数的和表示

    一个Java的笔试题上面遇到的题,当时没有做出来. 拆分: 序列升序 1--n所有的数都要能表示 用数组中数字的和表示 添加最少的数字 思路:这个要先从小的数开始表示,因为大的数可以用小数表示. 1- ...

  2. Android java传递int类型数组给C

    接着前面的文章<Android java传递int类型数据给C><Android java传递string类型数据给C>,继续实践 实现public native int[] ...

  3. 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数

    今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...

  4. 【C语言】给一组组数,仅仅有两个数仅仅出现了一次,其它全部数都是成对出现的,找出这两个数。

    //给⼀组组数,仅仅有两个数仅仅出现了一次.其它全部数都是成对出现的,找出这两个数. #include <stdio.h> int find_one_pos(int num) //找一个为 ...

  5. (转)js在数组中删除重复的元素自保留一个(两种实现思路)

    例如:var student = [‘qiang','ming','tao','li','liang','you','qiang','tao']; 第一种思路是:遍历要删除的数组arr, 把元素分别放 ...

  6. 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

    class Solution {     public int[] twoSum(int[] nums, int target) {         for (int i = 0; i < nu ...

  7. Java中只有按值传递,没有按引用传递!(两种参数情况下都是值传递)

    今天,我在一本面试书上看到了关于java的一个参数传递的问题: 写道 java中对象作为参数传递给一个方法,到底是值传递,还是引用传递? 我毫无疑问的回答:“引用传递!”,并且还觉得自己对java的这 ...

  8. 剑指Offer - 九度1351 - 数组中只出现一次的数字

    剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...

  9. 常用的函数式接口_Supplier和常用的函数式接口Supplier接口练习_求数组中元素最大值

    Supplier接口 package com.yang.Test.SupplierStudy; import java.util.function.Supplier; /** * 常用的函数式接口 * ...

随机推荐

  1. WPF控件--利用Winform库中的NotifyIcon实现托盘小程序

    WPF控件--NotifyIcon   运行界面如下所示:            图1                                             图2 代码很少,如下所示 ...

  2. codevs 1690 开关灯 线段树水题

    没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...

  3. hdu1873优先队列

    #include<stdio.h> #include<queue> using namespace std; struct node { int id; int val; fr ...

  4. Maven-在eclipse创建maven项目

    在eclipse使用maven则需要给eclipse安装maven插件,具体安装maven插件安装相关文章 构建Maven项目 以eclipse3.6为例 1)创建简单Maven项目 点击Eclips ...

  5. JAVA输入一个整数,求出其所有质因数

    首先得求出能整除A的数,再判断I是否是质数!!! import java.util.*; public class aa { public static void main(String[] args ...

  6. POJ2309BST(树状数组)

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9182   Accepted: 5613 Description C ...

  7. jquery------导入jquery.2.2.3.min.js

    问题: 导入jquery.2.2.3.min.js后MyEclipse会提示代码有错误 方法: 选中jquery.2.2.3.min.js->右键->选择“MyEclipse”中的“Exc ...

  8. windows2003安全加固脚本

    @echo off title= Windwos/index.html' target='_blank'>Windows Security echo. echo **************** ...

  9. Spring入门_01

    <bean id="userAction" class="com.umgsai.spring.UserAction"> <property n ...

  10. 【Derby 系列】Apache Derby 功能特点

    前言 进入Derby 的应用开发之前,浏览一下derby的功能集,这样在真正选择的时候,可以决定Derby是不是适合你的应用. 本篇的是[Derby 系列]初级的第3篇. 1. Derby 是关系型数 ...