文章目录
今日知识总结
-
32位整型
int
占据4个字节,long long
占据8个字节,char
占用1个字节 -
指针:地址,最小地址:0,最大地址:0XFFFFFFFF
-
指针的定义
datatype *p
-
取地址
int *p = &x(取地址符取x的地址)
, 即变量->地址 -
数组的地址,数组的地址是连续的,所以第一个元素的地址可以用指针表示
-
解引用:通过地址获取值, p代表地址,
*p
代表a的值 -
内存申请:c中利用malloc申请内存,传入参数为字节数,返回值为申请到的内存总地址,是什么类型,就要强转成什么类型
int *p = (int *)malloc(sizeof(int) * n);
-
范式
int *func(int *nums, int numsSize, int * returnSize){ int *ret = (int *) malloc(sizeof(int) * xxx); *returnSize = xxx; return ret;
题目分析
-
-
题目链接:https://leetcode-cn.com/problems/shuffle-the-array/
-
思路:
- 与2取模,就可以知道是前半部分还是后半部分
- 再算是第几位就可以了
-
代码:
/** * 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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/concatenation-of-array/
-
思路:模拟就行
-
代码:
/** * 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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/build-array-from-permutation/
-
思路:根据题目模拟即可
-
代码:
/** * 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; }
-
-
-
题目代码:https://leetcode-cn.com/problems/running-sum-of-1d-array/
-
思路:枚举
-
代码:
/** * 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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
-
思路:答案数组从k处开始,一直到最后一个,在从第一个开始遍历到第k个
-
题目代码:
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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/defanging-an-ip-address/
-
思路:
-
代码:
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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
-
思路:
-
代码:
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; }
-
-
-
题目链接: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 *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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/
-
思路:先算出会出现的最大值,接着把数去掉零,放进数组里,这里要做-1的偏移
-
代码:
/** * 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; }
-
-
-
题目链接:https://leetcode-cn.com/problems/create-target-array-in-the-given-order/
-
思路:
-
代码:
/** * 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; }
-
今日收获:
- 字符串数组写结束符可以省去返回字符串长度
- 创建指针数组需要分配空间
- 指针数组如果要计数的话,需要先初始化