//原文:
//
// Given a matrix in which each row and each column is sorted, write a method to find an element in it.
// 从最右一排,从上到下,小于右上角的元素则下降,大于右上角的元素则左移找;第一个相等的元素。
#include <iostream>
using namespace std;
int search(int **p, int sizex, int sizey, int key)
{
int x = sizex;
int y = 0;
while(y<sizey && x>-1)
{ if (key > p[y][x])
{
y ++;
}
else if(key < p[y][x])
{
x --;
}
else
{
cout<<"y "<<y<<" x "<<x<<endl;
return p[y][x];
}
} return -1;
} int main()
{
int sizea = 5;
int sizeb = 6;
int **p = new int *[sizea];
for (int i = 0; i<sizea; i++)
{
p[i] = new int[sizeb];
}
for (int i = 0; i<sizea; i++)
{
for (int j = 0; j<sizeb; j++)
{
p[i][j] = i*2 + j*3;
}
}
cout<<endl; for (int i = 0; i<sizea; i++)
{
for (int j = 0; j<sizeb; j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
} cout<<search(p,sizea,sizeb,16);
return 0;
}

Cracking The Coding Interview 9.6的更多相关文章

  1. Cracking the coding interview

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

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

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

  3. Cracking the Coding Interview(Trees and Graphs)

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

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

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

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

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

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

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

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

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

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. nginx反向代理实例

    通过一个例子演示一下nginx是如何代理服务的,并且讲一下nginx.conf的关于server模块和location模块的配置 server模块:配置虚拟主机的相关参数,一个http中可以有多个se ...

  2. 放弃 Tightvnc, 选择 Tigervnc

    构建headless vnc server ,我终于放弃了Tightvnc 基于以下原因: 1) 已知的Qt5的键盘映射问题,导致virtualbox 的使用出现困难 https://unix.sta ...

  3. Python 编程快速上手 第五章总结

    第五章 字典和结构化数据 创建数组 格式:myCat = {'size':'fat','color':'gray',disposition':'loud'} 对字典的操作 通过[ ] 访问字典的值 [ ...

  4. learn python the hard way 习题6~10总结

    习题6总结 定义字符串: 名字 = 值 其他 你也可以用 {types_of_people}的方式把它放在任何字符串中. 也就是说你可以在其他字符串中添加{},然后前面加一个 f,可用print()进 ...

  5. LeetCode--002--两数相加(java版)

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  6. POJ-3693/HDU-2459 Maximum repetition substring 最多重复次数的子串(需要输出具体子串,按字典序)

    http://acm.hdu.edu.cn/showproblem.php?pid=2459 之前hihocoder那题可以算出最多重复次数,但是没有输出子串.一开始以为只要基于那个,每次更新答案的时 ...

  7. server2008远程开端口的方法

    今天在通过本地链接远程oracle数据库的时候发现了个问题,建立好连接了,可是一直没连上,后面发现是防火墙的1521的oracle端口没开启.开启的方法可以采用如下方法: 操作:开始→控制面板→Win ...

  8. PHP的几种输出方式

    请写出echo.print_r.print.var_dump .die之间的区别 echo  只能输出字符串等单一数据 不能输出数据类型 不能输出数组等多种数据 print()  只能输出字符串等单一 ...

  9. HDU-2767-tarjan/Kosaraju求scc

    http://acm.hdu.edu.cn/showproblem.php?pid=2767 问最少添加几条边使得图为强连通. tarjan跑一下,然后对强连通分量缩点,找下此时出度为零和入度为零的点 ...

  10. hdu1569-方格取数-二分图网络流

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...