题解 | #旋转数组#

旋转数组

http://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042

package array;

/**

  • 移动数组

  • https://leetcode-cn.com/problems/rotate-array/

  • /
    public class RotateArray {

    public static void main(String[] args) {

      int[] array = new int[]{1, 2, 3, 4, 5, 6, 7};
    
      int[] result = rotateArray(array.length, 3, array);

    }

    /**

    • 旋转数组

    • @param n int整型 数组长度

    • @param m int整型 右移距离

    • @param a int整型一维数组 给定数组

    • @return int整型一维数组

    • /
      public static int[] rotateArray(int n, int m, int[] a) {

      //建立辅助数组存储
      int[] newArr = new int[n];
      for (int i = 0; i < n; ++i) {

        newArr[(i + m) % n] = a[i]; //理解这里

      }
      System.arraycopy(newArr, 0, a, 0, n);
      return newArr;
      }
      }

全部评论

相关推荐

12-11 14:24
门头沟学院 Java
在debug的伊泽瑞...:我说怎么这么眼熟查看图片
点赞 评论 收藏
分享
牛客74464229...:年底就这样,招人的要不是很缺人的,要不就是岗位要求高的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务