【leetcode】Search 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.

 1 class Solution {
 2 public:
 3     bool searchMatrix(vector<vector<int> >& matrix, int target) {
 4         int col=matrix[0].size();
 5         int row=matrix.size();
 6
 7         int targetrow=0;
 8         int flag=0;
 9
10
11         if(0==col-1&&0==row-1)
12         {
13             if(target==matrix[0][0])
14                 return true;
15             else
16                 return false;
17         }
18
19
20         for(int i=0;i<row;i++)
21         {
22             if(target<matrix[0][0])
23             {
24                 return false;
25             }
26             if(target==matrix[i][0])
27             {
28                 return true;
29             }
30             if(target<matrix[i][0]&&i>=1)
31             {
32                 flag=1;
33                 targetrow=i-1;
34                 break;
35             }
36             if(target<matrix[row-1][col-1])
37             {
38                 flag=1;
39                 targetrow=row-1;
40             }
41             if(target==matrix[row-1][col-1])
42             {
43                 return true;
44             }
45             flag=0;
46         }
47
48
49         for(int i=0;i<col;i++)
50         {
51             if(target==matrix[targetrow][i])
52             {
53                 flag=1;
54                 break;
55             }
56             flag=0;
57         }
58
59         if(0==flag)
60             return false;
61         else
62             return true;
63     }
64 };
时间: 2024-11-03 22:25:13

【leetcode】Search 2D Matrix的相关文章

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在

【Leetcode】Search a 2D Matrix II

题目链接:https://leetcode.com/problems/search-a-2d-matrix-ii/ 题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Intege

【Leetcode】Search a 2D Matrix in JAVA

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【leetcode】 Search a 2D Matrix (easy)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro