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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第11章:排序和搜索——题目4

    2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...

  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目3

    2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...

  10. 《Cracking the Coding Interview》——第11章:排序和搜索——题目2

    2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...

随机推荐

  1. 洛谷 P1509 找啊找啊找GF

    题目背景 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见." "诶,别再见啊..." 七夕...七夕...七夕这个日子,对于sqybi这 ...

  2. 2018.8.4session的removeAttribute()和invalidate()的区别

    session的removeAttribute()和invalidate()的区别 session.invalidate()是销毁跟用户关联session,例如有的用户强制关闭浏览器,而跟踪用户的信息 ...

  3. Spring boot 集成三种拦截方式

    三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...

  4. spring boot1.5.6 测试类

    package com.qutaoyao.demo.web; import com.qutaoyao.demo.web.controller.HelloController;import org.ju ...

  5. IBM区块链总经理谈区块链

    IBM区块链总经理谈区块链:3.4年前IBM的区块链人员就达到了1500人  Captain Hiro 2018-03-20 16:22 发布在 区块链 3 18349 CCN的记者Eric Eiss ...

  6. visual attention

    The visual attention mechanism may have at least the following basic components [Tsotsos, et. al. 19 ...

  7. RHEL7.X安装12.2RAC时root.sh错误CLSRSC-400的解决方案

    问题现象: [root@ora12c ghome]# /opt/oracle/ghome/root.sh Performing root user operation. The following e ...

  8. HDU 1084 What Is Your Grade?(排序)

    题目在这里:1084 题目描述: “Point, point, life of student!” This is a ballad(歌谣)well known in colleges, and yo ...

  9. Logger日志配置级别说明及设置方法、说明

    日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4j ...

  10. http2.2配置

    http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...