当前位置:网站首页>第一篇试水--*offer
第一篇试水--*offer
2022-07-23 05:45:00 【wzf6667】
知识输出
试水
题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
解法
最笨的方法 循环
public class Solution {
public boolean Find(int target, int [][] array) {
for(int i=0;i<array.length;i++){
for(int j=0;j<array[0].length;j++){
if(target==array[i][j]){
return true;
}
}
}
return false;
}
要注意array没有.size(),只有常量.length 。
既然说了横纵递增肯定有复杂度更小的算法
//从第一行最后一个元素开始比较,如果比target大则向左移动一个,比target小向下移动一个,然后从二维数组新获取到的这个值再和target比较。直到要找的这个值从数组范围内移动出去就停止,返回false
public class Solution {
public boolean Find(int target, int [][] array) {
int col = array[0].length;
int row = array.length;
int x = col - 1;
int y = 0;
while(x>=0 && y<=row-1){
int value = array[y][x];
if(value>target){
x--;
}
else if(value<target){
y++;
}
else{
return true;
}
}
return false;
}
}`
边栏推荐
- Object based - two classic classes
- [AUTOSAR DEM iv.event memory]
- Interpretation of the paper: "deep-4mcw2v: sequence based predictor for identifying N4 methylcytosine (4mc) sites in E. coli"
- 堆的实现与堆排序实现
- Find the saddle point of the matrix and its corresponding subscript.
- 钢结构基本原理全面详细总结
- Blog building five: drawing bed selection
- [introduction to AUTOSAR com 4.com service layer module]
- Axure实现增删改查
- 【AUTOSAR COM 3.信号的收发流程TX/RX】
猜你喜欢

Blog building five: drawing bed selection

【学习总结】
![[talent column] can't you use Apache dolphin scheduler? It takes a month to write the most comprehensive introductory teaching [2]](/img/34/abc6ef91d0b735f713f76da5a4b537.png)
[talent column] can't you use Apache dolphin scheduler? It takes a month to write the most comprehensive introductory teaching [2]

二叉树的实现-c

Prometheus Operator使用指南笔记

【Autosar 存储Stack NVM】

博客搭建五:图床选择

输入三角形边长,求面积

C语言数据库:基于tcp多进程的在线词典,有详细的步骤已经图解,欢迎大家来观看

钢结构基本原理复习
随机推荐
VS属性配置相关知识
嵌入式从入门到精通(入土)——超详细知识点分享3
Common sorting method - Select Sorting
hot 100深度优先
博客搭建六:绑定自己域名的方法
刷题笔记:二叉树的中序遍历(三种解法-递归,迭代,Morris)
高等代数知识结构
5.4 Pyinstaller库安装与使用
Switch implements expression evaluation
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 2
[AUTOSAR com 1. introduction to communication protocol stack]
匿名上位机v7波形显示
obs插件基础
【AUTOSAR CanTP 1.学习UDS诊断的网络层协议】
Object based - two classic classes
C语言:基于顺序表的学生管理系统,超级详细,全部都有注释,看完不懂来扇我。
常见的排序方法—选择排序
Blog building five: drawing bed selection
LeetCode——136. 只出现一次的数字
Interpretation of the paper: recognition of enhancer promoter interactions with neural networks based on pre trained DNA vectors and attention mechanisms