当前位置:网站首页>Leetcode (Sword finger offer) - 04. search in two-dimensional array
Leetcode (Sword finger offer) - 04. search in two-dimensional array
2022-07-25 07:38:00 【Sheep yard】
Topic link : Click to open the link
The main idea of the topic : A little
Their thinking

Related enterprises
- Bytes to beat
- Microsoft (Microsoft)
- Google (Google)
- Apple (Apple)
- Amazon (Amazon)
- Oracle (Oracle)
- tencent (Tencent)
- Hang Seng Electronics Co., Ltd
AC Code
- Java
// Solution (1)
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
int i = matrix.length - 1, j = 0;
while(i >= 0 && j < matrix[0].length)
{
if(matrix[i][j] > target) i--;
else if(matrix[i][j] < target) j++;
else return true;
}
return false;
}
}
// Solution (2)
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
int rowLen = matrix.length;
if (rowLen == 0 || matrix[0].length == 0) {
return false;
}
// Every time row Judge whether it is possible to implement , If you can perform binary search
for (int i = 0; i < matrix.length; i++) {
if (target < matrix[i][0] || target > matrix[i][matrix[i].length - 1]) {
continue;
}
int idx = find(matrix[i], target);
if (matrix[i][idx] == target) {
return true;
}
}
return false;
}
private int find(int[] arr, int target) {
int l = 0, r = arr.length - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return l - 1;
}
}- C++
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
int i = matrix.size() - 1, j = 0;
while(i >= 0 && j < matrix[0].size())
{
if(matrix[i][j] > target) i--;
else if(matrix[i][j] < target) j++;
else return true;
}
return false;
}
};边栏推荐
- [unity introduction program] basic concepts -2d rigid body 2D
- 曼哈顿距离简介
- Load capacity - sorting out the mind map that affects load capacity
- Summary of learning notes of deep learning application development (II)
- A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
- Nailing the latest version, how to clear the login phone number history data
- Is the yield of financial products high or low?
- Big guy Qiu zhaomianjing
- 【Unity入门计划】基本概念-GameObject&Components
- Before Oracle 19C migration, how important is it to do a good job of rat playback test?
猜你喜欢

【Unity入门计划】基本概念-2D碰撞体Collider 2D

Robot framework mobile terminal Automation Test ----- 01 environment installation

Completely replace the redis+ database architecture, and JD 618 is stable!

Line generation (matrix ')
![[unity entry program] basic concept trigger](/img/16/cd0f8ae579627fc095935195136729.png)
[unity entry program] basic concept trigger

【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector

Hikaricp connection pool does not operate for a period of time, and the data is automatically disconnected

【软件测试】包装简历从这几点出发、提升通过率

Gather the wisdom of developers and consolidate the foundation of the database industry

How should enterprise users choose aiops or APM?
随机推荐
JS note 17: the whole process of jest project configuration of typescript project
2-6. Automatic acquisition
曼哈顿距离简介
【Unity入门计划】基本概念-预制件 Prefab
Lidar construction map (overlay grid construction map)
12 combination methods and risk interpretation of database architecture optimization (books available)
轮询、中断、DMA和通道
The application of for loop and if judgment statement
P1049 [NOIP2001 普及组 T4] 装箱问题
Problems in deep learning training and testing: error: the following arguments are required: --dataroot, solution: the configuration method of training files and test files
整数a按位取反(~)后的值为-(a+1)
冰冰学习笔记:类与对象(上)
Delete in elasticserach_ by_ What is the mechanism of query?
Robot framework mobile terminal Automation Test ----- 01 environment installation
【软件测试】包装简历从这几点出发、提升通过率
[unity entry program] make my first little game
【Unity入门计划】制作我的第一个小游戏
Nailing the latest version, how to clear the login phone number history data
Bingbing's learning notes: classes and objects (Part 1)
Load capacity - sorting out the mind map that affects load capacity