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 row.

 

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

C++实现代码如下:

#include<iostream>
#include<vector>
using namespace std;

class Solution
{
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target)
    {
        if(matrix.empty()||matrix[0].empty())
            return false;
        int i=0;
        int j=matrix[0].size()-1;
        int temp;
        while(i<(int)matrix.size()&&j>=0)
        {
            temp=matrix[i][j];
            if(target==temp)
                return true;
            else if(target<temp)
                j--;
            else if(target>temp)
                i++;
        }
        return false;
    }
};

int main()
{
    Solution s;
    vector<vector<int> > vec=
    {
        {-10,-9},
        {-7,-6},
        {-5,-4},
        {-3,-2}
    };
    cout<<s.searchMatrix(vec,-6)<<endl;
}

开始提交了一种,死活通不过。

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        if(matrix.empty()||matrix[0].empty())
            return false;
        size_t i=0;
        size_t j=matrix[0].size()-1;
        int temp;
        while(i<matrix.size()&&j>=0)
        {
            temp=matrix[i][j];
            if(target==temp)
                return true;
            if(target<temp)
            {
                j--;
                continue;
            }
            if(target>temp)
            {
                i++;
                continue;
            }
        }
        if(i>=matrix.size()||j<0)
            return false;
        return true;
    }
};

一直报错Last executed input:[[-10],[-7],[-4]], -6

就因为将i和j声明为size_t类型,可能出现下溢。可以参考:https://oj.leetcode.com/discuss/11366/why-is-the-last-executed-input-error

时间: 2024-10-22 13:13:17

Search a 2D Matrix的相关文章

[LeetCode]74.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 previo

[LeetCode]240.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. Integers in each column are sorted in ascending from top to botto

[LeetCode] 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. Integers in each column are sorted in ascending from top to bottom.

[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总结之二分查找篇

二分查找算法虽然简单,但面试中也比较常见,经常用来在有序的数列查找某个特定的位置.在LeetCode用到此算法的主要题目有: Search Insert Position Search for a Range Sqrt(x) Search a 2D Matrix Search in Rotated Sorted Array Search in Rotated Sorted Array II 这类题目基本可以分为如下四种题型: 1. Search Insert Position和Search fo

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

careercup-排序和查找 11.6

11.6 给定M*N矩阵,每一行.每一列都按升序排序,请编写代码找出某元素. 类似leetcode:Search a 2D Matrix 但是与leetcode中这题不同的是下一行的第一个元素不一定大于上一行的最后一个元素.所以使用二分查找有点麻烦. 解法一:通过观察我们可知: 若列的开头大于x,那么x位于该列的左边: 若列的末端小于x,那么x位于该列的右边: 若行的开头小于x,那么x位于改行的上方: 若行的末端小于x,那么x位于改行的下方 我们可以从任意位置开始搜索,不过,让我们从列的起始元素

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能