//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. Linux 各种软件的安装 - svn

    首先感谢这篇博文 https://www.cnblogs.com/mymelon/p/5483215.html 按照他的配置,svn顺利搭好. 1:yum -y install subversion ...

  2. 放弃 Tightvnc, 选择 Tigervnc

    构建headless vnc server ,我终于放弃了Tightvnc 基于以下原因: 1) 已知的Qt5的键盘映射问题,导致virtualbox 的使用出现困难 https://unix.sta ...

  3. MySQL Connector/J

    5.1 Developer Guide 1. MysQL为由Java语言编程的客户端程序提供连接:MySQL Connector/J,这是一个实现Java Database Connectivity( ...

  4. 20165303实验一 Java开发环境的熟悉

    实验一简单的java程序编译及运行,文件夹的创建 1.添加文件夹: 命令mkdir+文件夹名称 2.编译,运行Java程序 :javac 主类名.java java 主类名 3.带包(package) ...

  5. canvas绘图在手机上边缘粗糙

    辛辛苦苦用canvas绘图,做好动画后,想看看效果,结果在手机上一打开,效果是有了,但是边缘特别粗糙,这怎么可以呢,有一种说法是你用canvas绘图时候在手机retain屏幕上,他把一个像素分为两个像 ...

  6. 一个项目中既有移动端,同时也有PC端的代码,并且 他们的代码分开写的,那么如何实现在手机跳转手机页面,pc点击跳转pc页面

    将以下代码放入pc首页即可 <script type="text/javascript"> function mobile_device_detect(url) { v ...

  7. P2469 [SDOI2010]星际竞速

    在何Au的讲解下终于搞明白了这个以前的坑. 首先考虑最小路径覆盖. 这个题意又要求我们求出的最大流为n-1(这样才能保证路径为1条) 考虑高速航行模式的图怎么建,只需要保证最大流的同时费用最小即可,显 ...

  8. SWUST OJ(954)

    单链表的链接 #include <stdio.h> #include <stdlib.h> typedef struct LinkNode //单链表节点结构的定义 { cha ...

  9. 关于react16.4——错误边界

    过去,组件内的 JavaScript 错误常常会破坏 React 内部状态,并导致它在下一次渲染时产生神秘的错误.这些错误总会在应用代码中较早的错误引发的,但 React 并没有提供一种方式能够在组件 ...

  10. 移动端rem适配 flex.js

    (function() { document.addEventListener('DOMContentLoaded', function () { var html = document.docume ...