zoukankan      html  css  js  c++  java
  • leetcode — search-a-2d-matrix

    /**
     * Source : https://oj.leetcode.com/problems/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.
     *
     */
    public class SearchMatrix {
    
        /**
         * 因为矩阵是连续有序的,所以可以当做一维数组处理,使用二分法搜索
         * 也可以使用二分法先搜索第一列,确定处于哪一行,再对该行使用二分法搜索
         *
         *
         *
         * @return
         */
        public boolean search (int[][] matrix, int target) {
            if (matrix.length == 0 || matrix[0].length == 0) {
                return false;
            }
            int m = matrix.length;
            int n = matrix[0].length;
            int left = 0;
            int right = m * n;
            int mid = (left + right) / 2;
            int midi = mid / n;
            int midj = mid % n;
            while (left <= right) {
                if (matrix[midi][midj] == target) {
                    return true;
                }
                if (matrix[midi][midj] > target) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
                mid = (left + right) / 2;
                midi = mid / n;
                midj = mid % m;
            }
            return false;
        }
    
    
        public static void main(String[] args) {
            SearchMatrix searchMatrix = new SearchMatrix();
            int[][] matrix = new int[][]{
                    {1,   3,  5,  7},
                    {10, 11, 16, 20},
                    {23, 30, 34, 50}
            };
            System.out.println(searchMatrix.search(matrix, 3));
            System.out.println(searchMatrix.search(matrix, 11));
            System.out.println(searchMatrix.search(matrix, 34));
            System.out.println(searchMatrix.search(matrix, 0));
    
        }
    }
    
  • 相关阅读:
    动态规划问题
    神经网络学习总结第二天
    神经网络学习第一天总结
    解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
    IntelliJ IDEA 历史版本下载地址
    第九章 数据查询基础
    第八章 用SQL语句操作数据
    第七章 用表组织数据
    第六章 程序数据库集散地:数据库
    linux文件或文件夹常见操作,排查部署在linux上程序问题常用操作
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7727002.html
Copyright © 2011-2022 走看看