//原文:
//
// 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. phpstudy2018配置站点后500错误问题

    phpstudy2016没发现这个问题,这是因为新项目要使用laravel,php版本要求7.1,所有换成了2108,但是laravel的项目没问题,原来tp框架的都不能正常访问,最好查看nginx错 ...

  2. GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程

    GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程 https://www.sdbeta.com/mf/2018/1002/226048.html 软件下载: ...

  3. 如何下载一个物种的全部EST序列 | NCBI | 表达序列标签

    EST:表达序列标签,expressed sequence tags . 顾名思义,很好理解,就是表达出来的序列,即从基因组DNA上表达出来的RNA,但是我们没法测序RNA,所以我们最终测的是表达序列 ...

  4. IDEA分享项目到GitHub出现Could not read from remote repository

    如果VCS->Import into Version Control->Share Project on GitHub出现如下错误:: 重点在最后一行Could not read from ...

  5. z-index注意事项

    1. z-index只对定位元素有效(如position:absolute!) 2. 被覆盖的元素将无法触发其鼠标相关事件.(个人经验,可能有例外.) 3. 无法通过z-index使父级覆盖子级,如果 ...

  6. vivado第一天从建立文件运行小程序开始

    今天,是第一天什么也处于懵懂的时候,首要的任务就是建立一个文件 首先打开vivado运行软件, 如图所示,选择第一个create new project 来新建文件 选择存储路径,一路向下 当选择芯片 ...

  7. CF-503div2-A/B/C

    A. New Building for SIS time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. python中sorted和.sorted 、reversed和reverse的注意点

    L=[1,2,3,4]l1=[123,123,23]if l1.sort() == L.reverse():   #这个判断式是恒等的,因为两个函数的返回值都是None(其实是无返回值)    pri ...

  9. mybatis的update使用选择

    更新后台设置时,会分多个页面更新同一个表中的数据,愿想是尽量减少sql请求数据量并且减少重复代码. 比如博客园的: 假如只有一个用户信息表,这样的话每个页面只更新部分字段. 这种情况下的更新推荐在xm ...

  10. 【LeetCode】Anagram

    Anagram 指由颠倒字母顺序而构成的单词. e.g. 给出 ["eat", "tea", "tan", "ate", ...