![](/img/trans.png)
[英]how to find the index of the maximum element of an integer array with null elements in it?
[英]Rotate array and find index of maximum element
我正在開發一個程序,在該程序中,我需要找出一個數組的索引位置,該數組在執行該數組的左旋轉后會保留該數組中的最大值。
例如:
如果array為{5,7,1,8,2}。 該數組的最大值為8。如果我將數組旋轉2次,則結果為1,8,2,5,7。 那么最大元素8的索引位置為1。
如果將數組{5,7,1,8,2}旋轉6次,則數組變為7,1,8,2,5。 現在索引8是2。
我的程序將數組和轉數作為輸入,並返回一個數組,其中包含旋轉數組時max元素的索引位置。
int[] rotate(int[] array, int[] rotate) {
int[] result = new int[rotate.length];
int index = 0;
int large = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > large) {
large = array[i];
index = i;
}
}
for (int i = 0; i < rotate.length; i++) {
int r = rotate[i];
if (index - r < 0) {
if (r > array.length) {
r = r % array.length;
result[i] = index - r;
} else {
result[i] = index - (index - r);
}
} else {
result[i] = index - r;
}
}
return result;
}
該程序在某些情況下可以工作,但對於其他我無法找到的測試用例卻無法通過。
您能幫我在此代碼中做錯的地方嗎?
您可以使用以下功能。 看到它在這里工作:
int[] rotate(int[] array, int[] rotate)
{
int[] result = new int[rotate.length];
int index = 0;
int large = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > large) {
large = array[i];
index = i;
}
}
int len = array.length;
for (int i = 0; i < rotate.length; i++) {
int r = (index - (rotate[i]%len));
result[i] = (r>=0) ? r : (len+r);
}
return result;
}
以下是完整的代碼:
class Test
{
static int[] rotate(int[] array, int[] rotate)
{
int[] result = new int[rotate.length];
int index = 0;
int large = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > large) {
large = array[i];
index = i;
}
}
int len = array.length;
for (int i = 0; i < rotate.length; i++) {
int r = (index - (rotate[i]%len));
result[i] = (r>=0) ? r : (len+r);
}
return result;
}
public static void main (String[] args) throws java.lang.Exception
{
int nums[] = {5,7,1,8,2};
int r[] = {2, 6, 5, 1, 2, 3, 4, 5, 0};
int res[] = rotate(nums, r);
for(int i=0; i<res.length; i++)
{
System.out.println(r[i] + " = "+ res[i]);
}
}
}
輸出 :
2 = 1
6 = 2
5 = 3
1 = 2
2 = 1
3 = 0
4 = 4
5 = 3
0 = 3
獲取最大數字的索引,您的代碼有效
int index = 0; int large = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > large) { large = array[i]; index = i; } }
旋轉陣列
//Rotate the array until the maximum value is the first index for (int rotations = 0; rotations < index; rotations++) { int temp = rotate[0]; //Store the first value of the array in a temporary value for (int i = 1; i < rotate.length; i++) { rotate[i - 1] == rotate[i]; //move each value one index to the left, replacing the old value at i with the one next to it } rotate[rotate.length - 1] = temp; //replace the last index of the array with the stored temp value ("Rotating it left") } return rotate; //return the new array which is now rotated
還要注意,在Java中,您無需返回數組值,只需將其傳遞到方法中,更改值,原始數組將與您在此方法內部修改的數組相同。 一個例子是...
Arrays.sort(array);
System.out.println(array);
//or
Collections.shuffle(Arrays.asList(array));
System.out.println(array);
您可以使用流來查找索引,然后通過每次旋轉來偏移它:
int[] rotate(int[] array, int[] rotate) {
int len = array.length;
// find the index of the greatest element
int index = IntStream.range(0, len)
.reduce(Integer.MIN_VALUE, (a, b) -> array[a] > array[b] ? a : b);
// offset the index by each rotation
return Arrays.stream(rotate)
.map(r -> (index + len + (r % len)) % len)
.toArray();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.