一 今日作业
1.
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
int* ans = (int*)malloc(sizeof(int)* numsSize);
int i,j = 0;
for (i = 0;i < n;i++)
{
ans[j] = nums[i];
ans[j+1] = nums[i + n];
j += 2;
}
*returnSize = numsSize;
return ans;
}
2.
int* getConcatenation(int* nums, int numsSize, int* returnSize){
int* ans = (int*)malloc(sizeof(int)* 2*numsSize);
int i;
for (i = 0;i < numsSize;i++)
{
ans[i + numsSize] = nums[i];
ans[i] = ans[i + numsSize];
}
*returnSize = 2 * numsSize;
return ans;
}
3.
int* buildArray(int* nums, int numsSize, int* returnSize){
int* ans = (int*)malloc(sizeof(int)* numsSize);
int i;
for (i = 0;i < numsSize;i++)
{
ans[i] = nums[nums[i]];
}
*returnSize = numsSize;
return ans;
}
4.
int* runningSum(int* nums, int numsSize, int* returnSize){
int* ans = (int*)malloc(sizeof(int)* numsSize);
int i;
ans[0] = nums[0];
for (i = 1;i < numsSize;i++)
{
ans[i] = nums[i] + ans[i - 1];
}
*returnSize = numsSize;
return ans;
}
5.
char* reverseLeftWords(char* s, int n){
char *s1 = (char *)malloc(sizeof(char)*(strlen(s) + 1));
int j = 0;
for (int i = n; i < strlen(s); i ++) {
s1[j ++] = s[i];
}
for (int k = 0; k < n; k++ ) {
s1[j ++] = s[k];
}
s1[j] = '\0';
return s1;
}