《LeetCode零基础指南》(第五天) 指针

1. 重新排列数组

1470. 重新排列数组

  • 题目

给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。

请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shuffle-the-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 题目分析

遇到数组的问题我们可以将问题分化,即先将数组元素减少至一个到两个。

  • 这里我们将数组变成只有两个元素,观察可以发现x1的位置在偶数位,y1的位置在奇数位,此时我们不需要在移动。
  • 当元素增加至四个,发现依旧满足上述的条件,只不过我们需要将位置进行移动,即[x1, y1, x2, y2]。现在的问题就是如何将数组按指定位置排列,建立一个新的数组用来保存元素。难点就是怎么将元素按我们指定位置排列。
  • 这里我是参照别人的做法,先找插入的元素,然后借助奇偶位插入指定的位置,
  • 源码实现
/**
 * 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(sizeof(int) * numsSize);
    for(int i = 0; i < numsSize; ++i)
    {
        if(i & 1)	//奇数位
        {
            //取数组n位以后的元素
            ans[i] = nums[n + i / 2];
        }
        else	//偶数位
        {
            //取数组0到n-1位上的元素
            ans[i] = nums[(i + 1) / 2];
        }
    }
    return ans;
}

2. 数组串联

1929. 数组串联

  • 题目

给你一个长度为 n 的整数数组 nums 。请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < ni ,满足下述所有要求:

ans[i] == nums[i]
ans[i + n] == nums[i]
具体而言,ans 由两个 nums 数组 串联 形成。

返回数组 ans 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/concatenation-of-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 题目分析
  • 最简单的做法就是先循环遍历赋值一次,然后再从numsSize的位置遍历赋值一遍,最后返回数组和长度,
  • 第二种就只需要遍历一次,使用%运算符,对numsSize取余,当i == numsSize时数组又是从0开始遍历
  • 源码实现
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getConcatenation(int* nums, int numsSize, int* returnSize){
    //第一步,定义一个长度为2n的数组
    int *ans = (int *)malloc(sizeof(int) * (2 *numsSize));
    //第二步,给数组赋值
    for(int i = 0; i < numsSize; ++i) //从0开始赋值	
    {
        ans[i] = nums[i];
    }
    for(int i = 0; i < numsSize; ++i)	//从numsSize开始赋值
    {
        ans[i + numsSize] = nums[i];
    }

    *returnSize = numsSize * 2; 	//返回数组的长度
    return ans;
}

3. 基于排列构建的数组

1920. 基于排列构建数组

  • 题目

给你一个从 0 开始的排列 nums(下标也从 0 开始)。请你构建一个同样长度的数组 ans ,其中,对于每个 i0 <= i < nums.length),都满足 ans[i] = nums[nums[i]] 。返回构建好的数组 ans

**从 0 开始的排列 **nums 是一个由 0nums.length - 10nums.length - 1 也包含在内)的不同整数组成的数组。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/build-array-from-permutation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 题目分析

抓住条件ans[i] = nums[nums[i]]nums是一个由0到nums.length - 1。构建一个新的数组,按照上述的条件写出循环依次赋值。

  • 源码实现
/**
 * 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;
    for(i = 0; i < numsSize; ++i)
        ans[i] = nums[nums[i]];
    *returnSize = numsSize;
    return ans;
}

4. 一维数组的动态和

1480. 一维数组的动态和

  • 题目

给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i])

请返回 nums 的动态和。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/running-sum-of-1d-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 思路分析

当前元素的值等于前n个元素的值,直接使用循环依次将前n个元素的值求和赋值给当前元素。注意第一个元素不用求直接赋值。

  • 源码实现
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* runningSum(int* nums, int numsSize, int* returnSize){
    //将前i个数求和,将求和后的值赋值给当前下标
    int *ans = (int *)malloc(numsSize * sizeof(int));
    for(int i = 0; i < numsSize; ++i)
    {
        ans[i] = nums[i];
        if(i)	//第一个元素不需要求和
        {
            ans[i] += ans[i - 1]; 
        }
    }
    *returnSize = numsSize;
    return ans;
}

5. 左旋转字符串

剑指 Offer 58 - II. 左旋转字符串

  • 题目

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 题目分析
  • 根据函数的定义我们是有可能存在无法修改传入的字符串的情况,因为直接使用指针定义的字符串存在于只读字段,无法被修改。所以需要重新定义一个新数组
  • 通过取余运算,我们从指定的位置开始遍历字符串,然后依次将数组中的元素赋值给新数组。
  • 源码实现
char* reverseLeftWords(char* s, int n){
    //定义指针
    //将字符串赋值个新数组
    char *ans = (char *)malloc(strlen(s) + 1);
    int i = 0;
    int len = strlen(s);	//将计算的结果用变量保存能提高循环的效率
    while(i < len)
    {
        *(ans++) = s[(n + i++)%len];	//取余, 指定从哪个位置遍历整个数组
    }
    *ans = '\0';	//记住一定要加这条代码,否则不符合字符串的定义

    return ans - len;	//注意最后将指针指向字符串的首地址
}

6. IP地址无效化

1108. IP 地址无效化

  • 题目

给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。

所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."

  • 题目分析

找到.,使用[.]替换即可(代码参照的别人的做了一定的修改),

记住一点,创建新数组的时候记得保存数组的大小

  • 源码实现
char * defangIPaddr(char * address){
    char *ans = (char *)malloc(sizeof(char) *100);//内存定大一点
    int i;
    int returnSize = 0;
    int len = strlen(address);
    for(i = 0; address[i]; ++i) //'\0'实质上是0,可以使用它作为字符串结束循环的条件
    {
        if(address[i] == '.')
        {
            ans[returnSize++] = '[';
            ans[returnSize++] = '.';
            ans[returnSize++] = ']';
        }
        else
        {
            ans[returnSize++] = address[i];
        }
    }
    //新字符串数组一定要加上下面这句代码
    ans[returnSize] = '\0';
    return ans;
}

7. 替换空格

剑指 Offer 05. 替换空格

  • 题目

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

  • 题目分析

参照上题的思路

  • 源码实现
char* replaceSpace(char* s){
    char *ans = (char *)malloc(sizeof(char) * (10000 + 1));

    int i;
    int returnSize = 0; //初始化
    for(i = 0; s[i]; ++i)
    {
        if(s[i] == ' ')
        {
            ans[returnSize++] = '%';
            ans[returnSize++] = '2';
            ans[returnSize++] = '0';
        }
        else
        {
            ans[returnSize++] = s[i];
        }
    }
    ans[returnSize] = '\0';
    return ans; 
}

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

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

  • 题目

给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。

换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != inums[j] < nums[i]

以数组形式返回答案。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 题目分析

使用双层循环,外层循环给出要比较的数字,内层循环比较数组中除它以外的所有值。然后将结果保存至数组中

  • 源码实现
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
    int *ans = (int *)malloc(sizeof(int) * numsSize);
    *returnSize = numsSize;

    int i, j;
    for(i = 0; i < numsSize; ++i)
    {
        int cnt = 0;
        for(j = 0; j < numsSize; ++j)
        {
            if(nums[i] > nums[j])
            {
                cnt++;
            }
        }
        ans[i] = cnt;
    }

    return ans;
}

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

剑指 Offer 17. 打印从1到最大的n位数

  • 题目

输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。

  • 题目分析
  • 首先将此时的n转换成对应的位数。
  • 其次开辟对应的数组的大小,写一个从0开始,元素从1开始的循环依次打印从1到n的数字
  • 源码实现
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* printNumbers(int n, int* returnSize){
    int i;
    int numsSize = 1;
    for(i = 1; i <= n; ++i)	//需要打印的位数
    {
        numsSize *= 10;
    }
    int len = numsSize - 1;
    int *ans = (int *)malloc(sizeof(int) * len);
    for(i = 0; i < len; ++i)	//从1开始打印
    {
        ans[i] = i + 1;
    }

    *returnSize = len;
    return ans;
}

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

1389. 按既定顺序创建目标数组

  • 题目

给你两个整数数组 numsindex。你需要按照以下规则创建目标数组:

  • 目标数组 target 最初为空。
  • 按从左到右的顺序依次读取 nums[i]index[i],在 target 数组中的下标 index[i] 处插入值 nums[i]
  • 重复上一步,直到在 numsindex 中都没有要读取的元素。
    请你返回目标数组。

题目保证数字插入位置总是存在。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/create-target-array-in-the-given-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 题目分析
  • 首先明确index[i]是索引值,nums[i]是需要保存的元素的值,为了方便区分,我这里使用idx保存索引值,ins保存插入的值。
  • 然后根据索引值将指定的元素插入目标数组,最后返回统计的结果和数组的长度。
  • 源码实现
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize){
    int *target = (int *)malloc(sizeof(int) * numsSize);

    int len = 0;	//初始长度为0
    int i, j, idx, ins;
    for(i = 0; i < numsSize; ++i)	//插入指定的元素
    {
        idx = index[i];
        ins = nums[i];
        //插入一个新的元素,被插入位置的元素之后的所有元素往后移一位
        for(j = len; j > idx; --j)	
        {
            target[j] = target[j - 1]; 
        }
        ++len;
        target[idx] = ins;
    } 
    *returnSize = numsSize;
    return target;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值