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 ...
随机推荐
- SpringBoot集成ActiveMQ
前面提到了原生API访问ActiveMQ和Spring集成ActiveMQ.今天讲一下SpringBoot集成ActiveMQ.SpringBoot就是为了解决我们的Maven配置烦恼而生,因此使用S ...
- gradlew 的https代理设定
在内网编译vlc for Android 时, 总是在 [./gradlew assemble] 卡住, 在网上找到了设置代理的方法: 在gradlew 的同一目录,建立一个 gradle.prope ...
- eclipse---->error and exception 和常用配置
1.eclipse打开后报错:An internal error occurred during: "Initializing Java Tooling". java.lang.N ...
- Centos6.5 升级Openssl + Openssh
xu言: 平时很懒,都不想写blog.今天(2018.05.15)开始尝试每天写一篇吧,看我自己能坚持多久! 准备工作: 为了防止在操作过程中导致ssh远程中断,首先安装一个telnet-server ...
- Windows定时开机并开启工作软件
开启休眠功能 在搜索窗口中输入“cmd.exe”,在结果中看见了“cmd.exe”,右击选择“以管理员权限运行程序”打开“cmd.exe”命令窗口,输入命令“powercfg -h on”即可开启计算 ...
- php实现队列
<?php //双向队列的实现 class DoubleEndedQueue{ public $elements; public function __construct(){//析构函数,创建 ...
- 进程锁,队列,JoinableQueue
内容梗概: 1.进程同步(锁) 2.队列(重点) 3.生产者消费者模式 4.JoinableQueue([maxsize]) 5.信号量(了解) 6.事件 1.进程同步(锁) 并发编程让我们能更加充分 ...
- Python特点
用一种方法,最好只用一种方法来做一件事 1.面向对象(解决一个问题,先考虑由“谁”来做,怎么做是“谁”的职责) 函数.模块.数字.字符串都是对象 在Python中一切皆对象 完全支持继承.重载.多重继 ...
- PTA L2-002 链表去重
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 第一次做链表题,有时间多看看 解释 ...
- 关于移动端rem适配
var num = 1 / window.devicePixelRatio; var fontSize = document.documentElement.clientWidth / 10; doc ...