如何找到最大的数字,如果数组为空则为null

时间:2022-12-26 21:30:23

fun indexOfMax(a: IntArray): Int? { return 0 }

有趣的indexOfMax(a:IntArray):Int? {return 0}

link to task https://try.kotlinlang.org/#/Examples/Problems/Index%20of%20Maximum/Index%20of%20Maximum.kt

链接到任务https://try.kotlinlang.org/#/Examples/Problems/Index%20of%20Maximum/Index%20of%20Maximum.kt


my try :

我的尝试:

fun indexOfMax(a: IntArray): Int? {

 var max = 0
 for (i 0..lastIndex){
 e = a[i]

 if (e > max){
  e = max 
  }
}
return max
}

2 个解决方案

#1


3  

In Kotlin you can do it in one line of code.

在Kotlin中,您可以在一行代码中完成。

If you are looking for the max element or null if the array is empty:

如果要查找max元素,如果数组为空,则返回null:

return  array.max()

If you are looking for the index of the max element or null if the array is empty:

如果要查找max元素的索引,如果数组为空,则返回null:

return array.max()?.let { array.indexOf(it) }

In the given problem you need to look for the last index:

在给定的问题中,您需要查找最后一个索引:

return array.max()?.let { array.lastIndexOf(it) }

The source code of the functions:

功能的源代码:

/**
 * Returns the largest element or `null` if there are no elements.
 */
public fun IntArray.max(): Int? {
    if (isEmpty()) return null
    var max = this[0]
    for (i in 1..lastIndex) {
        val e = this[i]
        if (max < e) max = e
    }
    return max
}

And

/**
 * Returns first index of [element], or -1 if the array does not contain element.
 */
public fun IntArray.indexOf(element: Int): Int {
    for (index in indices) {
        if (element == this[index]) {
            return index
        }
    }
    return -1
}

#2


0  

If i want to find the maximum number from a list of longs..

如果我想从长列表中找到最大数量..

ArrayList<Long> CallsTimes = new ArrayList<>();
Long HighestTimeOfCall;

HighestTimeOfCall = Collections.max(CallsTimes);

In your case it just should be ArrayList<Int> CallsTimes = new ArrayList<>(); HighestTimeOfCall should be int.

在你的情况下,它应该是ArrayList CallsTimes = new ArrayList <>(); HighestTimeOfCall应为int。


if(CallsTimes.size() > 0)
{
  // Its empty array list show error or do something else
}
else
{
  // find max int as shown above
}

#1


3  

In Kotlin you can do it in one line of code.

在Kotlin中,您可以在一行代码中完成。

If you are looking for the max element or null if the array is empty:

如果要查找max元素,如果数组为空,则返回null:

return  array.max()

If you are looking for the index of the max element or null if the array is empty:

如果要查找max元素的索引,如果数组为空,则返回null:

return array.max()?.let { array.indexOf(it) }

In the given problem you need to look for the last index:

在给定的问题中,您需要查找最后一个索引:

return array.max()?.let { array.lastIndexOf(it) }

The source code of the functions:

功能的源代码:

/**
 * Returns the largest element or `null` if there are no elements.
 */
public fun IntArray.max(): Int? {
    if (isEmpty()) return null
    var max = this[0]
    for (i in 1..lastIndex) {
        val e = this[i]
        if (max < e) max = e
    }
    return max
}

And

/**
 * Returns first index of [element], or -1 if the array does not contain element.
 */
public fun IntArray.indexOf(element: Int): Int {
    for (index in indices) {
        if (element == this[index]) {
            return index
        }
    }
    return -1
}

#2


0  

If i want to find the maximum number from a list of longs..

如果我想从长列表中找到最大数量..

ArrayList<Long> CallsTimes = new ArrayList<>();
Long HighestTimeOfCall;

HighestTimeOfCall = Collections.max(CallsTimes);

In your case it just should be ArrayList<Int> CallsTimes = new ArrayList<>(); HighestTimeOfCall should be int.

在你的情况下,它应该是ArrayList CallsTimes = new ArrayList <>(); HighestTimeOfCall应为int。


if(CallsTimes.size() > 0)
{
  // Its empty array list show error or do something else
}
else
{
  // find max int as shown above
}