상세 컨텐츠

본문 제목

Arrays 101 Code from Java to Go(In-Place Array Operations Introduction)

Go/Leet Code

by Gopythor 2022. 3. 5. 18:52

본문

728x90
반응형

What are in-place array operations?

  • Given an Array of integers, return an Array where every element at an even-indexed position is squared.

Input: array = [9, -2, -9, 11, 56, -12, -3]
Output: [81, -2, 81, 11, 3136, -12, 9]
Explanation: The numbers at even indexes (0, 2, 4, 6) have been squared,
whereas the numbers at odd indexes (1, 3, 5) have been left the same.

Not in-place Code

Java

public int[] squareEven(int[] array, int length) {

  // Check for edge cases.
  if (array == null) {
    return null;
  }

  // Create a resultant Array which would hold the result.
  int result[] = new int[length];

  // Iterate through the original Array.
  for(int i = 0; i < length; i++) {

    // Get the element from slot i of the input Array.
    int element = array[i];

    // If the index is an even number, then we need to square element.
    if (i % 2 == 0) {
      element *= element;
    }

    // Write element into the result Array.
    result[i] = element;
  }

  // Return the result Array.
  return result;
}

Go

func squareEven(array []int, length int) []int {

    // Check for edge cases.
    if array == nil {
        return nil
    }
    // Create a resultant Array which would hold the result.
    result := make([]int, length)

    // Iterate through the original Array.
    for i := 0; i < length; i++ {

        // Get the element from slot i of the input Array.
        element := array[i]

        // If the index is an even number, then we need to square element.
        if i%2 == 0 {
            element *= element
        }
        // Write element into the result Array.
        result[i] = element

    }
    // Return the result Array
    return result
}

reuslt

[81 -2 81 11 3136 -12 9]

https://go.dev/play/p/QLxl1OOLyVs

[Go Playground - The Go Programming Language

About the Playground The Go Playground is a web service that runs on go.dev's servers. The service receives a Go program, vets, compiles, links, and runs the program inside a sandbox, then returns the output. If the program contains tests or examples and n

go.dev](https://go.dev/play/p/QLxl1OOLyVs)

&nbsp;An&nbsp; inefficient &nbsp;way of solving the problem

In-place Code

Java

public int[] squareEven(int[] array, int length) {

  // Check for edge cases.
  if (array == null) {
    return array;
  }

  // Iterate through the original array.
  for(int i = 0; i < length; i++) {

    // If the index is an even number, then we need to square the element
    // and replace the original value in the Array.
    if (i % 2 == 0) {
      array[i] *= array[i];
    }
    // Notice how we don't need to do *anything* for the odd indexes? :-)
  }

  // We just return the original array. Some problems on leetcode require you
  // to return it, and other's dont.
  return array;
}

Go

func squareEven(array []int, length int) []int {

    // Check for edge cases.
    if array == nil {
        return nil
    }

    // Iterate through the original Array.
    for i := 0; i < length; i++ {

        // If the index is an even number, then we need to square the element
        // and replace the original value in the Array.
        if i%2 == 0 {
            array[i] *= array[i]
        }
        // Notice how we don't need to do *anything* for the odd indexes? :-)
    }
    // We just return the original array. Some problems on leetcode require you
    // to return it, and other's dont.
    return array
}

reuslt

[81 -2 81 11 3136 -12 9]

https://go.dev/play/p/8agy7VM8SuS

 

Go Playground - The Go Programming Language

About the Playground The Go Playground is a web service that runs on go.dev's servers. The service receives a Go program, vets, compiles, links, and runs the program inside a sandbox, then returns the output. If the program contains tests or examples and n

go.dev

 

728x90
반응형

관련글 더보기

댓글 영역