64,162
社区成员




一、解题报告
1.
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。
请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
int *ret = (int *)malloc(sizeof(int)*numsSize);
int i;
for (i=0;i<numsSize;++i){
if (i&1){//奇数为1,偶数为0
ret[i] = nums[n + i/2];
}else {
ret[i] = nums[(i+1)/2];
}
}
*returnSize = numsSize;
return ret;//返回地址而不是数组的第一个元素
}
2.
给你一个长度为 n 的整数数组 nums 。请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < n 的 i ,满足下述所有要求:
ans[i] == nums[i]
ans[i + n] == nums[i]
具体而言,ans 由两个 nums 数组 串联 形成。
返回数组 ans 。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getConcatenation(int* nums, int numsSize, int* returnSize){
int *ans = (int *)malloc(2 * numsSize * sizeof(int));
int i;
for (i=0;i<numsSize;++i){
ans[i] = nums[i];
ans[i+numsSize] = nums[i];
}
*returnSize = 2*numsSize;
return ans;
}
3.
给你一个 从 0 开始的排列 nums(下标也从 0 开始)。请你构建一个 同样长度 的数组 ans ,其中,对于每个 i(0 <= i < nums.length),都满足 ans[i] = nums[nums[i]] 。返回构建好的数组 ans 。
从 0 开始的排列 nums 是一个由 0 到 nums.length - 1(0 和 nums.length - 1 也包含在内)的不同整数组成的数组。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
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.
给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。
请返回 nums 的动态和。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* runningSum(int* nums, int numsSize, int* returnSize){
int *sums = (int *)malloc(sizeof(int)*numsSize);
int i;
for (i=0;i<numsSize;++i){
sums[i] = nums[i];
if(i){
sums[i] +=sums[i-1];
}
}
*returnSize = numsSize;
return sums;
}
5.
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
char* reverseLeftWords(char* s, int n){
int len = strlen(s);
int i;
char *ans = (char *)malloc(sizeof(char) * (len +1));
for (i=0;i<len;++i){
ans[i] = s[(i+n)%len];
}
ans[len] = '\0';
return ans;
}
6.
给你一个有效的 IPv4 地址 address
,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 "[.]"
代替了每个 "."
。
char * defangIPaddr(char * address){
char *ans = (char *)malloc(sizeof(char)*1000);
int i;
int returnSize = 0;
for (i=0;address[i];++i){//address非零执行循环
if (address[i] == '.'){//循环中i始终变化,不能用i计数
ans[returnSize++] = '[';
ans[returnSize++] = '.';
ans[returnSize++] = ']';
}
else {
ans[returnSize++] = address[i];
}
}
ans [returnSize] = '\0';
return ans;
}
7.请实现一个函数,把字符串 s
中的每个空格替换成"%20"。
char* replaceSpace(char* s){
char *ans = (char *)malloc(sizeof(char)*30001);
int i,returnSize = 0;
for (i=0;s[i];++i){
if (s[i] == ' '){
ans[returnSize++] = '%%';//%字符需要双打,不然会认为需要替换
ans[returnSize++] = '2';
ans[returnSize++] = '0';
}
else {
ans[returnSize++] = s[i];
}
}
ans [returnSize] = '\0';
return ans;
}
8.
给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。
换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。
以数组形式返回答案。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
int *ans = (int *)malloc(sizeof(int)*numsSize);
int i,j;
for (i=0;i<numsSize;++i){
ans[i] = 0;
for (j=0;j<numsSize;++j){
if (nums[j]<nums[i]){
ans[i]++;
}
}
}
*returnSize = numsSize;
return ans;
}
9.输入数字 n
,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* printNumbers(int n, int* returnSize){
int i,j=1;
for (i=0;i<n;i++){
j *=10;
}
--j;
*returnSize = j;
int *ans = (int *)malloc(sizeof(int )*j);
for (i=0;i<j;++i){
ans[i] = i+1;
}
return ans;
}
10.
给你两个整数数组 nums 和 index。你需要按照以下规则创建目标数组:
目标数组 target 最初为空。
按从左到右的顺序依次读取 nums[i] 和 index[i],在 target 数组中的下标 index[i] 处插入值 nums[i] 。
重复上一步,直到在 nums 和 index 中都没有要读取的元素。
请你返回目标数组。
题目保证数字插入位置总是存在。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize){
int len = 0;
int i,j,ins,idx;
int *ans = (int *)malloc(sizeof(int)*numsSize);
for (i=0;i<numsSize;++i){
idx = index[i];//插入的数组下标位置
ins = nums[i];//插入的数
for(j=len;j>idx;--j){//先将插入位之后的元素后移1位
ans[j] = ans[j-1];
}
ans[idx] = ins;//再插入数字
++len;//计算数组长度
}
*returnSize = len;
return ans;
}
二、重点
字符数组尾有结束标志'\0',strlen函数计数时以此为结束标志,'\0'不计入总数
计算长度应初始化为零
返回地址而不是第一个元素
字符数组旋转应该用循环取模
顺序表先移位后插入,避免原数字被替换
好厉害!给你点个赞