# [学习报告]《LeetCode零基础指南》（第五讲）指针

6 篇文章 0 订阅

1、重新排列数组

/**
* 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(numsSize*sizeof(int ));
for(int i=0;i<numsSize;++i){
if(i&1){
ret[i]=nums[n+i/2];
}else{
ret[i] = nums[(i+1)/2];
}
}
* returnSize = numsSize;
return ret;
}


2、数组串联

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


3、基于排列构建数组

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


4、一维数组的动态和

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


5、左旋字符串

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


6、IP地址无效化

char * defangIPaddr(char * address){
char *ret = (char *)malloc(sizeof(char )*1000);
int size=0;
ret[size++]='[';
ret[size++]='.';
ret[size++]=']';
}else{
}
}
ret[size++]='\0';
return ret;
}


7、替换空格

char* replaceSpace(char* s){
int i=0;
char *ans=(char*)malloc(sizeof(char)*strlen(s)*3+1);
while(*s){
if(*s==' '){
ans[i++]='%';
ans[i++]='2';
ans[i++]='0';
}else{
ans[i++]=*s;
}
s++;
}
ans[i]=0;
return ans;
}


8、有多少小于当前数字的数字

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


9、打印从1到最大的n位数

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


10、按既定顺序创建目标数组

/**
* 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 *ret = (int *)malloc(numsSize*sizeof(int ));
int idx,ins;
for(int i=0;i<numsSize;++i){
idx=index[i];
ins=nums[i];
for(int j=len;j>idx;--j){
ret[j]=ret[j-1];
}
ret[idx]=ins;
++len;
}
* returnSize = len;
return ret;
}


2021年11月26日

04-10 60
11-26 7128
03-17 59
03-21 49
04-06 778
03-18 473

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

먼지by

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。