CareerCup-5.7

An array A[1 n] contains all the integers from 0 to n except for one number which is 
missing In this problem, we cannot access an entire integer in A with a single operation The elements of A are represented in binary, and the only operation we can use 
to access them is “fetch the jth bit of A[i]”, which takes constant time Write code to 
find the missing integer Can you do it in O(n) time?

没有使用答案的递归的方法,尝试用异或在O(n)时间内解决

#include <iostream>
using namespace std;
#define N 6

class Array
{
public:
    Array()
    {
        for(int i=0; i<=N; i++)
        {
            data[i] = i;
        }
        data[0] ^= data[3];
        data[3] ^= data[0];
        data[0] ^= data[3];
    };
    bool fetchBit(int i, int j)
    {
        if(i < 1 || i > N || j < 0 || j > 30)
            return -1;
        return getBit(data[i], j);
    };
    void findMissing()
    {
        int t = 0;
        for(int i=1; i<=N; i++)
            t ^= i;
        int r = 0;
        int s;
        for(int j=30; j>=0; j--)
        {
            s=0;
            for(int i=1; i<=N; i++)
            {
                s ^= fetchBit(i, j);
            }
            r = (r << 1) + s;
        }
        cout<<(t ^ r)<<endl;
    };
private:
    int getBit(int d, int j)
    {
        return ((d & (1 << j)) >> j);
    };
    int data[N+1];
};
int main()
{
    Array array;
    array.findMissing();
    system("pause"); 
};

随后发现答案的代码有问题,第48和50行应该移位的是1和0,移位值是column。同时还需要将数据总数填充为2^n-1。于是修正了代码,并把自己的代码用Java实现了一下。

import java.util.ArrayList;

class BitInteger {
	BitInteger(int i) {
		data = i;
	}

	public static int INTEGER_SIZE = 31;
	Integer data;

	int fetch(int i) {
		return (data & (1 << i)) >> i;
	}
}

public class Test {

	static int findMissing(ArrayList<BitInteger> array) {
		int size = array.size();
		int t = 1;
		while (t < size)
			t = t << 1;
		if ((size & (size - 1)) > 0) {
			array = (ArrayList<BitInteger>) array.clone();
			int n = (int) Math.pow(2, t) - 1;
			while (array.size() < n) {
				array.add(new BitInteger(array.size() + 1));
			}
		}
		return findMissing(array, t - 1);
	}

	static int findMissing(ArrayList<BitInteger> input, int column) {
		if (column < 0) { // Base case and error condition
			return 0;
		}
		ArrayList<BitInteger> oddIndices = new ArrayList<BitInteger>();
		ArrayList<BitInteger> evenIndices = new ArrayList<BitInteger>();
		for (BitInteger t : input) {
			if (t.fetch(column) == 0) {
				evenIndices.add(t);
			} else {
				oddIndices.add(t);
			}
		}
		// System.out.println(oddIndices.size() + " " + evenIndices.size());
		if (oddIndices.size() >= evenIndices.size()) {
			return (findMissing(evenIndices, column - 1)) | 0 << column;
		} else {
			return (findMissing(oddIndices, column - 1)) | 1 << column;
		}
	}

	static int findMissingTwo(ArrayList<BitInteger> array) {
		int t = 0;
		for (int i = 1; i <= array.size(); i++) {
			t ^= i;
		}
		int r = 0;
		for (int j = BitInteger.INTEGER_SIZE; j >= 0; j--) {
			int s = 0;
			for (int i = 0; i < array.size(); i++) {
				s ^= array.get(i).fetch(j);
			}
			r = (r << 1) + s;
		}
		return t ^ r;
	}

	public static void main(String[] argv) {
		ArrayList<BitInteger> list = new ArrayList<BitInteger>();
		for (int i = 0; i < 13; i++) {
			if (i != 3)
				list.add(new BitInteger(i));
		}
		System.out.println(findMissing(list));
		System.out.println(findMissingTwo(list));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值