1470. Shuffle the Array
선택한 UI 언어에 맞게 문제 텍스트를 러시아어에서 번역합니다. 코드는 변경하지 않습니다.
Дан 배열 nums, состоящий из 2n elementов в форме [x1, x2, ..., xn, y1, y2, ..., yn].
return 배열 в форме [x1, y1, x2, y2, ..., xn, yn].
예제:
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
C# 해법
매칭됨/원본public class Solution {
public int[] Shuffle(int[] nums, int n) {
int[] result = new int[2 * n];
for (int i = 0; i < n; ++i) {
result[2 * i] = nums[i];
result[2 * i + 1] = nums[n + i];
}
return result;
}
}
C++ 해법
자동 초안, 제출 전 검토#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public vector<int>& Shuffle(vector<int>& nums, int n) {
vector<int>& result = new int[2 * n];
for (int i = 0; i < n; ++i) {
result[2 * i] = nums[i];
result[2 * i + 1] = nums[n + i];
}
return result;
}
}
Java 해법
매칭됨/원본class Solution {
public int[] shuffle(int[] nums, int n) {
int[] result = new int[2 * n];
for (int i = 0; i < n; ++i) {
result[2 * i] = nums[i];
result[2 * i + 1] = nums[n + i];
}
return result;
}
}
Go 해법
매칭됨/원본func shuffle(nums []int, n int) []int {
result := make([]int, 2*n)
for i := 0; i < n; i++ {
result[2*i] = nums[i]
result[2*i+1] = nums[n+i]
}
return result
}
Algorithm
Создайте 배열 result размером 2 * n.
Итеративно пройдите по 배열у nums от 0 до n - 1:
Сохраните element xi+1, то есть nums[i], в индекс 2 * i 배열а result.
Сохраните element yi+1, то есть nums[i + n], в индекс 2 * i + 1 배열а result.
return 배열 result.
😎
Vacancies for this task
활성 채용 with overlapping task tags are 표시됨.
아직 활성 채용이 없습니다.