《Cracking the Coding Interview》——第11章:排序和搜索——题目6
2014-03-21 21:50
题目:给定一个MxN的二位数组,如果每一行每一列都是升序排列(不代表全展开成一个一维数组仍是升序排列的)。请设计一个算法在其中查找元素。
解法:对于这么一个数组,有两点是确定的:1. 左上最小,右下最大;2. 左边不大于右边,上边不大于下边。根据这么个思路,你可以从左下或者右上开始查找,应该向左走向右走,还是向上走向下走,你懂的。用这种方法,可以在线性的时间内找出一个元素。
代码:
// 11.6 Given an MxN matrix, each row and each column is sorted in ascending order.
// For emxample,
// [10 19]
// [15 21]
// It doesn't mean you can expand it to a one dimensional array and still sorted.
#include <cstdio>
#include <vector>
using namespace std; void searchMatrix(vector<vector<int> > &v, int target, int &rx, int &ry)
{
int n, m; rx = ry = -;
n = (int)v.size();
if (n == ) {
return;
}
m = (int)v[].size(); int i, j; i = ;
j = m - ;
while (i <= n - && j >= ) {
if (target > v[i][j]) {
++i;
} else if (target < v[i][j]) {
--j;
} else {
rx = i;
ry = j;
return;
}
}
return;
} int main()
{
vector<vector<int> > v;
int n, m;
int i, j;
int target; while (scanf("%d%d", &n, &m) == && n > && m > ) {
v.resize(n);
for (i = ; i < n; ++i) {
v[i].resize(m);
}
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
scanf("%d", &v[i][j]);
}
}
while (scanf("%d", &target) == ) {
searchMatrix(v, target, i, j);
printf("(%d, %d)\n", i, j);
}
for (i = ; i < n; ++i) {
v[i].clear();
}
v.clear();
} return ;
}
《Cracking the Coding Interview》——第11章:排序和搜索——题目6的更多相关文章
- 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 ...
随机推荐
- QT5中两个窗体之间传递信息(值)
一个窗体A调用另一个窗体B: 1)包含窗体B的头文件#include"B.h" 2)在窗体A中增加slots函数: public slots: void infoRecv(QStr ...
- IOS 纯代码添加 Button Image Label 添加到自定义View中
@interface ViewController () /**获取.plist数据*/ @property (nonatomic,strong) NSArray *apps; @end @imple ...
- 0x40二分法
二分模板一共有两个,分别适用于不同情况.算法思路:假设目标值在闭区间[l, r]中, 每次将区间长度缩小一半,当l = r时,我们就找到了目标值. 版本1 在单调递增序列a中查找>=x的数中最小 ...
- 20145238-荆玉茗 《Java程序设计》第9周学习总结
20145238第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 ...
- a=a+(a++);b=b+(++b);计算顺序,反汇编
a=a+(a++); 013913BC mov eax,dword ptr [a] 013913BF add eax,dword ptr [a] 013913C2 mov dword ptr [a], ...
- 谷歌浏览器兼容IE插件
谷歌浏览器兼容IE插件 http://pan.baidu.com/s/1i31hspf
- Swiper插件
中文官网:Swiper中文网 英文:英文网 此插件功能比较强大,网页端.手机端都可以使用的插件.这里记录一下在微信h5页面里面滑动获取数据. 先下载css和js,引用到项目中 这里有6个节点,没划一个 ...
- 执行pip命令时遇到 Fatal error in launcher: Unable to create process using '"'
电脑同时安装了python-2.7.13跟python-3.6.1,安装时勾选了pip,环境变量也已经配置好. 为了方便运行,同时修改了可执行文件为 python2和python3.此时在cmd命令行 ...
- JAVA / MySql 编程——第七章 JDBC
1.JDBC:JDBA是Java数据库连接(Java DataBase Connectivity)技术的简称,提供连接各种常用数据库的能力: ●Java是通过JDBC技术实现对各种数据 ...
- 【LeetCode #179】Largest Number 解题报告
原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...