# 《LeetCode零基础指南》(第四讲) 指针 【解题报告】

HSY1547867066 2022-01-15 16:14:49

1.重新排列数组

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

2.数组串联

``````/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getConcatenation(int* nums, int numsSize, int* returnSize){
int i;
*returnSize = numsSize * 2;
nums = (int *)realloc(nums, sizeof(int) * *returnSize);

for (i = 0; i < numsSize; i++) {
nums[i + numsSize] = nums[i];
}

return nums;
}``````

3.基于排列构建数组

``````/**
* 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;
*returnSize=numsSize;
for(i=0;i<numsSize;i++)
ans[i]=nums[nums[i]];

return ans;
}``````

4.一维数组的动态和

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

5.左旋字符串

``````char* reverseLeftWords(char* s, int n){
if(s == NULL || n == 0)
return s;
int len = strlen(s);
if(len == n)
return s;

char *ret = malloc(len + 1);
for(int i = 0; i < len; i++) {
ret[i] = s[(i+n)%len];
}
ret[len] = '\0';
return ret;
}``````

6.IP地址无效化

``````char * defangIPaddr(char * address){
int i = 0,j=0;

{
{
str[j] = '[';
j++;
str[j] = '.';
j++;
str[j] = ']';
i++;
j++;
}
else
{
i++;
j++;
}
}
str[j] = '\0';
return str;
}``````

7.替换空格

``````char* replaceSpace(char* s){
int i, j, len = strlen(s), cnt;
char *ans;

for (i = 0; i < len; i++) {
if (s[i] == ' ') {
cnt++;
}
}

ans = malloc(sizeof(char) * (len + cnt * 2 + 1));
j = 0;
for (i = 0; i < len; i++, j++) {
ans[j] = s[i];
if (s[i] == ' ') {
ans[j++] = '%';
ans[j++] = '2';
ans[j] = '0';
}
}

ans[j] = '\0';

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

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* ret = (int*)malloc(sizeof(int) * indexSize);
int tail = -1;
for (int i = 0; i < indexSize; ++i) {
++tail;
for (int j = tail; j > index[i]; --j) {
ret[j] = ret[j - 1];
}
ret[index[i]] = nums[i];
}
*returnSize = indexSize;
return ret;
}``````

...全文
12 回复 1 打赏 收藏 举报

6.0w+

2022-01-15 16:14

QQ群：480072171