当前位置:网站首页>Given a two-dimensional list of m*n, find out whether a number exists
Given a two-dimensional list of m*n, find out whether a number exists
2022-06-21 08:19:00 【liuliang514218119】
<?php
# Given a m*n 2-D list of , Find out if a number exists , The list has the following features :
# The list of each row is sorted from left to right
# The first number of each line is larger than the last number of the previous line
# [
# [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); // That's ok
if ($height < 1) {
return false;
}
$width = count($matrix[0]); // Column
if ($width < 1) {
return false;
}
$left = 0;
$right = $width * $height - 1;
while ($left <= $right) {
$mid = intval(($left + $right) / 2);
$row = intval($mid / $width); // Divide by the width Key in the first few lines
$col = $mid % $width; // Width remainder In which column are the keys
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";
边栏推荐
- Scientific research information | national natural conclusion regulations: more than 50% of the fund balance or it will not be concluded
- Leedcode 1 - sum of two numbers
- Kotlin---- detailed explanation of data types
- PS prompts "script error -50 general Photoshop error, how to solve it?
- Kotlin middle tail recursive function
- Global and Chinese market of horizontal drilling rigs 2022-2028: Research Report on technology, participants, trends, market size and share
- Introduction to testing - Software Test Model
- Operator priority and combining order
- 运算符优先级与结合顺序
- 安装MySQL出现白页面怎么办
猜你喜欢

Listing of flaunting shares on Shenzhen Stock Exchange: market value of 4.2 billion, 9-month revenue decreased by 21% year-on-year

Wechat official account docking: push article information to official account with one click

Cobaltstrike office macro virus utilization

2022-2028 global postoperative pressure suit industry research and trend analysis report

Using elastic stack to analyze Olympic data (II)

CTF show WEB10

Figure neural network and cognitive reasoning - Tang Jie - Tsinghua University

Three methods of bean instantiation

为什呢代码没报错但是数据库里边的数据显示不出来

安装MySQL出现白页面怎么办
随机推荐
2022-2028 global cooling on-off valve industry research and trend analysis report
移动应用开发总结
Visual studio code annotation plug-in: korofileheader
CTF中命令执行知识点总结
php exec、 system 、shell_ Exec cannot be executed. There is no result. The result is nulld. Solution
General business plan
【元宇宙3d大赛】
Scientific research information | national natural conclusion regulations: more than 50% of the fund balance or it will not be concluded
运算符优先级与结合顺序
Figure neural network and cognitive reasoning - Tang Jie - Tsinghua University
怎么搭建深度学习框架?
Global and Chinese market of filter microplate reader 2022-2028: Research Report on technology, participants, trends, market size and share
Using the method of combining shapes in illustrator
/home/ljx/miniconda3/compiler_ compat/ld: cannot find crtbeginS. o: There is no such file or directory
Redis master-slave vulnerability and remote connection vulnerability
Cluster hui dsm7 add suite source
Operator priority and combining order
Three ways to solve cross domain problems
1004 counting leaves (30 points)
[visualization - source code reading] antvis / g-base interpretation - 1