• JAVA经典百题之二位数组查找最大值


    题目:输入一个3行4列的数组,找出该数组中绝对值最大的元素、输出该元素的绝对值及其两个下标值。如有多个输出行号最小的,还有多个的话输出列号最小的。

    这个问题可以分为以下几个步骤来解决:

    1. 遍历整个数组,找到绝对值最大的元素,同时记录其绝对值、行号和列号。
    2. 如果有多个绝对值最大的元素,选择行号最小的,如果行号相同,则选择列号最小的。

    接下来,我将分别展示三种不同的方法来实现这个任务,并列出它们的解题思路、实现代码以及各自的优缺点。

    方法一:嵌套循环法

    解题思路:

    使用两层循环遍历整个数组,计算每个元素的绝对值,然后比较找到最大的元素。在比较时,如果找到的元素的绝对值大于当前最大元素,或者绝对值相同但行号小于当前最大元素,或者绝对值相同但行号相同且列号小于当前最大元素,则更新最大元素。

    实现代码:
    public class Main {
        public static void main(String[] args) {
            int[][] array = new int[][] {
                {1, -2, 3, 4},
                {5, -6, 7, 8},
                {-9, 10, 11, 12}
            };
    
            int maxAbsValue = 0;
            int maxRow = 0;
            int maxCol = 0;
    
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    int absValue = Math.abs(array[i][j]);
                    if (absValue > maxAbsValue || (absValue == maxAbsValue && (i < maxRow || (i == maxRow && j < maxCol)))) {
                        maxAbsValue = absValue;
                        maxRow = i;
                        maxCol = j;
                    }
                }
            }
    
            System.out.println("绝对值最大的元素:" + maxAbsValue);
            System.out.println("行号:" + maxRow);
            System.out.println("列号:" + maxCol);
        }
    }
    
    • 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
    优点:
    • 简单易懂,容易实现。
    • 时间复杂度为O(12),因为需要遍历所有元素。
    缺点:
    • 需要两层嵌套循环,可能在大型数组上效率较低。

    方法二:一次遍历法

    解题思路:

    使用一次遍历来查找绝对值最大的元素,同时记录行号和列号。这种方法更高效

    实现代码:
    public class Main {
        public static void main(String[] args) {
            int[][] array = new int[][] {
                {1, -2, 3, 4},
                {5, -6, 7, 8},
                {-9, 10, 11, 12}
            };
    
            int maxAbsValue = 0;
            int maxRow = 0;
            int maxCol = 0;
    
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    int absValue = Math.abs(array[i][j]);
                    if (absValue > maxAbsValue) {
                        maxAbsValue = absValue;
                        maxRow = i;
                        maxCol = j;
                    }
                }
            }
    
            System.out.println("绝对值最大的元素:" + maxAbsValue);
            System.out.println("行号:" + maxRow);
            System.out.println("列号:" + maxCol);
        }
    }
    
    • 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
    优点:
    • 只需要一次遍历,因此在大型数组上更高效。
    • 简单易懂。
    缺点:
    • 如果需要记录多个最大元素,此方法不适用。

    方法三:多次遍历法

    解题思路:

    分两次遍历,第一次找到绝对值最大的元素,第二次遍历用于确定是否有多个相同最大值。

    实现代码:
    public class Main {
        public static void main(String[] args) {
            int[][] array = new int[][] {
                {1, -2, 3, 4},
                {5, -6, 7, 8},
                {-9, 10, 11, 12}
            };
    
            int maxAbsValue = 0;
            int maxRow = 0;
            int maxCol = 0;
    
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    int absValue = Math.abs(array[i][j]);
                    if (absValue > maxAbsValue) {
                        maxAbsValue = absValue;
                        maxRow = i;
                        maxCol = j;
                    }
                }
            }
    
            int count = 0;
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    int absValue = Math.abs(array[i][j]);
                    if (absValue == maxAbsValue) {
                        count++;
                    }
                }
            }
    
            System.out.println("绝对值最大的元素:" + maxAbsValue);
            System.out.println("行号:" + maxRow);
            System.out.println("列号:" + maxCol);
        }
    }
    
    • 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
    优点:
    • 能够处理多个相同最大值的情况。
    缺点:
    • 需要两次遍历,可能在大型数组上效率较低。

    总结与推荐:

    从上面的三种方法中,方法二(一次遍历法)是最简单和高效的,因为它只需要一次遍历即可找到答案。方法一(嵌套循环法)虽然简单,但在大型数组上性能可能较低。方法三(多次遍历法)能够处理多个相同最大值的情况,但需要两次遍历,因此在大型数组上效率较低。

    综合考虑,方法二是最好的选择,因为它简单、高效,并且满足了问题的基本要

  • 相关阅读:
    【深度学习】YOLO-Pose 人体关键点估计 人体姿态估计
    [附源码]计算机毕业设计springboot网上书城网站
    PyQt5_股票K线形态查看工具
    web 安全总结
    深度学习500问——Chapter03:深度学习基础(1)
    refseq数据库的特点_eureka如何剔除服务
    重装系统后新建文本文档打不开怎么办
    对zygote的理解
    CI2454集成2.4G收发SOC【遥控;灯具;玩具】技术开发资料
    主定理(Master Theorem)推导和理解(2)
  • 原文地址:https://blog.csdn.net/2302_79769114/article/details/133742615