• 力扣练习——55 搜索二维矩阵 II


    55 搜索二维矩阵 II

    1.问题描述
    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。要求使用二分查找。

    该矩阵具有以下特性:

    每行的元素从左到右升序排列。

    每列的元素从上到下升序排列。

    说明:以上所说的升序,由于中间存在重复元素,因此严格来说,“升序”应该理解成“非递减”

    示例:

    现有矩阵 matrix 如下:

    [

    [1, 4, 7, 11, 15],

    [2, 5, 8, 12, 19],

    [3, 6, 9, 16, 22],

    [10, 13, 14, 17, 24],

    [18, 21, 23, 26, 30]

    ]

    给定 target = 5,返回 true。

    给定 target = 20,返回 false。

    可使用以下main函数:

    int main()

    {

    vector > matrix;
    
    int target;
    
    int m,n,e;
    
    cin>>m;
    
    cin>>n;
    
    for(int i=0; i aRow;
    
        for(int j=0; j>e;
    
            aRow.push_back(e);
    
        }
    
        matrix.push_back(aRow);
    
    }
    
    cin>>target;
    
    bool res=Solution().searchMatrix(matrix,target);
    
    cout<<(res?"true":"false")<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    }

    2.输入说明
    首先输入matrix的行数m、列数n,

    然后输入m行,每行n个整数。

    最后输入一个整数target。
    3.输出说明
    输出true或false
    4.范例
    输入
    5 5
    1 4 7 11 15
    2 5 8 12 19
    3 6 9 16 22
    10 13 14 17 24
    18 21 23 26 30
    5
    输出
    true
    5.代码

    #include 
    #include 
    #include 
    #include 
    #include
    
    using namespace std;
    
    int binarySearch(vector<int>nums, int target)  //注意,这里不能把int改成bool,否则会报错
    {
    	int low = 0;
    	int high = nums.size() - 1;
    	while (low <= high)
    	{
    		int mid = (low + high) / 2;
    		if (nums[mid] < target)
    			low = mid + 1;
    		else if (nums[mid] > target)
    			high = mid - 1;
    		else
    			return mid;
    	}
    	return -1;
    }
    
    bool searchMatrix(vector<vector<int> >matrix, int target)
    {
    	//每行进行二分查找
    	
    	int m = matrix.size();//行数
    	int n = matrix[0].size();//列数
    	for (int i = 0; i < m; i++)
    	{
    		if (matrix[i][0] > target)//若该行第一个元素大于target,则该行及后面所有行都不用考虑
    			break;
    		if (matrix[i][n - 1] < target)//该行最后一个元素值小于target ,则继续搜下行
    			continue;
    		int col = binarySearch(matrix[i], target);//对第i行搜索target
    		if (col != -1)
    		{
    			//cout << "col=" << col << endl;
              return true;
    		}
    			
    	}
    	return false;
    }
    
    int main()
    
    {
    
    	vector<vector<int> > matrix;
    
    	int target;
    
    	int m, n, e;
    
    	cin >> m;
    
    	cin >> n;
    
    	for (int i = 0; i < m; i++)
    
    	{
    
    		vector<int> aRow;
    
    		for (int j = 0; j < n; j++)
    
    		{
    
    			cin >> e;
    
    			aRow.push_back(e);
    
    		}
    
    		matrix.push_back(aRow);
    
    	}
    
    	cin >> target;
    
    	bool res = searchMatrix(matrix, target);
    
    	cout << (res ? "true" : "false") << endl;
    
    	return 0;
    
    }
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
  • 相关阅读:
    设计模式之观察者模式
    cocosCreator2.4.x 打包 ios ,xcode问题记录
    ​DPDK 高效原因初探
    计算机网络概述(接入网和物理媒体)
    用 AWTK 和 AWPLC 快速开发嵌入式应用程序 (3)- 定时器
    橘子学JVM之命令行监控01之jps
    谷粒商城学习笔记
    【方法封装】时间格式化输出,获取请求设备和IP
    JS VUE 用 canvas 给图片加水印
    ms office学习记录:Word㈣ 布局&设计&引用选项卡 对应配套作业㈤
  • 原文地址:https://blog.csdn.net/qq_43403657/article/details/126186140