《LeetCode零基础指南》(第四讲) 指针 【解题报告】
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;
}
/**
* 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;
}
/**
* 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;
}
相关推荐
leetcode第四题-LeetCode_Exercise:力扣_练习
leetcode 第四题力扣_练习 位掩码 结束了一些位操作和位掩码练习,难度由低到高总结了几道非常讲的位运算和位掩码题,难度由低到高 (简单的) (简单的) (强烈推荐) (中等的) (中)(强烈推荐) (中)(强烈推荐,即使你能想到每个元素出现4、5、6次而只有一个出现1次的问题,你怎么解决) (中)(强烈推荐) (难)(强烈推荐) 为什么我们需要使用Bitmask来处理一些棘手的问题? 原因1:位操作比'+-*/'等正常操作更快 原因2:位掩码可以帮助我们记录状态。 如果您使用 int 变量或布尔变量来存储状态,它将花费 4 个字节,因此您使用位 0/1 来存储状态,这将节省大量空间。 一些位掩码的操作: 1.记录状态 如果您有 8 个状态,则可以将位掩码设为 00000000,并使用“或 |” 操作以保存您的状态。 例如。 位掩码 = 00000000 new_state1 = 00001000 位掩码 | new_state1 = 00001000 这意味着已经记录了第四个状态 new_state2 = 00010001 位掩码 | new_state2 = 00011001
《LeetCode零基础指南》(第四讲) 指针
冲破指针的恐惧!
《LeetCode零基础指南》(第八讲) 二级指针
遇事不决就枚举
《LeetCode零基础指南》(第十一讲) 遇到不会的题怎么办?
遇到不会的题千万不要放弃,要找方法突破
leetcode:力扣刷题
密码 力扣刷题笔记 版本历史 0.1.0 关于作者 野子- 电子邮件:


