0
点赞
收藏
分享

微信扫一扫

基于SSM的网络音乐系统的设计与实现

在这里插入图片描述

目录

1.题目

给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。

示例 1:

输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]

示例 2:

输入:nums = [-1,-100,3,99], k = 2
输出:[3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]

提示:

  • 1 <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1
  • 0 <= k <= 105

进阶:

  • 尽可能想出更多的解决方案,至少有 三种 不同的方法可以解决这个问题。
  • 你可以使用空间复杂度为 O(1)原地 算法解决这个问题吗?

2.答案

class Solution {
    public void rotate(int[] nums, int k) {
        if (nums.length < 1) {
            return;
        }
        int times = k % nums.length;
        int[] tailValues = new int[k];
        System.arraycopy(nums, nums.length - times, tailValues, 0, times);
        System.arraycopy(nums, 0, nums, times, nums.length - times);
        System.arraycopy(tailValues, 0, nums, 0, times);
    }
}

3.提交结果截图

在这里插入图片描述

整理完毕,完结撒花~ 🌻

举报

相关推荐

C++音乐系统

0 条评论