2022-03-18 18:05

# 《LeetCode零基础指南》(第四讲) 指针

``````class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> res;
for(int i=0;i<n;i++){
res.push_back(nums[i]);
res.push_back(nums[n+i]);
}
return res;
}
};
``````

1480. 一维数组的动态和

``````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) {
s += s.substr(0,n);
return s.substr(n,s.length()-n);
}
};

``````

``````class Solution {
public:
string replaceSpace(string s) {
for(int i=0;i<s.size();i++){
if(s[i]==' '){
s.erase(s.begin()+i);
s.insert(i,"%20");
}
}
return s;
}
};

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

``````
``````class Solution {
public:
string replaceSpace(string s) {
for(int i=0;i<s.size();i++)
{
if(s[i]==' ') s=s.replace(i,1,"%20");
}
return s;
}

};``````

stl摸鱼系列

``````class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int> v=nums;
sort(v.begin(),v.end());

int hash[110];

for(int i=v.size()-1;i>=0;i--)
hash[v[i]]=i;

for(int i=0;i<nums.size();i++)
v[i]=hash[nums[i]];

return v;
}

};``````

``````class Solution {
public:
vector<int> printNumbers(int n) {
vector<int> ans(pow(10,n)-1);
iota(begin(ans),end(ans),1);
return ans;
}
};

``````
``````class Solution {
public:
vector<int> printNumbers(int n) {
vector<int> ans;
// 最大的n位数
int t = pow(10, n) - 1;
for(int i = 1; i <= t; i++){
ans.push_back(i);
}
return ans;
}
};

``````

• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答