【leetcode】数组篇刷题 --删除元素

// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem27.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=27 lang=cpp
 *
 * [27] 移除元素
 */

// @lc code=start
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {

    //原地移除,返回arr长度,不必考虑元素顺序

    //快慢指针
    //rp用于遍历完数组
    //lp用于更新数值,lp永远指向下一个可用于更新数值的坑位
    int lp = 0;
    int rp = 0;
    while(rp < nums.size()){
        if(nums[rp] != val ){
            nums[lp] = nums[rp];
            lp++;
            }
        rp++;
    }
    return lp;
    }
};
// @lc code=end


/*
 * @lc app=leetcode.cn id=26 lang=cpp
 *
 * [26] 删除有序数组中的重复项
 */

// @lc code=start
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int lp = 0;
        int rp = 1;
        while(rp < nums.size()){
            if(nums[lp] != nums[rp]){
                lp++;
                nums[lp] = nums[rp];
            }
            rp++;
        }
    //不同于上一题,这里的lp指针指向当前已确认的元素,而不是指向存放下一个确认元素的空位
    return lp+1;
    }
};
// @lc code=end

// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem283.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=283 lang=cpp
 *
 * [283] 移动零
 */

// @lc code=start
class Solution {
public:
//利用27题移除元素的method
//先移除所有的0,
//再追加0
    void moveZeroes(vector<int>& nums) {
        //返回值正好是追加0的第一个位置,顺着这个索引追加往后即可
        int zCount = removeElement(nums,0);
        while(zCount < nums.size()){
            nums[zCount++] = 0; 
        }

        
    }

    int removeElement(vector<int>& nums, int val) {

   
    int lp = 0;
    int rp = 0;
    while(rp < nums.size()){
        if(nums[rp] != val ){
            nums[lp] = nums[rp];
            lp++;
            }
        rp++;
    }
    //这里返回的是下一个空位(可以理解为数组大小,而不是end索引)
    return lp;
    }
};
// @lc code=end


 *
 * [283] 移动零
 */

// @lc code=start
class Solution {
public:
//利用27题思路,但仅交换
    void moveZeroes(vector<int>& nums) {
        int lp = 0;
        int rp = 0;
        while(rp < nums.size()){
            if(nums[rp] != 0){
                // nums[lp] = nums[rp];
                //同27题相比修改了此行
                swap(nums[lp],nums[rp]);
                lp++;
            }
            rp++;
        }  
    }
  
};
*
 * @lc app=leetcode.cn id=844 lang=cpp
 *
 * [844] 比较含退格的字符串
 */

// @lc code=start
class Solution {
public:
    bool backspaceCompare(string s, string t) {
        // scanString(s);、
        //idea:重构string,依次对s,t进行扫描。比对重构后的string
        //法1:用栈扫描重构法
        return buldingString(s) == buldingString(t);

        // return false;
    }

    string buldingString(string s){
        string str;
        for(char ch : s){
            if(ch != '#'){
                str.push_back(ch);
                //pop判空
            }else if(!str.empty()){
                str.pop_back();
            }
        }
        return str;
    }
// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem844.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=844 lang=cpp
 *
 * [844] 比较含退格的字符串
 */

// @lc code=start
class Solution {
public:
    bool backspaceCompare(string s, string t) {
        // scanString(s);、
        //idea:重构string,依次对s,t进行扫描。比对重构后的string
        //法2:用双指针扫描重构法
     
        return !scanString(s).compare(scanString(t));

        // return false;
    }
    
    string scanString(string arr){
        string str = arr;
        int lp = 0;
        int rp = 0;
        while(rp < str.length()){
            if(str.at(rp) != '#'){
                str[lp] = str[rp];
                lp++;
            }else{
                lp--;
            }
            rp++; 
            //过多回退符导致回退到负索引 
            if(lp < 0){
                lp = 0;
             }
        }
        //返回切割后的string
        return str.substr(0,lp);
    }
    
};
// @lc code=end

出现溢出

// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem977.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=977 lang=cpp
 *
 * [977] 有序数组的平方
 */

// @lc code=start
#include<queue>
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
       //test case 有序,且可能含负数
       //若含负数则平方后存在两组有序序列
       //对此两组有序序列进行归并排序即可
        vector<int> ans;
       //1.若含负数
       //找到最后一个负数的index
       int i;
        for(int j = 0; j < nums.size(); j++){
            if(nums[i] < 0){
                i++;
            }
       }
       //i必指向非负,
       //如含负,则i-1必指向最后一个负
        int negative = i - 1;
        while(negative >= 0 || i < nums.size()){
            //正数到头了
            if(i == nums.size()){
                ans.push_back(nums[negative] * nums[negative]);
                negative--;
                //负数到头了
            }else if(negative < 0){
                ans.push_back(nums[i] * nums[i]);
                i++;
                //负平方>正平方
            }else if(nums[negative] * nums[negative] < nums[i] * nums[i]){
                 ans.push_back(nums[negative] * nums[negative]);
                negative--;
                //正平方>负平方
            }else{
                 ans.push_back(nums[i] * nums[i]);
                i++;
            }
        }
        return ans;
        
    }
};
// @lc code=end


// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem977.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=977 lang=cpp
 *
 * [977] 有序数组的平方
 */

// @lc code=start
#include<queue>
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
      //两个指针从数组左右两端开始比较
      //每次将大的一个数放入返回数组中
      //返回数组
      vector<int> arr(nums.size(),0);
      //分别指向排序数组的首、尾
      int rp = nums.size() - 1;
      int lp = 0;
      //返回数组,从该数组的末尾开始添加元素。
      for(int i = nums.size()-1; i >= 0; i--){
        if(nums[rp] * nums[rp] > nums[lp] * nums[lp]){
            arr[i] = nums[rp]*nums[rp];
            rp--;
        }else{
            arr[i] = nums[lp]*nums[lp];
            lp++;
        }
      }
      return arr;   
    }
};
// @lc code=end


image

posted @ 2024-02-23 19:30  main(void)  阅读(7)  评论(0编辑  收藏  举报
.c_ad_block { display: none !important; } #ad_t2{ display: none !important; }