//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?
//
//
#include <iostream>
#include <vector>
using namespace std; void print(int p)
{
vector<int> v;
for (int k = 0;k<32; k++)
{
int t = p&1;
v.push_back(t);
p=p>>1;
}
for (int i = v.size()-1;i>-1; i--)
{
cout<<v[i]<<" ";
}
cout<<endl;
} /***取得a[i]的j位***/
int fetch(int *a, int i,int j)
{
int t = a[i];
t = t>>j;
return (t&1);
} /***左移,相或,得到a[i]***/
int getai(int *a, int i)
{
int t = 0;
for (int s=31; s>-1; s--)
{
t = t<<1;
t = t|fetch(a, i, s);
} return t;
} /***缺少的数为:(0+1+2+...n) - (a[0]+...a[n-1]) ***/
int getN(int *a,int n)
{
int sumn = 0;
int suma = 0;
for (int i = 0;i <n; i++)
{
sumn += i;
suma += a[i];
}
return sumn + n - suma;
} int main()
{
int a[] = {
0, 1, 2, 3, 4, 5, 7, 8, 9, 10
};
cout<<getN(a,10);
return 0;
}

Cracking The Coding Interview 5.7的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. Eclipse安装lombok

    下载lombok 下载地址:https://projectlombok.org/downloads/lombok.jar 或者访问官网下载  https://projectlombok.org/ 安装 ...

  2. java ----> 手动编译java项目

    环境: jdk1.8,cmd,notepad++ 创建java工程test,创建文件夹: src classes lib 说明: src 放置.java文件 classes 放置.class文件 li ...

  3. Robot Movement(机器人移动)

    中文标题[机器人移动] 这个题目是 Kayak 发布的代码挑战题目. 我认为题目本身描述的不是十分清楚,方法需要返回结果,但是结果没有说明是机器人最后的坐标位置,还是最后的坐标位置距离原点的距离.同时 ...

  4. TP5中即点即改,json分页,单删

    HTML页面: <!doctype html><html lang="en"><head> <meta charset="UTF ...

  5. 【洛谷p5015】标题统计

    (写上瘾了再来一篇吧) 标题统计[传送门] 洛谷算法标签 字符串这种东西,我看到是崩溃的.因为我们只学到了二维数组[这个梗自行get],总之我们当时还没有学.然后显然就是各种翻书,各种百度.大致了解了 ...

  6. python3 mail

    # !usr/bin/python3# -*-coding=UTF-8-*-import smtplib # python 对SMTP的支持,smtplib这个库负责发送邮件from email.mi ...

  7. Plus One leetcode java

    问题描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...

  8. HashTable Queue Stack SortedList BitArray

    HashTable 由于是非泛型集合,因此存储进去的都是object类型,不管是键还是值. Hashtable不允许排序 key不允许重复 键不允许为null Queue和Queue<T> ...

  9. Oracle表空间状态

    1.表空间只读 查看当前表空间状态 SYS@userdata>column file_name format a60 SYS@userdata>column tablespace_name ...

  10. windows工具打开命令

    程序 命令 位置 记事本 notepad C:\Windows\system32 ping ping C:\Windows\System32 服务管理器 services.msc C:\Windows ...