《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 ...
随机推荐
- SIEMENS Simotion 运动控制器设置Web service, HTTP, FTP访问密码
早期版本的web service, 访问密码是 用户名 : simotion 密码 : simotion 新版本,Firmware >= 4.4之后,考虑到安全性,控制器没有默认密码.设置密码方 ...
- PHP @ at 记号的作用
看PHP的代码,总有些行前边有@符号,一直不知道是什么意思. 例如dede5.7 @$ni=imagecreatetruecolor($ftoW,$ftoH); 今天用到了,就记一下吧.其实它 ...
- IOS GCD03-其他用法
#define global_queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define main_queu ...
- POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)
题目链接: https://vjudge.net/problem/POJ-2456 题目大意: 有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大 ...
- 既然红黑树那么好,为啥hashmap不直接采用红黑树,而是当大于8个的时候才转换红黑树?
因为红黑树需要进行左旋,右旋操作, 而单链表不需要,以下都是单链表与红黑树结构对比.如果元素小于8个,查询成本高,新增成本低如果元素大于8个,查询成本低,新增成本高 https://bbs.csdn. ...
- HTTP的三次握手
在讲三次握手之前,希望大家理解一个概念,什么概念呢? 就是在我们的客户端和我们的服务器之间进行http请求,发送和返回的过程当中,我们是需要去创建一个tcp connection的东西,因为http是 ...
- Spring 上下文操作工具类 ContextUtils
ContextUtils.java package com.java.config; import org.springframework.beans.BeansException; import o ...
- MAC卸载/删除 Parallels Desktop虚拟机的方法
一些MAC用户在自己的电脑上安装了虚拟机之后,想要将它卸载,但是不知道该怎么做.今天小编就为大家带来了这个问题的解决方法. 解决方案(删除/卸载虚拟机 (VM): 1.启动Parallels Desk ...
- 基于指令的移植方式的几个重要概念的理解(OpenHMPP, OpenACC)-转载
引言: 什么是基于指令的移植方式呢?首先我这里说的移植可以理解为把原先在CPU上跑的程序放到像GPU一样的协处理器上跑的这个过程.在英文里可以叫Porting.移植有两种方式:一种是使用CUDA或者O ...
- NVIDIA CUDA Library Documentation
http://developer.download.nvidia.com/compute/cuda/4_1/rel/toolkit/docs/online/index.html 英伟达CUDA库说明文 ...