第四天《指针》

文章目录

今日知识总结

  1. 32位整型 int 占据4个字节,long long 占据8个字节,char占用1个字节

  2. 指针:地址,最小地址:0,最大地址:0XFFFFFFFF

  3. 指针的定义 datatype *p

  4. 取地址 int *p = &x(取地址符取x的地址), 即变量->地址

  5. 数组的地址,数组的地址是连续的,所以第一个元素的地址可以用指针表示

  6. 解引用:通过地址获取值, p代表地址,*p代表a的值

  7. 内存申请:c中利用malloc申请内存,传入参数为字节数,返回值为申请到的内存总地址,是什么类型,就要强转成什么类型

    int *p = (int *)malloc(sizeof(int) * n);
    
  8. 范式

    int *func(int *nums, int numsSize, int * returnSize){
        int *ret = (int *) malloc(sizeof(int) * xxx);
        
        *returnSize = xxx;
        return ret;
        
    

题目分析

  1. 1470. 重新排列数组

    1. 题目链接:https://leetcode-cn.com/problems/shuffle-the-array/

    2. 思路:

      • 与2取模,就可以知道是前半部分还是后半部分
      • 再算是第几位就可以了
    3. 代码:

      /**
       * 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(sizeof(int) * numsSize);
              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. 1929. 数组串联

    1. 题目链接:https://leetcode-cn.com/problems/concatenation-of-array/

    2. 思路:模拟就行

    3. 代码:

      /**
       * Note: The returned array must be malloced, assume caller calls free().
       */
      int* getConcatenation(int* nums, int numsSize, int* returnSize){
          int * res = (int *)malloc(sizeof(int) * numsSize * 2);
          for(int i = 0; i < numsSize; i++){
              res[i] = nums[i];
              res[i + numsSize] = nums[i];
          }
          *returnSize = 2 * numsSize;
          return res;
      
      }
      
  3. 1920. 基于排列构建数组

    1. 题目链接:https://leetcode-cn.com/problems/build-array-from-permutation/

    2. 思路:根据题目模拟即可

    3. 代码:

      /**
       * Note: The returned array must be malloced, assume caller calls free().
       */
      int* buildArray(int* nums, int numsSize, int* returnSize){
          int *ret = (int *) malloc(sizeof(int) * numsSize);
          for(int i = 0; i < numsSize; i++){
              ret[i] = nums[nums[i]];
          }
          *returnSize = numsSize;
          return ret;
      }
      
  4. 1480. 一维数组的动态和

    1. 题目代码:https://leetcode-cn.com/problems/running-sum-of-1d-array/

    2. 思路:枚举

    3. 代码:

      /**
       * Note: The returned array must be malloced, assume caller calls free().
       */
      int* runningSum(int* nums, int numsSize, int* returnSize){
              int *ret = (int *)malloc(sizeof(int) * numsSize);
              
              for(int i = 0; i < numsSize; i++){
                  int mid = i;
                  int sum = 0;
                  printf("%d", mid);
                  while(mid >= 0){
                      sum += nums[mid];
                      mid--;
                  }
                  
                  ret[i] = sum;
              }
              *returnSize = numsSize;
              return ret;
              
          }
      
      
      
  5. 剑指 Offer 58 - II. 左旋转字符串

    1. 题目链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/

    2. 思路:答案数组从k处开始,一直到最后一个,在从第一个开始遍历到第k个

    3. 题目代码:

      char* reverseLeftWords(char* s, int n){
          int len = strlen(s);
          int cnt = 0;
          char *res = (char*) malloc((len + 1) * sizeof(char));
          for(int i = n; i < len; i++){
              res[cnt] = s[i];
              cnt++;
          }
          for(int i = 0; i < n; i++){
              res[cnt] = s[i];
              cnt++;
          }
          res[len] = '\0';//要记得加上结束符号,不然需要返回数组长度
      
          return res;
      
      
      }
      
  6. 1108. IP 地址无效化

    1. 题目链接:https://leetcode-cn.com/problems/defanging-an-ip-address/

    2. 思路:

    3. 代码:

      char * defangIPaddr(char * address){
          int len = strlen(address);
          char *res = (char*)malloc(1000 * sizeof(char));
          int cnt = 0;
      
          for(int i = 0; i < len; i++){
              if(address[i] == '.'){
                  res[cnt++] = '[';
                  res[cnt++] = address[i];
                  res[cnt++] = ']';
              }
              else {
                  res[cnt++] = address[i];
              }
          }
          res[cnt] = '\0';
          return res;
      
      }
      
  7. 剑指 Offer 05. 替换空格

    1. 题目链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/

    2. 思路:

    3. 代码:

      char* replaceSpace(char* s){
          int len = strlen(s);
          int cnt = 0;
          char *res = (char*)malloc(30000 * sizeof(char));
          
          for(int i = 0; i < len; i++){
              if(s[i] == ' '){
                  res[cnt++] = '%';
                  res[cnt++] = '2';
                  res[cnt++] = '0';
              }
              else {
                  res[cnt++] = s[i];
              }
          }
          res[cnt] = '\0';
          return res;
      
      }
      
  8. 1365. 有多少小于当前数字的数字

    1. 题目链接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/

    2. 思路:循环这个数组两次,第一次确定比较对象,第二次确定比较结果

    3. 代码:

      /**
       * Note: The returned array must be malloced, assume caller calls free().
       */
      int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
          int *ret =(int *)malloc(sizeof(int) * numsSize);
          for(int i = 0; i < numsSize; i++){
              ret[i] = 0;
          }
          for(int i = 0; i < numsSize; i++){
              for(int j = 0; j < numsSize; j++){
                  if(nums[j] < nums[i]){
                      ret[i]++;
                  }
              }
          } 
          *returnSize = numsSize;
          return ret;
      
      }
      
  9. 剑指 Offer 17. 打印从1到最大的n位数

    1. 题目链接:https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/

    2. 思路:先算出会出现的最大值,接着把数去掉零,放进数组里,这里要做-1的偏移

    3. 代码:

      /**
       * Note: The returned array must be malloced, assume caller calls free().
       */
      int* printNumbers(int n, int* returnSize){
          int i;
          int f = 1;
          int *ret;
      
          for(int i = 0; i < n; i++){
              f *= 10;
          }
          --f;
          *returnSize = f;
          ret = (int *)malloc(sizeof(int) * f);
          for(int i = 1; i <= f; i++){
              ret[i - 1] = i;
          }
      
          return ret;
      
      }
      
  10. 1389. 按既定顺序创建目标数组

    1. 题目链接:https://leetcode-cn.com/problems/create-target-array-in-the-given-order/

    2. 思路:

    3. 代码:

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

今日收获:

  1. 字符串数组写结束符可以省去返回字符串长度
  2. 创建指针数组需要分配空间
  3. 指针数组如果要计数的话,需要先初始化

今日疑问:

资料链接

博客链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值