Cracking The Coding Interview 9.6
//原文:
//
// 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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 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》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- MySQL official tutorial
1.installation 2.setup environment variables add %/MySQL Server/bin to path. then restart cmd/powers ...
- VS Code插件
VS Code下载地址: https://code.visualstudio.com/ 1.view in browser 和 Open-In-Browser 安装可在编辑器中打开html,在 ...
- pytorch backward问题
pytorch中关于backward的很有意思的一个问题 <https://blog.csdn.net/shiheyingzhe/article/details/83054238> 但是我 ...
- SQL SERVER select,update,delete使用表别名
[SELECT] select * from 表名 表别名 [UPDATE] update 表别名 set 表别名.列=值 from 表名 表别名 where 条件 [DELETE] delete 表 ...
- python记录_day05 字典
字典 字典由花括号表示{ },元素是key:value的键值对,元素之间用逗号隔开 特点:1.字典中key是不能重复的 且是不可变的数据类型,因为字典是使用hash算法来计算key的哈希值,然后用哈希 ...
- constant函数
返回一个常量的值 constant('constname');
- 解决Requests中文乱码【有用】,读取htm文件 读取txt文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0
打开这个网址https://blog.csdn.net/chaowanghn/article/details/54889835 python在open读取txt文件时,出现UnicodeDecodeE ...
- hadoop集群添加新节点
0.说明 Hadoop集群已经运行正常,现在新买了一些机子,要加入到集群里面增加新的节点.以下就是增加的过程. 1.配置运行环境 安装与master和其他slave相同的java环境,jdk版本要相同 ...
- 灵活运用 SQL SERVER FOR XML PATH 转
灵活运用 SQL SERVER FOR XML PATH FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些 ...
- 函数使用三:采购过账BAPI_GOODSMVT_CREATE
一.货物移动.bapi BAPI_GOODSMVT_CREATE其中 参数 : GOODSMVT_CODE 有 GMCODE Table T158G - 01 - MB01 - Goods Rece ...