数组的旋转意味着每个元素通过一个索引向右移动,并且数组的最后一个元素也将其移至第一名



例如, array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]的旋转。目的是旋转数组a k次。也就是说,a的每个元素将通过k索引向右移动。

例如,给定的数组A = [3, 8, 9, 7, 6]K = 3,该功能应返回[9, 7, 6, 3, 8]

我想要在爪哇。我已经尝试过。

public static int[] rotation(int[] a,int k) {
    int[] newArray = new int[a.length];
    for(int i = 0 ; i < a.length ; i++) {
        int newPosition = (i + k)%a.length;
        newArray[newPosition] = a[i];
    }
    return newArray;
}

我没有得到确切想要的主题启动器,但在这里我的代码

class Solution {
public int[] solution(int[] arr, int k) {
    int[] newArr = new int[arr.length];
    if (arr.length == 0) return arr;
       k = k%arr.length;
    for (int i=0; i<arr.length; i++) {
      newArr[i] = arr[(i + (arr.length - k)) % (arr.length)];
    }
    return newArr;
}

}

您也可以使用A = B.clone();

public int[] solution(int[] A, int K) {
    // write your code in Java SE 8
    int [] B =new int [A.length];
    for(int l=0;K>l;K--){
        int j=0;
        for(int i=0;i<A.length;i++){
            if(i==0){
                B[j]=A[A.length-1];
                j++;
            }
            else{
                B[j]=A[i-1];
                j++;
            }
        }
        //below part
        /*for(int i= 0;i<A.length;i++){
            A[i]=B[i];
        }*/
        A = B.clone();
    }
    return B;
} 

如果您喜欢:D

您可以使用Arrays.toString打印结果。例如:

System.out.println(Arrays.toString(rotation(new int[] { 3, 8, 9, 7, 6}, 3)));
public int[] solution(int[] A, int K) {
    // write your code in Java SE 8
    int [] B =new int [A.length];
    for(int l=0;K>l;K--){
        int j=0;
        for(int i=0;i<A.length;i++){
            if(i==0){
                B[j]=A[A.length-1];
                j++;
            }
            else{
                B[j]=A[i-1];
                j++;
            }
        }
        for(int i= 0;i<A.length;i++){
            A[i]=B[i];
        }
    }
    return B;
} 
  function solution(A, K) {
        function shiftArray(arrayToShift, newArray=[] ){
            newArray[0] = arrayToShift[arrayToShift.length-1] ; 
            for (var i=1; i<arrayToShift.length; i++){
                newArray[i] = arrayToShift[i-1];   
            }
            // console.log("arrayToShift");
            // console.log(newArray);
            return newArray;
        }

        var newArray = A;
        for(var i=0; i<K; i++){
            newArray =  shiftArray(newArray);
        }
        return newArray
    }

console.log(solution([3, 8, 9, 7, 6], 3));

实现了100%正确性

public int[] solution(int[] A, int K) {
    // write your code in Java SE 8
    if(K == A.length || A.length == 0)
        return A;
    if(K > A.length) {
        K = K%A.length;
    }
    int[] arr1 = Arrays.copyOfRange(A, A.length-K, A.length);
    int[] arr2 = Arrays.copyOfRange(A, 0, A.length-K);
    int aLen = arr1.length;
    int bLen = arr2.length;
    int[] result = new int[aLen + bLen];
    System.arraycopy(arr1, 0, result, 0, aLen);
    System.arraycopy(arr2, 0, result, aLen, bLen);
    return result;
}

这是使用JavaScript,测试100%的解决方案。

function solution(A, K) {
    for(let i=0; i<K; i++) {
        let lastIndex = A.length - 1;
        let lastItem = A[lastIndex];
        for(let j=(A.length-1); j>-1; j--) {
            if(j>0) {
                A[j] = A[j-1];
            } else {
                A[j] = lastItem;
            }
        }
    }
    return A;
}

类解决方案{ public int []解决方案(int [] a,int k){

    if((A.length == 0) || (K == 0)){
        return A;
    }
    int [] B = new int[A.length];
    int c = K;
    while(c != 0){
        for(int i = 1; i< A.length; i++){
            B[i] = A[i-1];
        }
        c--;
        B[0] = A[A.length-1];
        System.arraycopy(B, 0, A, 0, A.length);
    }
    return A;
}

}

在kotlin中:

fun flipList(list: List<Int>, times: Int): List<Int> {
    val flippedList = list.toMutableList()
    val referenceList = list.toMutableList()
    repeat(times) {
        var position = 0
        referenceList.forEach {
            position++
            if (position < list.size)
                flippedList[position] = referenceList[position -1]
            else
                flippedList[0] = referenceList.last()
        }
        referenceList.clear()
        referenceList.addAll(flippedList)
    }
    return flippedList
}

单位teste

class FlipListUnitTest {
        private val referenceListMock = listOf(1,2,3,4)
        private val referenceListOneTimeFlipResultMock = listOf(4,1,2,3)
        private val referenceListFourTimesFlipResultMock = listOf(1,2,3,4)
        @Test
        fun `check test api is working`(){
            assertTrue(true)
        }
        @Test
        fun `check list flip 1 time`(){
            assertTrue(flipList(referenceListMock, 1) == referenceListOneTimeFlipResultMock)
        }
        @Test
        fun `check list flip 4 time`(){
            assertTrue(flipList(referenceListMock, 4) == referenceListFourTimesFlipResultMock)
        }
}

在我的代码

public static int[] solution(int[] A, int K){
    if(A.length > 0) {
        for(int i = 0; i < K ; i++){
            int temp = A[A.length - 1];
            for(int j = A.length - 2; j >= 0; j--){
                A[j + 1] = A[j];
            }
            A[0] = temp;
        }
    }
    return A;
}
public static int[] solution(int[] A, int K) {
    if(A.length<1)
        return A;
    int[] newArray = new int[A.length];
    while (K>0){
        newArray[0]=A[A.length-1];
        for(int i=0; i<A.length-1; i++){
            newArray[i+1]=A[i];
        }
        for (int i=0; i<newArray.length; i++) {
            A[i]=newArray[i];
        }
        K--;
    }
    return A;
}

给出了由n个整数组成的数组。阵列的旋转意味着每个元素被一个索引向右移动,并且数组的最后一个元素被移至第一个位置。

嗨,大家,这是Java中此问题的另一个简单解决方案,100%工作。

class Solution {
    public int[] solution(int[] A, int K) {
        
        // Corner cases to save resources
         if(K == 0 || A.length <= 0 || A.length == K){
             return A;
         }
         
         // Loop to traverse K times
         for(int i=0; i<K; i++){
             int last = A[A.length - 1]; // Last digit
             
             // Loop to traverse A.Length times in swing order,
             // so that first element can be set
             for(int j=A.length-1; j>0; j--){
              A[j] = A[j-1];   
             }
             
             // Set last element
             A[0] = last;
         }
         
         // Return result
         return A;
    }
}

这是我在JavaScript中的答案

function solution(A, K){
let arr = [];
let lastIndex = A.length -1;
        let rotation = K-1;
    for(let i = 0; i < K; i++){
        arr[rotation] = A[lastIndex];
        --lastIndex; 
        --rotation;
    }
    for(let j = 0; j <= lastIndex; j++){
        arr.push(A[j]);
    }
    return arr;
}

这是我在python中的答案,而无需循环并使用字符串替换

def solution(A, K):
    K = K % len(A) if K > len(A) else K
    if (K == 0) or (K == len(A)) or (len(A) in [0, 1]):
        return A
    
    first_index = len(A) - K
    return A[first_index:] + A[:first_index]
function solution(A, K) {
   let tmpA = [];
   for (let i = 1; i <= K; i++){
       tmpA.unshift(A.pop());
   }
   for (let i = 1; i <= K; i++){
       A.unshift(tmpA.pop());
   }
   return A;   
}

这是用PHP编写的代码,但逻辑可以用任何语言使用。

function solution($A, $K) {
// write your code in PHP7.0      
    $cnt = count($A);
    for($i=1; $i<=$K; $i++){ 
        foreach($A as $key=>$val){
            $key++;
            if($key==$cnt){
                $A[0] = $val;
            }
            else
            {   
                $A[$key] = $val;
            }
        }
    }
    return $A;
}

javaScript |得分:100%

功能解决方案(a,k){

for(let i=0 ; i<K ; i++){
    let lastIndex = A.length-1;
    let lastNumber = A[lastIndex];
    A.unshift(lastNumber);
    A.pop();
}
return A;

}

public static int[] rotateArrayKTimes(int[] A, int K) {
        if (A.length == 0 || A.length > 1000 || K > 100) {
            return new int[]{};
        }
        for (int index = 0; index < K; index++) {
            int temp = A[A.length - 1];
            System.arraycopy(A, 0, A, 1, A.length - 1);
            A[0] = temp;
        }
        return A;
    }

java解决方案 -

class Solution2 {
    public int[] solution(int[] arr, int k) {
        int temp;
        int startindex = 0;
        if (arr.length != 0) {
            for (int i = 1; i <= k; i++) {
                temp = arr[arr.length - 1];
                for (int j = arr.length - 1; j >= startindex; j--) {
                    if (j != (startindex)) {
                        arr[j] = arr[j - 1];
                    } else
                        arr[startindex] = temp;
                }
            }
        } else
            System.out.println("This is empty array");
        return arr;
    }
}

最新更新