当前位置:网站首页>给定一个m*n的二维列表,查找一个数是否存在
给定一个m*n的二维列表,查找一个数是否存在
2022-06-21 08:12:00 【liuliang514218119】
<?php
# 给定一个m*n的二维列表,查找一个数是否存在,列表有下列特性:
# 每一行的列表从左到右已经排序好
# 每一行第一个数比上一行最后一个数大
# [
# [1,3,5,7],
# [10,11,16,20],
# [23,30,34,50],
# ]
# [
# [0,1,2,3],
# [4,5,6,7],
# [8,9,10,11],
# ...
# ]
function matrix_search($matrix, $target)
{
$height = count($matrix); // 行
if ($height < 1) {
return false;
}
$width = count($matrix[0]); // 列
if ($width < 1) {
return false;
}
$left = 0;
$right = $width * $height - 1;
while ($left <= $right) {
$mid = intval(($left + $right) / 2);
$row = intval($mid / $width); // 除以宽度 键在第几行
$col = $mid % $width; // 宽度取余 键在第几列
if ($matrix[$row][$col] == $target) {
return true;
} else if ($matrix[$row][$col] > $target) {
$right = $mid - 1;
} else {
$left = $mid + 1;
}
}
return false;
}
$matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50],
];
$target = 2;
var_dump(matrix_search($matrix, $target));
$target = 16;
var_dump(matrix_search($matrix, $target));
echo "\n";
边栏推荐
- [Redis]-[Redis底层数据结构]-字典
- 图解 Google V8 # 14:字节码(二):解释器是如何解释执行字节码的?
- 怎么搭建深度学习框架?
- Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
- Difference between function declaration and function expression
- How can we make millions a year now?
- Decrypt FTP
- Blank screen of virtual machine browser
- 测试入门——软件测试模型
- 1006 Sign In and Sign Out (25 分)
猜你喜欢
![[actual combat] ACM players illustrate leetcode using stack to realize queue](/img/f7/0a21f2fdc7c18f352c1b134d27c21c.jpg)
[actual combat] ACM players illustrate leetcode using stack to realize queue

Bean实例化的三种方法
![[kotlin] premier jour](/img/51/18b394a6bf0ab74b71e5c59ad3341c.png)
[kotlin] premier jour

Yyds dry goods inventory rapid establishment of CEPH cluster

Three methods of bean instantiation

For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!

How to write attractive titles for short videos? Learn these tips to make your title more convincing

移动应用开发总结

Illustration of Google V8 16: how does V8 improve function execution efficiency through inline caching?

2022-2028 global internal gear motor industry research and trend analysis report
随机推荐
群晖DSM7添加套件源
Kotlin---- detailed explanation of data types
【kotlin】第一天
Images, graphics and Applications (II)
2021-06-16 STM32F103 EXTI 中斷識別 使用固件庫
showCTF Web入门题系列
Showctf web primer series
2022-2028 global after sales spark plug industry research and trend analysis report
Difference between function declaration and function expression
Global and Chinese market for crankshaft position sensors 2022-2028: Research Report on technology, participants, trends, market size and share
[DB written interview 225] in Oracle, if the online redo log file is damaged, how to recover it?
2022-2028 global internal gear motor industry research and trend analysis report
Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
图解 Google V8 # 14:字节码(二):解释器是如何解释执行字节码的?
Using elastic stack to analyze Olympic data (II)
Multiplication and addition of univariate polynomial (20 points)
Construct URL and Base64 download in the form of binary stream for file download
(thinking) C. differential sorting
MySql 过滤查询(以字母开头,以数字开头,非数字开头,非字母开头)
文件下载 二进制流的形式构造url和base64下载