《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;
	char* str = malloc(strlen(address)+7);

	while (address[i])
	{
		if (address[i] == '.')
		{
			str[j] = '[';
			j++;
			str[j] = '.';
			j++;
			str[j] = ']';
			i++;
			j++;
       	}
		else
		{
			str[j] = address[i];
			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;
}

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

/**
 * 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;
}

9.打印从1到最大的N个数

/**
 * 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+

社区成员

学习「 算法 」的捷径就是 「 题海战略 」,社区由「 夜深人静写算法 」作者创建,三年ACM经验,校集训队队长,亚洲区域赛金牌,世界总决赛选手。社区提供系统的训练,答疑解惑,面试经验,大厂内推等机会
帖子事件
创建了帖子
2022-01-15 16:14
社区公告

QQ群:480072171

英雄算法交流 8 群