Cracking The Coding Interview 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?
//
//
#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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- 单细胞数据初步处理 | drop-seq | QC | 质控 | 正则化 normalization
比对 The raw Drop-seq data was processed with the standard pipeline (Drop-seq tools version 1.12 from ...
- 【WPF】 Behavior
Hello,Behavior 引言 在看PDC-09大会的视频时,其中一篇讲利用Blend来扩展Silverlight元素的行 为,当时感觉很酷:在Blend中,将MouseDra ...
- test example
#coding=utf-8 import os import caffe import numpy as np root='/home/xxx/caffe/' #根目录 deploy=root + ' ...
- 性能测试工具 Web Service 性能测试工具比较
[转自]https://testerhome.com/topics/3003 背景 希望选择一款Web Service性能测试工具,能真实模拟大量用户访问网站时的请求,从而获取服务器当前的请求处理能力 ...
- json -- dump load dumps loads 简单对比
json.dumps是将一个Python数据类型列表进行json格式的编码解析, 示例如下: >>> import json #导入python 中的json模块>>&g ...
- dp 最大正方形
洛谷P1387 最大正方形 分析:画图模拟,发现以坐标(i,j)为右下顶点的正方形可以根据它左上方的三个点得到,就很轻松写出状态转移方程了. 代码: #include<bits/stdc++.h ...
- CO15批次确定,标准的太蛋疼了
1.批次确定的配置,之前有转过,自己也动手配过,可以是可以,但是蛋疼,用户不愿意去弹出的界面选批次...2.因为这边的批次全部是按年月日+流水生成的,所以在批次确定这里就需要按照批次的号来排序选择了 ...
- [转]2017年最具价值的十大开源项目!GitHub 年度报告~
<GitHub 2017 年度报告>GitHub 每年都会在年度盛会中推出数据报告,其中列出了一些年度的数据,包括其网站中最受欢迎的编程语言.开源项目等.那么今年哪些开源项目最具价值呢?我 ...
- HTML5 <li> <ol> <ul> 用法
定义和用法 <li> 标签定义列表项目. <li> 标签可用在有序列表 (<ol>) 和无序列表 (<ul>) 中. HTML 与 XHTML 之间的差 ...
- 用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...