【九日集训】第四天:指针

C语言的指针是一大难点,理解了也就对C的基础没什么问题了。
只需要记住一点,指针就是地址
使用指针创建变量数组等就是开辟一个新的内存空间,这里就需要用到malloc函数,这里是一个固定格式,记下即可

第一题

1470. 重新排列数组 - 力扣(LeetCode)

int* shuffle(int* nums, int numsSize, int n, int* returnSize){
    int * res = (int *) malloc( sizeof(int) * n * 2);
    *returnSize = numsSize;
    for(int i = 0; i < n; ++i) {
        res[i * 2] = nums[i];
    }
    for(int i = n; i < numsSize; ++i) {
        res[2 * (i - n) + 1] = nums[i];
    }
    return res;
}

第二题

1929. 数组串联 - 力扣(LeetCode)

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

第三题

1480. 一维数组的动态和 - 力扣(LeetCode)

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

}

第四题

LCR 182. 动态口令 - 力扣(LeetCode)

char* dynamicPassword(char* password, int target) {
    int n = strlen(password);
    char * res = (char *) malloc( sizeof(char) * (n + 1) );

    for(int i = 0; i < n; i++) {
        res[i] = password[(i + target) % n];
    }
    res[n] = '\0';
    return res;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子琦啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值