Java 查找数字中的最高和第二高数字

Java 查找数字中的最高和第二高数字,java,Java,在找到一个数字中的最高数字后,如何仅使用循环和if语句来查找该数字中的第二高数字 public static int maximum(int max){ while(num != 0){ int rightDigit = num % 10; num /= 10; if(rightDigit > max) rightDigit = max; } r

在找到一个数字中的最高数字后,如何仅使用循环和if语句来查找该数字中的第二高数字

    public static int maximum(int max){

while(num != 0){
            int rightDigit = num % 10;
            num /= 10;
            if(rightDigit > max)
                rightDigit = max;
        }
        return max;
        }

使用
列表
存储所有数字并对其进行
排序
这样您就可以根据需要访问最高、次高和最低数字。要排序
列表
请使用
集合。排序(列表)

这是您想要的吗?数组中的第一个元素最大,第二个元素第二大。如果没有这样的元素,则返回-1

public static void main(String[] args) {
    int[] tab = maximum(12);
    System.out.println("Largest digit: " + tab[0]);
    System.out.println("Second largest digit: " + tab[1]);
}
public static int[] maximum(int max){
    int num = max;
    int largest = -1;
    int secondLargest = -1;
    while(num != 0){
        int rightDigit = num % 10;
        num /= 10;

        if(rightDigit > largest) {
            secondLargest = Math.max(secondLargest, largest);
            largest = rightDigit;

        } else if(rightDigit > secondLargest)
            secondLargest = rightDigit;
    }
    return new int[]{largest,secondLargest};
    }
public int secondMax(整数编号){
列表=新的ArrayList();
而(数量>0){
列表。添加(编号%10);
数字=数字/10;
}
集合。排序(列表);
int size=list.size();
返回列表。获取(大小为2);
}

假设maxValue是最高值,您可以轻松识别第二高值

   if (times[i] > maxValue) {
        secondhighest  = maxValue;
        maxValue = times[i];
    } else if (times[i] > secondhighest) {
        secondhighest  = times[i];
    }
int num=1395248,n,i,n2;
对于(n2=i=n=0;num>0;i=num%10,n2=n
对数字列表进行排序并获得第一和第二大数字充其量只能给您提供时间复杂度
O(n*logn)
时间复杂度(假设您使用)。
如果使用另一种方法,可以获得更好的性能:对数组进行分区(重新排序)(如在快速排序中),因此将有一个pivot值将数组分为两部分:小于pivot的部分位于左部分(左子数组),较大的部分位于右部分(右子数组)。检查轴的索引:

  • 如果它等于一个数字数组的大小减去2,那么它就是第二大元素(第一大元素在它旁边,在右边的子数组中)
  • 如果pivot的索引小于一个数字数组的大小减2,则对右边的子数组重复分区
  • 如果pivot的索引大于一个数字数组的大小减2,则对左边的子数组重复分区
在某一点上,您的轴将是数组末尾的第二个元素,这意味着它是第二大元素,最大的数字位于数组末尾(因为获取轴的方式)。时间复杂度要比快速排序好,因为在每个分区之后,您只对一个子数组进行分区,而不是两个子数组都进行分区

您可以扩展此方法,不仅可以获得第一个和第二个最大数字,还可以获得第k个(任意最高)数字,不仅可以获得最大数字,还可以获得最小数字

请查看我几天前编写的代码:

public Long selectKthElement(int left, int right, int k, Type type) {
    int med = partitionIt(left, right);

    if ((type.equals(Type.Smallest) && med == k - 1) || (type.equals(Type.Largest) && med == nElems - k)) {
        return theArray[med];
    } else if (type.equals(Type.Smallest) && med > k || type.equals(Type.Largest) && med > nElems - k) {
        return selectKthElement(left, med - 1, k, type);
    } else if (type.equals(Type.Smallest) && med < k || type.equals(Type.Largest) && med < nElems - k){
        return selectKthElement(med + 1, right, k, type);
    } else {
        // impossible case, but the source code won't compile w/o the else
        return null;
    }
}
public Long selectk工具(int left、int right、int k、Type){
int med=分区(左、右);
if((type.equals(type.minimable)和&med==k-1)| |(type.equals(type.maximust)和&med==nElems-k)){
返回阵列[med];
}else if(type.equals(type.minimable)和&med>k | | type.equals(type.maximust)和&med>nElems-k){
返回SelectkHelement(左,中-1,k,类型);
}else if(type.equals(type.minimable)和&med
这里的数组是一个数字数组,
partitionIt
方法对数组重新排序并返回中位数的索引,您可以自己编写实现,也可以通过web进行搜索。

public static int nthHighest(int[]arr,int n){
    public static int nthHighest(int[] arr, int n) {
         List<Integer> lst = Arrays.asList(ArrayUtils.toObject(arr)); //use apache commons library
         Collections.sort(lst);
         return lst.get(arr.length-n);
    }
List lst=Arrays.asList(ArrayUtils.toObject(arr));//使用ApacheCommons库 集合。排序(lst); 返回lst.get(arr.length-n); }
静态void Main(字符串[]args)
{
int max=0,temp=0,secondMax=0,number=0;
数字=6541891;
while(数字!=0)
{
温度=数字%10;
如果(最大==0)
{
最大值=温度;
secondMax=温度;
}
否则,如果(温度>最大值)
{
int lastmax=max;
最大值=温度;
如果(lastmax>secondMax)
{
secondMax=lastmax;
}
}
如果((温度>秒最大值和温度<最大值)|秒最大值>=最大值)
{
secondMax=温度;
}
数字=数字/10;
}
int Result=secondMax;
}

在一个循环中实现这一点的唯一方法是保留所有值的列表,对它们进行排序,然后取前两个。那么,您是否正在尝试从
125673
获取
7
5
?另外,您是否正在检查重复项?由于num=0,您的while循环将不会被访问。@Chris或只保留对周围两个最高数字的引用,然后使用if块使它们保持同步。如果数字为52163429,则最高和第二高的数字为9和6。如果数字为45886,则最高值和第二高值应分别为8和6。此外,还应使用循环和if语句来完成。考虑到他正在处理int(最大长度为10),这是迄今为止最简单的解决方案。这是最实用的,只要OP不要求做作业,我本想用第二个变量添加另一个解决方案,但最终认为,对于数字量来说,它最终会变成几乎不可读的混乱代码,这从设计上来说要干净得多perspective@Marco,Set表示没有重复项。OP并没有指定这一点。也许
TreeList
是正确的structure@user2991964作业整数的最大长度为10位。任何解决方案的总运行时间与下一个解决方案相差不远。与其说是效率,不如说是可读性和简洁性。@TimPote我知道,这听起来很幼稚,但我就是忍不住要证明我知道一点更有效的解决方案。:)
public Long selectKthElement(int left, int right, int k, Type type) {
    int med = partitionIt(left, right);

    if ((type.equals(Type.Smallest) && med == k - 1) || (type.equals(Type.Largest) && med == nElems - k)) {
        return theArray[med];
    } else if (type.equals(Type.Smallest) && med > k || type.equals(Type.Largest) && med > nElems - k) {
        return selectKthElement(left, med - 1, k, type);
    } else if (type.equals(Type.Smallest) && med < k || type.equals(Type.Largest) && med < nElems - k){
        return selectKthElement(med + 1, right, k, type);
    } else {
        // impossible case, but the source code won't compile w/o the else
        return null;
    }
}
    public static int nthHighest(int[] arr, int n) {
         List<Integer> lst = Arrays.asList(ArrayUtils.toObject(arr)); //use apache commons library
         Collections.sort(lst);
         return lst.get(arr.length-n);
    }
static void Main(string[] args)
{




    int max = 0, temp = 0, secondMax = 0, number = 0;

    number = 6541891;
    while (number != 0)
    {
        temp = number % 10;

        if (max == 0)
        {

            max = temp;
            secondMax = temp;

        }
        else if (temp > max)
        {
            int lastmax = max;

            max = temp;

            if (lastmax > secondMax)
            {
                secondMax = lastmax;
            }


        }

        if ((temp > secondMax && temp < max) || secondMax >= max)
        {
            secondMax = temp;
        }

        number = number / 10;
    }

    int Result = secondMax;

}