Java 如何将数组向右旋转
我已经编写了一个程序来向左移动int数组,但是找不到一种方法来向右移动它。如果您对如何基于空格数(intx)将数组向右“旋转”有任何想法,请查看我的代码并发表评论,因为它目前只向左移动。谢谢Java 如何将数组向右旋转,java,arrays,rotation,Java,Arrays,Rotation,我已经编写了一个程序来向左移动int数组,但是找不到一种方法来向右移动它。如果您对如何基于空格数(intx)将数组向右“旋转”有任何想法,请查看我的代码并发表评论,因为它目前只向左移动。谢谢 public void makeRight(int x) { int[] anArray = {0, 1, 2, 3, 4, 5}; int counter = 0; while (counter < x) { int temp = anArray[0];
public void makeRight(int x) {
int[] anArray = {0, 1, 2, 3, 4, 5};
int counter = 0;
while (counter < x) {
int temp = anArray[0];
for (int i = 0; i < anArray.length - 1; i++) {
anArray[i] = anArray[i + 1];
}
anArray[anArray.length - 1] = temp;
counter++;
}
for (int i = 0; i < anArray.length; i++){
System.out.print(anArray[i] + " ");
}
}
public void makeRight(int x){
int[]anArray={0,1,2,3,4,5};
int计数器=0;
while(计数器
只需像这样更改代码
public void makeRight(int x) {
int[] anArray = {0, 1, 2, 3, 4, 5};
int counter = 0;
while(counter< x){
int temp = anArray[anArray.length - 1];
for (int i = anArray.length - 1; i > 0; i--) {
anArray[i] = anArray[i - 1];
}
anArray[0] = temp;
counter++;
}
for (int i = 0; i < anArray.length; i++)
System.out.print(anArray[i] + " ");
}
int[] anArray = {0, 1, 2, 3, 4, 5};
//int counter = 0;
//int x = 2;
int[] secondArray = new int[anArray.length];
for (int i = 0; i < anArray.length; i++) {
secondArray[(i + x) % anArray.length] = anArray[i];
}
for (int i = 0; i < secondArray.length; i++){
System.out.print(secondArray[i] + " ");
}
public void makeRight(int x){
int[]anArray={0,1,2,3,4,5};
int计数器=0;
while(计数器0;i--){
anArray[i]=anArray[i-1];
}
anArray[0]=温度;
计数器++;
}
for(int i=0;i
while(计数器0;i--){
anArray[i]=anArray[i-1];
}
anArray[0]=温度;
计数器++;
}
将阵列向右旋转
public void makeRight( int x )
{
int[] anArray =
{ 0, 1, 2, 3, 4, 5 };
int counter = 0;
while ( counter < x )
{
int temp = anArray[anArray.length - 1];
for ( int i = anArray.length - 1; i > 0; i-- )
{
anArray[i] = anArray[i - 1];
}
anArray[0] = temp;
counter++;
}
for ( int i = 0; i < anArray.length; i++ )
{
System.out.print( anArray[i] + " " );
}
}
public void makeRight(int x)
{
int[]无误=
{ 0, 1, 2, 3, 4, 5 };
int计数器=0;
while(计数器0;i--)
{
anArray[i]=anArray[i-1];
}
anArray[0]=温度;
计数器++;
}
for(int i=0;i
在我看来,基本上你已经在大部分部分完成了旋转阵列(右)。
这就是
anArray[i]=secondArray[(i+x)%anArray.length]代码>
及
anArray[(i+x)%anArray.length]=secondArray[i]代码>
有点不同
会有类似的事情发生
public void makeRight(int x) {
int[] anArray = {0, 1, 2, 3, 4, 5};
int counter = 0;
while(counter< x){
int temp = anArray[anArray.length - 1];
for (int i = anArray.length - 1; i > 0; i--) {
anArray[i] = anArray[i - 1];
}
anArray[0] = temp;
counter++;
}
for (int i = 0; i < anArray.length; i++)
System.out.print(anArray[i] + " ");
}
int[] anArray = {0, 1, 2, 3, 4, 5};
//int counter = 0;
//int x = 2;
int[] secondArray = new int[anArray.length];
for (int i = 0; i < anArray.length; i++) {
secondArray[(i + x) % anArray.length] = anArray[i];
}
for (int i = 0; i < secondArray.length; i++){
System.out.print(secondArray[i] + " ");
}
int[]anArray={0,1,2,3,4,5};
//int计数器=0;
//int x=2;
int[]secondArray=new int[anArray.length];
for(int i=0;i
至于“%”是如何工作的,这个链接应该有一个清晰的解释。类似的东西应该可以工作
private void shiftArrayRight() {
int endElementvalue = element[element - 1];
int[] startElements = Arrays.copyOfRange(element, 0 , element.length - 1);
element[0] = endElementvalue;
for(int i = 0, x = 1; i < startElements.length; i++, x++) {
element[x] = startElements[i];
}
System.out.println(Arrays.toString(element);
}
private void shiftArrayRight(){
int endElementvalue=元素[element-1];
int[]startElements=Arrays.copyOfRange(元素,0,元素.length-1);
元素[0]=endElementvalue;
对于(int i=0,x=1;i
下面的函数可以帮助您
public static void rightRotateArray(int[] a, int requiredIterations) {
// right-rotate [a] by k moves
// totalActiveIterations by MOD
// => because every n(a.length) rotations ==> we receive the same array
int totalActiveIterations = requiredIterations % a.length;
for (int i = 0; i < totalActiveIterations; i++) {
// make lastElement as BKP temp
int temp = a[a.length - 1];
// make other elements => each one equal previous one [starting by lastElement]
for (int j = (a.length - 1); j >= 1; j--) {
a[j] = a[j - 1];
}
// make 1stElement equal to (BKP as temp = lastElement)
a[0] = temp;
}
}
公共静态void rightrotaray(int[]a,int requiredictions){
//向右旋转[a]k步
//按MOD计算的总迭代次数
//=>因为每n(a.length)个旋转==>我们接收相同的数组
int totalActiveIterations=requiredIterations%a.length;
对于(int i=0;i每个元素等于前一个元素[从lastElement开始]
对于(int j=(a.length-1);j>=1;j--){
a[j]=a[j-1];
}
//使1温度等于(BKP作为温度=最后一个元素)
a[0]=温度;
}
}
其他答案只是代码转储,没有任何解释。我想出了一个算法:
我们将数组旋转到位。观察每个元素的目标位置由(index+k)给出模大小
。对于范围0
到k-1
,只要目标位置大于当前位置,我们就递归地将每个元素与其目标位置中的元素交换。这是因为我们从较低的索引递增到较高的索引,较小的目标索引表示相应的我已经换了
Example:
Rotate [1, 2, 3, 4, 5, 6] by 3
Index to target index:
0 to 3
1 to 4
2 to 5
3 to 0
4 to 1
5 to 2
swap(0, 3) => [4, 2, 3, 1, 5, 6]
swap(0, 0) => return
swap(1, 4) => [4, 5, 3, 1, 2, 6]
swap(1, 1) => return
swap(2, 5) => [4, 2, 6, 1, 2, 3]
swap(2, 2) => return
Done!
Another example:
Rotate [2, 3, 4, 1] by 1
Index to target index:
0 to 1
1 to 2
2 to 3
3 to 0
swap(0, 1) => [3, 2, 4, 1]
swap(0, 2) => [4, 2, 3, 1]
swap(0, 3) => [1, 2, 3, 4]
swap(3, 0) => return
Done!
代码:
static void rotateRight(int[] xs, int k) {
swap(0, 0, xs, k);
}
private static void swap(int original, int current, int[] xs, int k) {
int target = (original + k) % xs.length;
if (target > current) {
int tmp = xs[current];
xs[current] = xs[target];
xs[target] = tmp;
swap(target, current, xs, k);
}
}
公共静态列表rotateLeft(int d,List arr)
{
int listSize=arr.Count();
int[]newArr=newint[listSize];
对于(int-oldIndex=0;oldIndex
它当前将其移动到正确的位置,有什么问题吗?请您解释一下您的代码,以及问题中的代码错误的原因?