Skip to content

mico: 从数组里面取两个最大值 #115

Open

Description

@MicroKibaco

方法一:

 static int[] twoMaxNum(int[] data){
        if(data==null ||data.length==0)
            return null;
// 1. 用一个数组大小为2的int类型作为目标数组接收
        int result[]=new int[2];
// 2. 把这个 两个数组第一个值和第二个值 作为数组的最大值
        result[0]=result[1]=Integer.MIN_VALUE;
// 3. 遍历数组
        for(int i=1;i<data.length;i++)
            if(data[i]>result[0])
// 4. 如果数组第一个元素比遍历的结果大,交互位置
                result[0]=data[i];
        for(int i=1;i<data.length;i++)
// 5. 再遍历一次,如果数组小于目标数组元素,则进行二次交互
            if(data[i]>result[1] && data[i]< result[0])
                result[1]=data[i];
        return result;
    }

优化方案

image

Activity

  1. changed the title [-]从数组里面取两个最大值[/-] [+]mico: 从数组里面取两个最大值[/+] on Nov 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions