【问题标题】:Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place数组的旋转意味着每个元素右移一个索引,数组的最后一个元素也移到第一位
【发布时间】:2016-12-30 07:03:11
【问题描述】:

例如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]

我想要这个在 java 中。 这个我试过了。

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;
}

【问题讨论】:

标签: java arrays


【解决方案1】:

我没有得到真正想要的主题启动器,但这里是我的任务代码

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;
}

}

【讨论】:

  • 很好的解决方案!
【解决方案2】:

你也可以使用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

【讨论】:

    【解决方案3】:

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

    System.out.println(Arrays.toString(rotation(new int[] { 3, 8, 9, 7, 6}, 3)));
    

    【讨论】:

    • 你可以对这么小的东西发表评论。
    • 它回答了 Dhaval Mistry 评论中解释的问题。一个小问题得到一个小答案。我添加了一个示例。
    【解决方案4】:
    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;
    } 
    

    【讨论】:

    • 请添加一些描述以解释其他人的利益。
    【解决方案5】:
      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));
    

    【讨论】:

      【解决方案6】:

      实现了 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;
      }
      

      【讨论】:

        【解决方案7】:

        这是我使用 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;
        }
        

        【讨论】:

          【解决方案8】:

          类解决方案{ 公共 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;
          }
          

          }

          【讨论】:

            【解决方案9】:

            在 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
            }
            

            单元测试

            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)
                    }
            }
            

            【讨论】:

              【解决方案10】:

              在我的代码中

              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;
              }
              

              【讨论】:

                【解决方案11】:
                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;
                }
                

                【讨论】:

                  【解决方案12】:

                  给定一个由 N 个整数组成的数组 A。数组的旋转意味着每个元素右移一个索引,数组的最后一个元素移动到第一位。

                  大家好,这里是 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;
                      }
                  }
                  
                  

                  【讨论】:

                    【解决方案13】:

                    这是我在 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;
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2021-03-14
                      • 1970-01-01
                      • 2016-09-27
                      • 2017-12-14
                      • 2018-10-15
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多