//原文:
//
// 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. MATLAB矩阵操作

  2. Getting Started with Processing 第五章的总结

    Getting Started with Processing 第五章:响应 一次与永久 setup()函数 Processing 中,setup()函数只运行一次,用于设置一些初始的值,比如画布的大 ...

  3. HTML第三章总结

    在这一章节中,主要讲了 HTML 中众多的 element,element 就像在建筑房屋时候的材料,它可以分为两种: Block Element Inline ElementBlock Elemen ...

  4. 协方差分析 | ANCOVA (Analysis of Covariance)

    If you are worried about leaving out covariates you could regress out them first and analyse the res ...

  5. hdu-4507 吉哥系列故事——恨7不成妻 数位DP 状态转移分析/极限取模

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 求[L,R]中不满足任意条件的数的平方和mod 1e9+7. 条件: 1.整数中某一位是7:2.整数的每一 ...

  6. PHP如何定义类及其成员属性与操作

    1.类的定义: 类的关键字定义使用class 1.定义一个空类 Class Person{}; 2.定义一个有成员属性和操作的类 Class Person{ //成员属性 $name     =  ' ...

  7. 『TensorFlow』第十一弹_队列&多线程&TFRecod文件_我辈当高歌

    TF数据读取队列机制详解 一.TFR文件多线程队列读写操作 TFRecod文件写入操作 import tensorflow as tf def _int64_feature(value): # val ...

  8. XXE漏洞

    原理:XML外部实体注入,简称XXE漏洞,XML数据在传输中数据被修改,服务器执行被恶意插入的代码.当允许引用外部实体时,通过构造恶意内容,就可能导致任意文件读取.系统命令执行.内网端口探测.攻击内网 ...

  9. bzoj2431

    题意:求有多少个逆序对为k的排列 题解:\(dp[i][j]\)表示1~i的排列中有j个逆序对的方案数,转移就是把i放在1~i-1的排列中的第几位,\(dp[i][j]=\sum_{x=0}^{min ...

  10. BCS SET EMAIL

    FUNCTION zcrm_send_email. *"------------------------------------------------------------------- ...