简体   繁体   English

左右移动数组

[英]Move array to left and right

I am using java in eclipse.我在 eclipse 中使用 java。 I am trying to solve two problems.我正在尝试解决两个问题。 I know how to move an array to the right or left by one and fill in the last element with the first or the first with the last.我知道如何将数组向右或向左移动一个,并用第一个或第一个用最后一个填充最后一个元素。 I am now trying to move an array, say, three indices to the right OR three indices to the left.我现在正在尝试移动一个数组,例如向右移动三个索引或向左移动三个索引。 Is someone able to assist?有人可以提供帮助吗? I do not want to use modulus.我不想使用模数。

exmample, my array is {1,2,3,4,5} : and im rotating this to the left by three indexes if x is negative, so in this case: x = -3例如,我的数组是{1,2,3,4,5} :如果 x 为负,我将它向左旋转三个索引,所以在这种情况下: x = -3

if (x < 0) {
  for (i = length1 - 2, j = 0; j < (length1 - Math.abs(x)); i++, j++) {
        temp[i] = myArray[j];
  }
  for (i = 0, j = (length1 - Math.abs(x)); j < length1; i++, j++) {
        temp[i] = myArray[j];
  }
}

This will not run unless in my first for loop, I have: i = length1 - 2 .这不会运行,除非在我的第一个 for 循环中,我有: i = length1 - 2

Is there a more universal way to do this?有没有更通用的方法来做到这一点? What if I'm trying to rotate the number by 23 indexes, how would I go about doing that?如果我试图将数字旋转 23 个索引怎么办,我将如何 go 这样做?

input 1,2,3,4,5 output 4,5,1,2,3输入1,2,3,4,5 output 4,5,1,2,3

you can use a very simple algorithm to shift the positions of your array to the right or to the left:您可以使用一种非常简单的算法将数组的位置向右或向左移动:

You start with an empty array and iterate over your initial array, you skip N positions and then fill in order, adding what you skipped to the index and using % modulus to ensure that you get back to the begining once you reach the end您从一个空数组开始并迭代您的初始数组,跳过 N 个位置然后按顺序填充,将跳过的内容添加到索引中并使用 % 模数来确保到达结束后回到开始

If you want to allow negative numbers, you can also add the original array to ensure always positive numbers and bound the shift using modulus again, so the shift never gets too large, because shifting an array of three elements, three times is like not doing anything如果要允许负数,还可以添加原始数组以确保始终为正数并再次使用模数绑定移位,因此移位永远不会太大,因为将三个元素的数组移位三次就像不做任何事物

Here is some code example: (its actually javascript, to make it so you can run it here, but you get the idea and syntaxis is very similar这是一些代码示例:(它实际上是 javascript,以便您可以在这里运行它,但是您明白了这个想法和语法非常相似

 function moveBy(array, num){ num = num % array.length; // bounds the shift let result = new Array(array.length); // inits empty array for (let i = 0; i < array.length; i++) { let mappedIndex = (i + num + array.length) % array.length; result[i] = array[mappedIndex]; // skips num elements and fills everything using % to get back to the begining once reached the end } return result; } let example = [1,2,3,4,5]; console.log(moveBy(example, 1)); console.log(moveBy(example, 3)); console.log(moveBy(example, -2));

public void move(int[] nums, int k) {
    
    k = k % nums.length;
    
    // Reverse the orginal array.
    swap(nums, 0, nums.length - 1);
    
    // Reverse the left portion.
    swap(nums, 0, k - 1);
    
    // Reverse the right portion.
    swap(nums, k, nums.length - 1);
}    

private void swap(int[] nums, int l, int r) {
    while (l <= r) {
        
        int temp = nums[l];
        nums[l] = nums[r];
        nums[r] = temp;
        
        l++;
        r--;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM