剑指offer_1 cpp
来源:互联网 发布:华中科技大学网络大专 编辑:程序博客网 时间:2024/06/07 05:27
1.题目描述
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
//第一种方式
#include<map>class Solution {public: bool Find(int target, vector<vector<int> > array) { map<int,bool>mp;//生成一个map,将vector数组中的数值通过遍历赋值给map的键值 for(int i=0;i<array.size();i++) { for(int j=0;j<array[i].size();j++) { mp[array[i][j]]=true; } } return mp.find(target)!=mp.end();//mp.find(target)是产生一个指向target值的迭代器,当没有相应的target时,指向mp.end(); }};
//第二种方式
关于map的操作可以参考:http://blog.csdn.net/allovexuwenqiang/article/details/5686583class
Solution {
public
:
bool
Find(vector<vector<
int
> > array,
int
target) {
int
m,n,x,y;
m = array.size();
//行数
n = array[0].size();
//列数
x=m-1;y=0;
//坐标定在左下角
while
(x>=0 && y<=n-1){
if
(target<array[x][y]){
x--;
//遇小上移
}
else
if
(target>array[x][y]){
y++;
//遇大右移
}
else
{
return
true
;
}
}
return
false
;
}
//左下角开始,遇大右移,遇小上移,直到超过边界都没找到,得false。否则得true。};
0 0
- 剑指offer_1 cpp
- Offer_1
- 剑指offer_1 二维数组中的查找
- cpp
- CPP
- CPP
- cpp
- cpp
- CPP
- CPP
- ClientSockThread.cpp&&ClientUrlThread.cpp
- cpp调用其他cpp
- ex34a.cpp&&ex34aDoc.cpp&&ex34aView.cpp
- MainFrm.cpp&&ServerThread.cpp&&SheetConfig.cpp
- CPP thread
- cpp->html
- CFaderWnd.cpp
- PasswordReminder.cpp
- 二维数组中查找指定数据
- 重写equals()方法同时还需要重写hashCode()方法
- QuickHit
- IIS7 CGI 的设置 -- IIS 下运行 Delphi 编译的 CGI 程序
- 使用apidoc生成接口文档
- 剑指offer_1 cpp
- Linux下java调取命令通过ip获取mac地址
- Shell入门(三)echo.printf.test命令
- Springmvc下一次选多个文件上传(二)Ajax异步
- springMVC下无法加载静态资源文件(js、css、image)
- csu1337 搞笑版费马大定理
- Java并发性和多线程介绍
- 蓝桥杯 地宫取宝 (DP)
- @property 装饰