# 一、今日知识点总结

1.指针的使用

2.字符串结尾为'\0'

3.判断奇偶还可以用if(x&1)

# 三、解题报告

1108. IP 地址无效化

``````char * defangIPaddr(char * address){
char *ret = (char *)malloc(1000 * sizeof(char));   // (1)
int returnSize = 0;
int i;
for(i = 0; address[i]; ++i) {                      // (2)
ret[ returnSize++ ] = '[';
ret[ returnSize++ ] = '.';
ret[ returnSize++ ] = ']';
}else {
}
}
ret[ returnSize ] = '\0';                          // (3)
return ret;
}``````

``````char* reverseLeftWords(char* s, int n){
int i;
int k=strlen(s);
char *ret=(char *)malloc((k+1)*sizeof(char));
for(i=0;i<=k-n-1;++i){
ret[i]=s[i+n];
}
for(i=0;i<=n-1;i++){
ret[k-n+i]=s[i];
}
ret[k]='\0';
return ret;
}``````

1480. 一维数组的动态和

``````/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* runningSum(int* nums, int numsSize, int* returnSize){
int *ret=(int *)malloc(sizeof(int)*numsSize);int i,j,sum;
for(i=0;i<=numsSize-1;++i){
sum=0;
for(j=0;j<=i;++j){
sum+=nums[j];
}
ret[i]=sum;
}
*returnSize=numsSize;
return ret;
}``````

1920. 基于排列构建数组

``````/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* buildArray(int* nums, int numsSize, int* returnSize){
int *ret=(int *)malloc(sizeof(int)*numsSize);int i;
for(i=0;i<=numsSize-1;++i){
ret[i]=nums[nums[i]];
}
*returnSize=numsSize;
return ret;
}``````

1929. 数组串联

``````/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getConcatenation(int* nums, int numsSize, int* returnSize){
int *ret=(int *)malloc(2*sizeof(int)*numsSize);int i;
for(i=0;i<=numsSize-1;++i){
ret[i+numsSize]=ret[i]=nums[i];
}
*returnSize=2*numsSize;
return ret;
}``````

1470. 重新排列数组

``````/**
* 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-1;++i){
if(i&1){//判断i是否为奇数
ret[i]=nums[n+i/2];//是奇数取y值
}else{
ret[i]=nums[i/2];
}
}
*returnSize=numsSize;
return ret;
}``````

# 四、总结

``````/**
* Note: The returned array must be malloced, assume caller calls free().  // (1)
*/
int *func(int *nums, int numsSize, int *returnSize) {                     // (2)
int *ret = (int *)malloc( sizeof(int) * xxx );                        // (3)
// TODO                                                               // (4)
*returnSize = xxx;                                                    // (5)
return ret;                                                           // (6)
}
``````

...全文
39 回复 点赞 打赏 收藏 举报

6.0w+

2022-01-15 19:55

QQ群：480072171