首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在java中搜索二维数组

在Java中搜索二维数组可以使用循环遍历或者二分查找算法来实现。下面是两种方法的示例代码:

  1. 循环遍历方法:
代码语言:java
复制
public class SearchIn2DArray {
    public static boolean search(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        
        int rows = matrix.length;
        int cols = matrix[0].length;
        int row = 0;
        int col = cols - 1;
        
        while (row < rows && col >= 0) {
            if (matrix[row][col] == target) {
                return true;
            } else if (matrix[row][col] < target) {
                row++;
            } else {
                col--;
            }
        }
        
        return false;
    }
    
    public static void main(String[] args) {
        int[][] 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}
        };
        int target = 5;
        boolean result = search(matrix, target);
        System.out.println("Result: " + result);
    }
}
  1. 二分查找方法(要求二维数组每行按照升序排列):
代码语言:java
复制
public class SearchIn2DArray {
    public static boolean search(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        
        int rows = matrix.length;
        int cols = matrix[0].length;
        int left = 0;
        int right = rows * cols - 1;
        
        while (left <= right) {
            int mid = (left + right) / 2;
            int midValue = matrix[mid / cols][mid % cols];
            
            if (midValue == target) {
                return true;
            } else if (midValue < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return false;
    }
    
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 3, 5, 7},
            {10, 11, 16, 20},
            {23, 30, 34, 60}
        };
        int target = 3;
        boolean result = search(matrix, target);
        System.out.println("Result: " + result);
    }
}

以上代码示例中,第一个方法是通过循环遍历二维数组来搜索目标值,时间复杂度为O(m+n),其中m为行数,n为列数。第二个方法是利用二分查找算法在有序的二维数组中搜索目标值,时间复杂度为O(log(m*n)),其中m为行数,n为列数。

推荐的腾讯云相关产品:腾讯云云服务器(ECS)和腾讯云数据库(TencentDB)。

  • 腾讯云云服务器(ECS):提供弹性计算服务,可根据业务需求快速创建、部署和扩展云服务器实例。了解更多信息,请访问:腾讯云云服务器
  • 腾讯云数据库(TencentDB):提供多种数据库产品,包括关系型数据库(MySQL、SQL Server、PostgreSQL等)和非关系型数据库(MongoDB、Redis等),满足不同业务场景的需求。了解更多信息,请访问:腾讯云数据库
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

4分36秒

【剑指Offer】4. 二维数组中的查找

23.8K
17分25秒

072 - Java入门极速版 - 基础语法 - 常用类和对象 - 数组 - 二维数组

12分30秒

day07_数组/04-尚硅谷-Java语言基础-复习:一维数组与二维数组

12分30秒

day07_数组/04-尚硅谷-Java语言基础-复习:一维数组与二维数组

12分30秒

day07_数组/04-尚硅谷-Java语言基础-复习:一维数组与二维数组

10分26秒

day07_数组/05-尚硅谷-Java语言基础-二维数组练习:基本操作

10分26秒

day07_数组/05-尚硅谷-Java语言基础-二维数组练习:基本操作

10分26秒

day07_数组/05-尚硅谷-Java语言基础-二维数组练习:基本操作

16分50秒

day07_数组/06-尚硅谷-Java语言基础-二维数组练习:杨辉三角

16分50秒

day07_数组/06-尚硅谷-Java语言基础-二维数组练习:杨辉三角

16分50秒

day07_数组/06-尚硅谷-Java语言基础-二维数组练习:杨辉三角

6分52秒

day06_Eclipse的使用与数组/17-尚硅谷-Java语言基础-二维数组元素的调用

领券