64,111
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
目录
剑指 Offer 17. 打印从1到最大的n位数
解法一:临时空间法
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int>jie(2*n);
for(int i=0;i<2*n;i+=2){
jie[i+1]=nums[n+i/2];
jie[i]=nums[i/2];
}
return jie;
}
};
class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
int n=nums.size();
vector<int>jie(2*n);
for(int i=0;i<n;i++){
jie[i]=nums[i];
jie[i+n]=nums[i];
}
return jie;
}
};
或:
class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
vector<int>jie(nums.begin(),nums.end());
for(auto it:nums)jie.emplace_back(it);
return jie;
}
};
class Solution {
public:
vector<int> buildArray(vector<int>& nums) {
int n=nums.size();
vector<int>jie(n);
for(int i=0;i<n;i++)jie[i]=nums[nums[i]];
return jie;
}
};
//有兴趣的小伙伴可以顺便了解一下【前缀和】这种解题技巧
class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
int n=nums.size();
vector<int>dp(n);
dp[0]=nums[0];
for(int i=1;i<n;i++)dp[i]=nums[i]+dp[i-1];
return dp;
}
};
//这道题可以帮助巩固很多字符串基操。
解法一:字符串的截取操作。
class Solution {
public:
string reverseLeftWords(string s, int n) {
s += s;
return s.substr(n,s.size()/2); //从第n号位开始截取
}
};
解法二:三次反转法
class Solution {//技巧性很强,稍作观察
public:
string reverseLeftWords(string s, int n) {
int len=s.size();
reverse(s.begin(),s.end()-len+n);
reverse(s.begin()+n,s.end());
reverse(s.begin(),s.end());
return s;
}
};
解法三:临时空间法
class Solution {
public:
string reverseLeftWords(string s, int n) {
string jie;
for(int i=n;i<s.size();i++){
jie.push_back(s[i]);
}
for(int i=0;i<n;i++){
jie.push_back(s[i]);
}
return jie;
}
};
解法四:拼接法//一行
class Solution {
public:
string reverseLeftWords(string s, int n) {
return s.substr(n,s.size()-n)+s.substr(0,n);
}
};
解法五:指针法
class Solution {
public:
string reverseLeftWords(string s, int n) {
string jie="";
for(int i=0;i<s.size();i++)jie+=s[(i+n)%s.size()];
return jie;
}
};
1108. IP 地址无效化
//下面两题套用临时空间很容易秒掉,先不深入了
class Solution {
public:
string defangIPaddr(string address) {
string jie;
for(char i:address){
if(i=='.')jie+="[.]";
else jie+=i;
}
return jie;
}
};
class Solution {
public:
string replaceSpace(string s) {
string k="";
for(int i=0;i<s.size();i++){
if(s[i]!=' ')k+=s[i];
else k+="%20";
}
return k;
}
};
解法一:直观的BF;
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int>jie;
for(int i=0;i<nums.size();i++){
int tmp=0;
for(int j=0;j<nums.size();j++){
if(j==i)continue;
if(nums[i]>nums[j])tmp++;
}
jie.emplace_back(tmp);
}
return jie;
}
};
解法二:前缀和解法
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
vector<int>cnt(101,0);
for(int i:nums)cnt[i]++;
for(int i=1;i<101;i++)cnt[i]+=cnt[i-1]; //用前缀和计算小于i的元素个数
vector<int>jie(nums.size());
for(int i=0;i<nums.size();i++){
if(nums[i]==0)jie[i]=0;
else jie[i]=cnt[nums[i]-1];
}
return jie;
}
};
解法一:直接破】//如果n超过64字节怎么办,那就只能用大数算法了——(用数组模拟大数)
class Solution {
public:
vector<int> printNumbers(int n) {
vector<int>jie;
for(int i=1;i<pow(10,n);i++)jie.push_back(i);
return jie;
}
};
解法二:补充一下内置函数
class Solution {
public:
vector<int> printNumbers(int n) {
vector<int>jie(pow(10,n)-1);
iota(jie.begin(),jie.end(),1); //iota,包含在头文件numeric里面,会用连续的T类型值填充数组
return jie; //效果,就如题目一般
}
};
class Solution {//C++,STL
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
vector<int>jie;
for (int i = 0; i < nums.size(); ++i) {
jie.insert(jie.begin() + index[i], nums[i]);
}
return jie;
}
};