Java HackerRank数组左旋转

huangapple go评论71阅读模式
英文:

Java Hackerank left array Rotation

问题

以下是翻译后的代码部分:

static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];  // 看这里,为什么这样可以工作

        while(logicBreak < a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        logicBreak = 0;
    
        while(logicBreak < d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        return copy;
    }

希望这能满足您的需求。

英文:

We have a method in which an array and number of rotations are passed as input and we have to return the array after left rotations. Here is my solution.

static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];  // SEE HERE GUYS, WHY THIS WORKS
        int[] copy = a;  // AND WHY NOT THIS,

        while(logicBreak &lt; a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        logicBreak = 0;
    
        while(logicBreak &lt; d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        return copy;
    }

I hope that there is no confusion until now and if you have then, open this page. So my problem is really basic. Why the answer works when I do this int[] copy = new int[a.length]; but does not work when I do this int[] copy = a;. Can you tell me the difference, because we are changing all the values of copy array, so what is the matter we make it new int[] or same as the array a. I have created an android app but I am still not getting this array concept, if there is a difference then please tell me.

For your ease here is the full code.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];

        while(logicBreak &lt; a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        logicBreak = 0;
    
        while(logicBreak &lt; d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        return copy;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv(&quot;OUTPUT_PATH&quot;)));

        String[] nd = scanner.nextLine().split(&quot; &quot;);

        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        String[] aItems = scanner.nextLine().split(&quot; &quot;);
        scanner.skip(&quot;(\r\n|[\n\r\u2028\u2029\u0085])?&quot;);

        for (int i = 0; i &lt; n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        int[] result = rotLeft(a, d);

        for (int i = 0; i &lt; result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));

            if (i != result.length - 1) {
                bufferedWriter.write(&quot; &quot;);
            }
        }

        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

答案1

得分: 2

当你执行以下操作时:

int[] copy = a;

你只是将 a 的引用赋值给了 copy。它们在内存中"引用"同一个位置。

以下是一个示例:

int[] a = {1,2,3};
int[] copy  = a;
a[1] = 100;
System.out.println(Arrays.toString(copy));

输出结果为:

[1,100,3]
英文:

When you do the following:

int[] copy = a;

You are just assigning the reference of a to copy. They "refer" to the same location in memory.

Here is a demo

int[] a = {1,2,3};
int[] copy  = a;
a[1] = 100;
System.out.println(Arrays.toString(copy));

Prints

[1,100,3]


</details>



huangapple
  • 本文由 发表于 2020年9月9日 22:48:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63814155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定