# [学习报告]《LeetCode零基础指南》(第四讲) 指针

## 第一题

1470. 重新排列数组

``` class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> ret(2*n);
for(int i = 0;i < 2*n;++i){
if(i & 1)ret[i] = nums[n+i/2];
else ret[i] = nums[(i+1)/2];
}
return ret;
}
};```

``` class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
for(int i = 0; i < 2*n; i ++){
int j;
if(i < n)j = 2 * i;
else j = 2 * (i - n) + 1;
nums[j] |= (nums[i] & 1023) << 10;
}
for(int& e: nums) e >>= 10;
return nums;
}
};```

## 第二题

1929. 数组串联

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

``` class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n*2);
for(int i = 0;i < n;++i)
ans[i] = ans[n+i] = nums[i];
return ans;
}
};```

## 第三题

1920. 基于排列构建数组

``` class Solution {
public:
vector<int> buildArray(vector<int>& nums) {
int n = nums.size();
vector<int>ans(n);
for(int i = 0;i < n;++i)
ans[i] = nums[nums[i]];
return ans;
}
};```

## 第四题

1480. 一维数组的动态和

``` class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
for(int i = 0;i < n;++i){
ans[i] = nums[i];
if(i) ans[i] += ans[i-1];
}
return ans;
}
};```

``` class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
int n = nums.size();
for(int i = 1;i < n;++i)
nums[i] += nums[i-1];
return nums;
}
};```

## 第五题

``` class Solution {
public:
string reverseLeftWords(string s, int n) {
int l = s.length();
string ans;
for(int i = 0;i < l;++i)
ans += s[(i+n)%l];
return ans;
}
};```

``` class Solution {
public:
string reverseLeftWords(string s, int n) {
int l = s.length();
reverse(s.begin()+n,s.end());
reverse(s.begin(),s.end());
reverse(s.begin()+l-n,s.end());
return s;
}
};```

## 第六题

1108. IP 地址无效化

``` class Solution {
public:
string ans;
for(int i = 0;address[i];++i){
if(address[i] == '.')ans += "[.]";
else ans += address[i];
}
return ans;
}
};```

## 第七题

``` class Solution {
public:
string replaceSpace(string s) {
string ans;
for(int i = 0;s[i];++i){
if(s[i] == ' ')ans += "%20";
else ans += s[i];
}
return ans;
}
};```

## 第八题

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

``` class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
for(int i = 0;i < n;++i){
ans[i] = 0;
for(int j = 0;j < n;++j)
if(nums[j] < nums[i])++ans[i];
}
return ans;
}
};```

``` class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int> cnt(101, 0);
int n = nums.size();
for (int i = 0; i < n; i++)cnt[nums[i]]++;
for (int i = 1; i <= 100; i++)cnt[i] += cnt[i - 1];
vector<int> ans;
for (int i = 0; i < n; i++)
if(nums[i])ans.push_back(cnt[nums[i]-1]);
else ans.push_back(0);
return ans;
}
};```

## 第九题

``` class Solution {
public:
vector<int> printNumbers(int n) {
int f = 1;
for(int i = 0;i < n;++i){
f *= 10;
}
--f;
vector<int> ans(f);
for(int i = 1;i <= f;++i)
ans[i-1] = i;
return ans;
}
};```

## 第十题

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

``` class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
int n = nums.size();
vector<int> ans(n);
for(int i = 0;i < n;++i){
int idx = index[i];
int ins = nums[i];
for(int j = i;j > idx;--j){
ans[j] = ans[j-1];
}
ans[idx] = ins;
}
return ans;
}
};```

...全文
11 回复 2 打赏 收藏 举报

6.0w+

2022-01-16 19:26

2022-01-15 19:45

QQ群：480072171