gpt4 book ai didi

java - 将数组向后洗牌

转载 作者:行者123 更新时间:2023-12-02 03:43:00 24 4
gpt4 key购买 nike

基本上我有一个像这样的简单问题,但我很难弄清楚。我知道如何对数组进行洗牌,但不太确定如何让它专门洗牌到像后面这样的地方。

问题:

一个数组(已完全填充)被用作循环缓冲区。在 JAVA 中编写一个代码片段,将数组的所有元素向后移动,并将最后一个元素移动到旋转数组的前面。 (无输出)

示例 1:

int[] 数组 = new int[] {1, 2, 3};数组将变为 {3, 1, 2}

谢谢!

最佳答案

public int[] shiftLeft(int[] nums) {
if (nums == null || nums.length <= 1) {
return nums;
}
int start = nums[0];
System.arraycopy(nums, 1, nums, 0, nums.length - 1);
nums[nums.length - 1] = start;
return nums;
}

关于java - 将数组向后洗牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com