《Cracking the Coding Interview》——第11章:排序和搜索——题目8
2014-03-21 22:23
题目:假设你一开始有一个空数组,你在读入一些整数并将其插入到数组中,保证插入之后数组一直按升序排列。在读入的过程中,你还可以进行一种操作:查询某个值val是否存在于数组中,并给出这个元素在数组中的位置(如果有多个的重复元素话,给出最小的下标)。
解法:书上的原题不是这么描述的,但我觉得用这种插入排序的说法更好理解。虽然说是数组,但实际上既可以用真的数组来模拟这一过程,也可以用一棵二叉搜索树来做。我的实现是用二叉搜索树,每个节点里除了记录元素的值之外,还记录它的左子树有多少个点。这样在树里面也能进行对数级的查找。其实,用数组直接模拟的话,代码应该更好写的。
代码:
// 11.8 Given an array of integers, find out for a given value, how many integers are there in the array, that are no greater than the value.
// If the value is not in the array, return -1 instead.
#include <algorithm>
#include <iostream>
#include <string>
using namespace std; struct TreeNode {
int val;
int count;
int count_left;
TreeNode *left;
TreeNode *right;
TreeNode(int _val = ): val(_val), count(), count_left(), left(nullptr), right(nullptr) {};
}; void insertNode(TreeNode *&root, int val)
{
if (root == nullptr) {
root = new TreeNode(val);
} else if (val == root->val) {
++(root->count);
} else if (val < root->val) {
++(root->count_left);
insertNode(root->left, val);
} else {
insertNode(root->right, val);
}
} int getRank(TreeNode *root, int val)
{
int result;
TreeNode *ptr; result = ;
ptr = root;
while (ptr != nullptr) {
if (ptr->val > val) {
ptr = ptr->left;
} else if (ptr->val < val) {
result += ptr->count_left + ;
ptr = ptr->right;
} else {
break;
}
}
if (ptr != nullptr) {
result += ptr->count_left;
return result;
} else {
return -;
}
} void clearTree(TreeNode *&root)
{
if (root != nullptr) {
clearTree(root->left);
clearTree(root->right);
delete root;
root = nullptr;
}
} int main()
{
int val;
TreeNode *root;
string s; root = nullptr;
while (cin >> s) {
if (s == "i") {
cin >> val;
insertNode(root, val);
} else if (s == "r") {
cin >> val;
printf("rank = %d\n", getRank(root, val));
} else if (s == "e") {
break;
}
}
clearTree(root); return ;
}
《Cracking the Coding Interview》——第11章:排序和搜索——题目8的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目3
2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目2
2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...
随机推荐
- Bonita portal 源码编译(未完成)
首先下载源代码 https://github.com/bonitasoft/bonita-portal-js 以下内容为Github 的安装教程包含我安装过程中遇到的问题.并加以修正 Bonita p ...
- Last_IO_Errno: 1062
主键冲突的错误 1062 模拟错误: 在主库上操作: create table test100(id int not null,name varchar(20),primary key(id) ...
- 【转】android gravity属性 和 weight属性
有点忘记这两个属性了,复习一下. 来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <Linea ...
- C++STL之map映照容器
map映照容器 map映照容器的元素数据是由一个键值和一个映照数据组成的, 键值与映照数据之间具有一一映照关系. map映照容器的数据结构也是采用红黑树来实现的, 插入元素的键值不允许重复, 比较函数 ...
- python显示灰度图
import matplotlib import matplotlib.pyplot as plt %matplotlib inline im=plt.imread('../lena.jpg', py ...
- python中的反斜杠问题
python本身使用 \ 来转义一些特殊字符,比如在字符串中加入引号的时候 s = 'i\'m superman' print(s) # i'm superman 为了防止和字符串本身的引号冲突,使用 ...
- django ORM单表操作
1.ORM介绍 ORM是“对象-关系-映射”的简称 映射关系: mysql---------Python 表名----------类名 字段----------属性 表记录--------实例化对象 ...
- 3.初识Cron表达式
Cron: 计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思.在Linux中,我们经常用到 cron 服务器来完成这项工作.cron服务器可以根据配置文件约定的时间来执行特定的作务. ...
- 友盟分享小结 - iOS
因之前都写在了 AppDelegate 类中,看起来过于臃肿,此次基于友盟分享重新进行了一次优化,此次分享内容基于已经成功集成 SDK 后 code 层级部分.注:此次分享基于 SDK 6.9.3,若 ...
- [优化]Steamroller-freecodecamp算法题目
晚上在medium看到一篇关于找工作的文章,里面提到一个面试题目--flattening an array(扁平化数组).这我好像在哪看过!应该是freecodecamp里的算法某一题.翻了下博客记录 ...