《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 ...
随机推荐
- Js 数据类型 Number()转型函数
alert(Number(true)); //转换为1,如果为false为0 alert(Number()); //25,数值型直接返回 alert(Number(null)); //0,空对象返回0 ...
- nodejs封装的webget webpost方法
在我之前的项目中,经常用到Nodejs通过post\get方法访问其它网站.webapi.下面是我封装的 Get.Post方法,很适合在一些web字符串收发场景使用(暂不支持文件.二进制流等传输). ...
- php session小节
1.为什么要用session? 在人们访问网站的时候,有很多个网页,由于http自身的特点,用户每执行一个脚本都需要和web服务器重新建立连接.由于他们之间是无状态的,这次的连接无法得到上次连接的状态 ...
- json 序列化和反序列化的3个方法
https://www.cnblogs.com/caofangsheng/p/5687994.html
- UTF8与ANSI互转
在取回的结果中,如果有Unicode字符,用printf来打印的话,则会出现乱码.通过这个方法,可以判断是否为unicode字符,是的话,通过wprintf来打印.1.判断字符串是否为Unicode的 ...
- java、javac -version不一致(java编译及运行环境不一致)的环境变量设置问题解决
问题描述: 电脑上同时安装了JDK1.6与1.7 设置了环境变量JAVA_HOME为jdk1.6.0_21的安装目录,并且在PATH变量中加入了%JAVA_HOME%\bin,但在Windows命令 ...
- Error:linker command failed with exit code 1 (use -v to see invocation) - iOS
今天在操作 CoreData 时,创建完 Create NSManagedObject Subclass... 后,工程中会自动生成四个文件,如下图所示: 此时此刻便以工程,激动人心的时刻来临了 ...
- POJ1286 Necklace of Beads(Polya定理)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9359 Accepted: 3862 Description Beads ...
- Spring Cloud 入门 Consul-Client服务提供
前面介绍了 Rureka Client服务提供, 只需要改pom.xml部分内容 1.pom.xml <?xml version="1.0" encoding="U ...
- JS - 给数组的原型添加去掉重复元素的distinct方法
/* 调用完该方法,原数组只留下非重复的数据 返回一个数组,里面是依次出现的重复元素 */Array.prototype.distinct = function () { var removeA ...