[Algorithm][综合训练][两个数组的交集][点击消除][牛牛的快递]详细讲解


1.两个数组的交集

1.题目链接


2.算法原理详解 && 代码实现

  • 自己的版本:排序 + 双指针 + 哈希表
     vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
     {
         set<int> hash;
         vector<int> ret;
         sort(nums1.begin(), nums1.end());
         sort(nums2.begin(), nums2.end());
    
         // 双指针
         int p1 = 0, p2 = 0;
         while(p1 < nums1.size() && p2 < nums2.size())
         {
             if(nums1[p1] == nums2[p2])
             {
                 hash.insert(nums1[p1]);
                 p1++;
                 p2++;
             }
             else if(nums1[p1] < nums2[p2])
             {
                 p1++;
             }
             else
             {
                 p2++;
             }
         }
    
         for(const auto& x : hash)
         {
             ret.push_back(x);
         }
    
         return ret;
     }
    
  • 优化版本:直接用哈希表
    • 哈希表用原生数组模拟即可(true为在,false为不在)
    • 为避免重复,在找到相同元素之后,可将哈希表中的值删除
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
    {
    	bool hash[1001] = { 0 };
    	vector<int> ret;
    	
    	for(const auto& x : nums1)
    	{
    		hash[x] = true;
    	}
    
    	for(const auto& x : nums2)
    	{
    		ret.push_back(x);
    		hash[x] = false;
    	}
    
    	return ret;
    }
    

2.点击消除

1.题目链接


2.算法原理详解 && 代码实现

  • 自己的版本:直接string
     #include <iostream>
     #include <string>
     using namespace std;
     
    int main()
    {
        string str;
        getline(cin, str);
     
        // 双指针
        int left = 0, right = 1;
        while(right < str.size())
        {
            if(str[left] == str[right])
            {
                str.erase(left, 2);
     
                if(left > 0)
                {
                    left--;
                }
                if(right > 1)
                {
                    right--;
                }
            }
            else
            {
                left++;
                right++;
            }
        }
     
        if(str.empty())
        {
            cout << 0;
        }
        else
        {
            cout << str;
        }
     
        return 0;
    }
    
  • 优化版本:栈
    • 细节:用可变长数组(如string)模拟栈结构
      • 为什么这是一个细节,这样做更好?
        • 最后从栈中拿数据是一个逆序的结果,还要对此结果再进行一次逆序才能拿到最终想要的结果,麻烦
        • 而用可变长数组模拟则没有这种问题
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string str, st;
    	getline(cin, str);
    
    	for(const auto& ch : str)
    	{
    		if(st.size() && st.back() == ch)
    		{
    			st.pop_back()
    		}
    		else
    		{
    			st += ch;
    		}
    	}
    
    	cout << (st.size() == 0 ? 0 : st) << endl;
    
    	return 0;
    }
    

3.牛牛的快递

1.题目链接


2.算法原理详解 && 代码实现

  • 自己的版本while硬算
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int cnt = 20;
        float weight = 0.0;
        char flag = 'n';
    
        cin >> weight >> flag;
        if(flag == 'y')
        {
            cnt += 5;
        }
    
        weight--;
        while(weight > 0.0)
        {
            weight--;
            cnt++;
        }
    
        cout << cnt << endl;
        
        return 0;
    }
    
  • 优化版本:向上取整
    • 库函数ceil()
    • (int)x向下取整,再判断x - int(x) > 0,则(int)x + 1
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int cnt = 20;
    	double weight = 0.0;
    	char flag = 'n';
    	
    	cin >> weight >> flag;
    	if(flag == 'y')
    	{
    		cnt += 5;
    	}
    
    	if(--weight > 0.0)
    	{
    		// cnt += ceil(weight); // v1.0
    
    		// v2.0
    		if(weight - (int)weight > 0)
    		{
    			cnt += (int)weight + 1;
    		}
    	}
    
    	cout << cnt << endl;
    	
    	return 0;
    }
    
  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DieSnowK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值