英雄集训DAY4

科牛

关注

阅读 68

2022-04-20

## 2020/4/19

### 题目描述:

> [1470. 重新排列数组 - 力扣(LeetCode) (leetcode-cn.com)](https://leetcode-cn.com/problems/shuffle-the-array/comments/)

```c
1470. 重新排列数组
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。
请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组
```

#### 我的题解:

```c
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
    int i;
//创建数组
    int*tmp=(int*)malloc(sizeof(int)*numsSize);
    for(i=0;i<numsSize;i++){
    if(1&i){
    //判断下标为奇数或者偶数
    //若为1则说明为奇数,则需要把y放入到前面
    tmp[i]=nums[n+i/2];
    }else{
        //为偶数(0为特殊的偶数),也就是说把原来的数组的下标为1的数要放到分配的下表为2处
        //0->0 1->2 2->4 3->6
        tmp[i]=nums[i/2];
    }

    }
    *returnSize=numsSize;
    return tmp;
    
}
```

#### 思路:

```c
//看注释就行。。
```

### 题目描述:

> [1929. 数组串联 - 力扣(LeetCode) (leetcode-cn.com)](https://leetcode-cn.com/problems/concatenation-of-array/)

```
数组串联:
给你一个长度为 n 的整数数组 nums 。请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < n 的 i ,满足下述所有要求:
ans[i] == nums[i]
ans[i + n] == nums[i]
具体而言,ans 由两个 nums 数组 串联 形成。
返回数组 ans 。
```

#### 我的题解:

```c
int* getConcatenation(int* nums, int numsSize, int* returnSize){
    int i;
    int*ret=(int*)malloc(sizeof(int)*2*numsSize);
    for(i=0;i<numsSize;i++){
        ret[i+numsSize]=ret[i]=nums[i];
    }
    *returnSize=2*numsSize;
    return ret;
}
```

### 题目描述:水题

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

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

```

#### 我的题解:

```c
int* buildArray(int* nums, int numsSize, int* returnSize){
    int i;
    int*ret=(int*)malloc(sizeof(int)*numsSize);
    for(i=0;i<numsSize;++i){
        ret[i]=nums[nums[i]];
    }
    *returnSize=numsSize;
    return ret;
}
```

### 题目描述:

> [1480. 一维数组的动态和 - 力扣(LeetCode) (leetcode-cn.com)](https://leetcode-cn.com/problems/running-sum-of-1d-array/)

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

#### 我的题解:

```c
int* runningSum(int* nums, int numsSize, int* returnSize){
        int i;
        int* ret=(int*)malloc(sizeof(int)*numsSize);
        for(i=0;i<numsSize;++i){
            ret[i]=nums[i];
            if(i){
                ret[i]+=ret[i-1];
            }
            
        }
        *returnSize=numsSize;
        return ret;
}
```

#### 思路:

```c
//套题目公式即可
```

### 题目描述:

> [剑指 Offer 58 - II. 左旋转字符串 - 力扣(LeetCode) (leetcode-cn.com)](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)

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

#### 我的题解:

```c
//定义一个反转的方法
char* reverse(char* nums,int start,int end)
{
    while(start<=end){
        char tmp=nums[start];
        nums[start++]=nums[end];
        nums[end--]=tmp;
    }
    return nums;
}
char* reverseLeftWords(char* s, int n){
    // //由于输入字符串和数组稍微有点区别
    // int i;
    // //计算字符串的长度
    // int len=strlen(s);
    // //创建数组进行接受的操作,记得由于是字符串要多申请空间放入‘\0’
    // char *ret=(char*)malloc((n+1)*sizeof(char));
    // for(i=0;i<len;++i){
    //     //若要收尾相接则需要取模运算
    //     //让首部和反转位置进行相加再取模实际上就是首部反转后的地址如果是
    //     //遍历到k之后再取模则是因为相加之后超过数组长度长度与数组长度取模
    //     //相当于首尾相接后多出来的那一个部分就是后面反转后的值
    //     //若字符串为"abc"反转位置是2,则如果加之后"abcabc"取完3的摸就是到b位置
    //     ret[i]=s[(i+n)%len];
    // }
    // ret[n]='\0';
    //以上方法爆栈
    int len=strlen(s);
//反转前n个字符
    s=reverse(s,0,n-1);
    //反转n->末尾的字符
    s=reverse(s,n,len-1);
    //反转整个字符串
    s=reverse(s,0,len-1);
    return s;
}
```

#### 思路:

```c
两种方法但是有一种爆栈了,另一种套路具体思路看注释
```

精彩评论(0)

0 0 举报