通过填充元素将数组拆分为大小为 K 的子数组
给定一个大小为N的数组nums[ ] ,任务是使用以下过程将数组分成大小为K的组:
- 第一组由数组的前K个元素组成,第二组由数组的下K个元素组成,依此类推。每个元素都可以恰好是一个组的一部分。
- 对于最后一组,如果数组没有剩余K个元素,则使用 0完成该组。
例子:
Input: nums[ ] = {1,2,3,4,5,6,7,8}, K = 4
Output: [[1, 2, 3, 4] [ 5, 6, 7, 8]]
Explanation:
The first 4 element [1, 2, 3, 4] form the first group.
The next 4 elements [ 5, 6, 7, 8] form the second group.
Since all groups can be completely filled by element from the array, no need to use 0.
Input: nums[ ] = {3,2,5,7,9,1,3,5,7}, K = 2
Output: [[3, 2] ,[5, 7], [9,1], [3, 5], [7, 0]]
Explanation: The last group was one short of being of size 2. So, one 0 is used.
方法:这是一个与实现相关的简单问题。请按照以下步骤解决问题:
- 维护一个代表字符串中每个组的临时向量。
- 如果索引i+1可被K整除,则可以得出结论,组已以第 i 个索引结束。
- 如果组结束,则将temp推入ans向量。
- 检查最后一组是否有大小K。
- 如果不相等,则添加K – (len+1)大小的填充,并使用 0。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to split the array
vector > divideArray(int nums[],
int K, int N)
{
vector > ans;
vector temp;
for (int i = 0; i < N; i++) {
temp.push_back(nums[i]);
if(((i+1)%K)==0) {
ans.push_back(temp);
temp.clear();
}
}
// If last group doesn't have enough
// elements then add 0 to it
if (!temp.empty()) {
int a = temp.size();
while (a != K) {
temp.push_back(0);
a++;
}
ans.push_back(temp);
}
return ans;
}
// Function to print answer
void printArray(vector >& a)
{
int n = a.size();
cout << n;
for (int i = 0; i < n; i++) {
cout << "[ ";
for (int j = 0; j < a[i].size(); j++)
cout << a[i][j] << " ";
cout << "]";
}
}
// Driver Code
int main()
{
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = sizeof(nums) / sizeof(nums[0]);
int K = 4;
vector > ans
= divideArray(nums, K, N);
printArray(ans);
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
class GFG {
// Function to split the array
static ArrayList> divideArray(int nums[], int K, int N) {
ArrayList> ans = new ArrayList>();
ArrayList temp = new ArrayList();
for (int i = 0; i < N; i++) {
temp.add(nums[i]);
if (((i + 1) % K) == 0) {
ans.add(temp);
temp = new ArrayList();
}
}
// If last group doesn't have enough
// elements then add 0 to it
if (temp.size() != 0) {
int a = temp.size();
while (a != K) {
temp.add(0);
a++;
}
ans.add(temp);
}
return ans;
}
// Function to print answer
static void printArray(ArrayList> a) {
int n = a.size();
for (int i = 0; i < n; i++) {
System.out.print("[ ");
for (int j = 0; j < a.get(i).size(); j++)
System.out.print(a.get(i).get(j) + " ");
System.out.print("]");
}
}
// Driver Code
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = nums.length;
int K = 4;
ArrayList> ans = divideArray(nums, K, N);
printArray(ans);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python3 program for the above approach
# Function to split the array
def divideArray(nums, K, N):
ans = []
temp = []
for i in range(0, N):
temp.append(nums[i])
if(((i+1) % K) == 0):
ans.append(temp.copy())
temp.clear()
# If last group doesn't have enough
# elements then add 0 to it
if (len(temp) != 0):
a = len(temp)
while (a != K):
temp.append(0)
a += 1
ans.append(temp)
return ans
# Function to print answer
def printArray(a):
n = len(a)
for i in range(0, n):
print("[ ", end="")
for j in range(0, len(a[i])):
print(a[i][j], end=" ")
print("]", end="")
# Driver Code
if __name__ == "__main__":
nums = [1, 2, 3, 4, 5, 6, 7, 8]
N = len(nums)
K = 4
ans = divideArray(nums, K, N)
printArray(ans)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to split the array
static List > divideArray(int[] nums, int K,
int N)
{
List > ans = new List >();
;
List temp = new List();
for (int i = 0; i < N; i++) {
temp.Add(nums[i]);
if (((i + 1) % K) == 0) {
ans.Add(temp);
temp = new List();
}
}
// If last group doesn't have enough
// elements then add 0 to it
if (temp.Count != 0) {
int a = temp.Count;
while (a != K) {
temp.Add(0);
a++;
}
ans.Add(temp);
}
return ans;
}
// Function to print answer
static void printArray(List > a)
{
int n = a.Count;
for (int i = 0; i < n; i++) {
Console.Write("[ ");
for (int j = 0; j < a[i].Count; j++)
Console.Write(a[i][j] + " ");
Console.Write("]");
}
}
// Driver Code
public static int Main()
{
int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = nums.Length;
int K = 4;
List > ans = divideArray(nums, K, N);
printArray(ans);
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
输出
[ 1 2 3 4 ][ 5 6 7 8 ]
时间复杂度: 在)
辅助空间: O(N)