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

凉意 2022-01-15 19:45:30

第一题

1470. 重新排列数组

给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。

请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。

 

九日集训做法:

 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;
     }
 };

 

时间复杂度上已经O(N)了,可利用位运算达到O(1)的空间复杂度:

 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. 数组串联

给你一个长度为 n 的整数数组 nums 。请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < n 的 i ,满足下述所有要求:

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

返回数组 ans 。

 

九日集训做法:

 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;
     }
 };

 

简单题,时间复杂度已为O(N),没多少优化空间。

 

第三题

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 也包含在内)的不同整数组成的数组。

 

九日集训做法:

 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;
     }
 };

 

简单题,时间复杂度已为O(N),没多少优化空间。

 

第四题

1480. 一维数组的动态和

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

请返回 nums 的动态和。

 

九日集训做法:

 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;
     }
 };

 

简单题,时间复杂度已为O(N),也可考虑直接原地修改:

 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;
     }
 };

 

 

第五题

剑指 Offer 58 - II. 左旋转字符串

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

 

九日集训做法:

 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 地址无效化

给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。

所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."

 

九日集训做法:

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

 

简单题,优化意义不大。

 

第七题

剑指 Offer 05. 替换空格

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

 

九日集训做法:

 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. 有多少小于当前数字的数字

给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。

换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。

以数组形式返回答案。

 

九日集训做法:

 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;
     }
 };

 

 

第九题

剑指 Offer 17. 打印从1到最大的n位数

输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。

 

九日集训做法:

 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;
     }
 };

 

由于题目返回的是vector<int>, 故不考虑大数问题,若要考虑大数问题可使用dfs进行全排列求解,这里不过多展开。

 

第十题

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

给你两个整数数组 nums 和 index。你需要按照以下规则创建目标数组:

目标数组 target 最初为空。 按从左到右的顺序依次读取 nums[i] 和 index[i],在 target 数组中的下标 index[i] 处插入值 nums[i] 。 重复上一步,直到在 nums 和 index 中都没有要读取的元素。 请你返回目标数组。

题目保证数字插入位置总是存在。

 

九日集训做法:

 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;
     }
 };

 

简单题,按照题意进行模拟,然后输出答案。

...全文
69 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,172

社区成员

发帖
与我相关
我的任务
社区描述
学习「 算法 」的捷径就是 「 题海战略 」,社区由「 夜深人静写算法 」作者创建,三年ACM经验,校集训队队长,亚洲区域赛金牌,世界总决赛选手。社区提供系统的训练,答疑解惑,面试经验,大厂内推等机会
社区管理员
  • 英雄哪里出来
  • 芝麻粒儿
  • Amy卜bo皮
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

QQ群:480072171

英雄算法交流 8 群

 

试试用AI创作助手写篇文章吧