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.
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;
}
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
}
[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)
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;
}
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
}
[81 -2 81 11 3136 -12 9]
https://go.dev/play/p/8agy7VM8SuS
Arrays 101 Code from Java to Go(A Better Repeated Deletion Algorithm - Intro) (0) | 2022.03.06 |
---|---|
Arrays101 Replace Elements with Greatest Element on Right Side in GO (2) | 2022.03.05 |
Arrays101 Valid Mountain Array in GO (0) | 2022.03.04 |
Arrays101 Check If N and Its Double Exist in go (0) | 2022.03.02 |
Arrays 101 Code from Java to Go(Search in an Array) (0) | 2022.03.02 |
댓글 영역