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

YUAN_798 2022-01-15 20:16:22

### 一、今日知识点总结

1. 学习了指针的概念、如何定义指针、取地址、解引用等

2. 熟悉了vector相关操作

### 二、今日做题记录

1470. 重新排列数组

class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
//定义temp中的前n个元素为nums中的前n个元素
vector<int> temp(nums.begin(),nums.begin()+n);

for(int i = 0; i < 2 * n; i+=2){
nums[i] = temp[i / 2];
nums[i + 1] = nums[i / 2 + n];
}
return nums;
}
};

class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> ret(nums.begin(),nums.end());
for(int i = 0;i < 2*n;i++){
if(i&1){
//如果i为奇数
ret[i] = nums[n + i / 2];
}else{
//如果i为偶数
ret[i] = nums[(i+1) / 2];
}
}
return ret;
}
};

1929. 数组串联

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

1920. 基于排列构建数组

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

1480. 一维数组的动态和

class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
vector<int> ans(nums);
for(int i = 0; i < nums.size(); 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) {
reverse(s.begin(),s.end());
reverse(s.end()-n,s.end());
reverse(s.begin(),s.end()-n);
return s;
}
};

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

char* reverseLeftWords(char* s, int k){
int i;
int n = strlen(s);
char *ret = (char*)malloc(sizeof(char)*(n+1));

for(int i = 0;i < n; i++){
ret[i] = s[(i + k) % n];
}
ret[n] = '\0';
return ret;
}

1108. IP 地址无效化

class Solution {
public:
for(int i = 0; i < address.size();i++){
i += 2;
}
}
}
};

char *ret = (char*)malloc(sizeof(char)*1000);
int returnSzie = 0;
ret[returnSzie++] = '[';
ret[returnSzie++] = '.';
ret[returnSzie++] = ']';
}else{
}
}
ret[returnSzie] = '\0';
return ret;
}

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

char* replaceSpace(char* s){
char *ret = malloc( sizeof(char) * 30001 );
int retSize = 0;
for(int i = 0; s[i]; ++i) {
if(s[i] == ' ') {
ret[retSize++] = '%%';
ret[retSize++] = '2';
ret[retSize++] = '0';
}else {
ret[retSize++] = s[i];
}
}
ret[retSize] = '\0';
return ret;
}

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

C++双层for循环，直接暴力解

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

}
};

class Solution {
public:
vector<int> printNumbers(int n) {
vector<int> ret;
int max = pow(10,n);
for(int i = 1; i < max; i++){
ret.push_back(i);
}
return ret;
}
};

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

class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
vector<int> target;
int n = nums.size();
for(int i = 0; i < n; i++){
target.insert(target.begin()+index[i],nums[i]);
}
return target;
}
};

### 三、今日收获

1.了解了如何在LeetCode提交带指针的题目，掌握了需要返回一维数组的题目的相关范式

2.了解到判断奇偶的一种新写法

if(i&1 == 1) 等价于if(i % 2 != 0) ，即表示如果i为奇数

if(I&1 == 0)等价于if(i%2 == 0)，即表示如果i为偶数

...全文
31 回复 1 打赏 收藏 举报

6.0w+

2022-01-15 20:16

QQ群：480072171