Leetcode早餐组合 (vector容器的使用)

题目描述

小扣在秋日市集选择了一家早餐摊位,一维整型数组 staple 中记录了每种主食的价格,一维整型数组 drinks 中记录了每种饮料的价格。小扣的计划选择一份主食和一款饮料,且花费不超过 x 元。请返回小扣共有多少种购买方案。

注意:答案需要以 1e9 + 7 (1000000007) 为底取模,如:计算初始结果为:1000000008,请返回 1

示例 1:

输入:staple = [10,20,5], drinks = [5,5,2], x = 15

输出:6

解释:小扣有 6 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
第 1 种方案:staple[0] + drinks[0] = 10 + 5 = 15;
第 2 种方案:staple[0] + drinks[1] = 10 + 5 = 15;
第 3 种方案:staple[0] + drinks[2] = 10 + 2 = 12;
第 4 种方案:staple[2] + drinks[0] = 5 + 5 = 10;
第 5 种方案:staple[2] + drinks[1] = 5 + 5 = 10;
第 6 种方案:staple[2] + drinks[2] = 5 + 2 = 7。

示例 2:

输入:staple = [2,1,1], drinks = [8,9,5,1], x = 9

输出:8

解释:小扣有 8 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
第 1 种方案:staple[0] + drinks[2] = 2 + 5 = 7;
第 2 种方案:staple[0] + drinks[3] = 2 + 1 = 3;
第 3 种方案:staple[1] + drinks[0] = 1 + 8 = 9;
第 4 种方案:staple[1] + drinks[2] = 1 + 5 = 6;
第 5 种方案:staple[1] + drinks[3] = 1 + 1 = 2;
第 6 种方案:staple[2] + drinks[0] = 1 + 8 = 9;
第 7 种方案:staple[2] + drinks[2] = 1 + 5 = 6;
第 8 种方案:staple[2] + drinks[3] = 1 + 1 = 2;

提示:

1 <= staple.length <= 10^5
1 <= drinks.length <= 10^5
1 <= staple[i],drinks[i] <= 10^5
1 <= x <= 2*10^5

解题思路

方法一:暴力法(两个for循环)

//设数组arr1代表食物,数组arr2代表饮料,从两个数组中各取一个数相加,得到的结果小于总金额x=15
int main(){
	int count = 0;
	int arr1[]={10,20,5};
	int arr2[]={5,5,2};
	for(int i=0; i<3; i++){
		for(int j=0; j<3; j++){
			if(arr1[i]+arr2[j] <=15){
				cout << arr1[i] << "\t" << arr2[j] << endl;
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

方法二:利用指针

#include <iostream>
#include <vector>	//使用容器需要的库 
#include <algorithm>	//使用sort需要的库 
#include <functional>
using namespace std;
```class Solution {
public:
    int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
       int res = 0;
       sort(staple.begin(), staple.end());	//对容器中staple的数进行从小到大排序(默认是升序) 
       sort(drinks.begin(), drinks.end());	//对容器中drinks的数进行从小到大排序(默认是升序)
       int j = drinks.size() - 1, stapleSize = staple.size();
       for(int i=0; i<stapleSize; ++i)	//i从前向后,指向容器的第一个数字;j从后向前,指向容器的最后一个数字 
       {								//若不满足条件,j向前移动 
           while(j >= 0 && staple[i] + drinks[j] > x) j --;
           if(j == -1) break;
           res += j + 1;			//所有满足条件的情况加起来
           res %= 1000000007;		//答案需要以 1e9 + 7 (1000000007) 为底取模,如:计算初始结果为:1000000008,请返回 1 
       } 
       return res;
    }
};

int main(){
	vector<int> a = {10,20,5};
	vector<int> b = {5,5,2};
	int x=15; 
	Solution s;
	
	int c = s.breakfastNumber(a,b,x);

	cout << c << endl;
	return 0;
}

方法三:提供一个思路。当前金额 - 早餐金额=最大饮料金额。若可选的饮料金额小于等于最大饮料金额,则该方案可行

今日笔记

1.库的使用

  #include <iostream>  //std::cout
  #include <functional> //std.functional
  #include <algorithm>	//使用sort需要的库
/*
包含这个库后
#include <algorithm>
才可以使用以下函数
(1)sort(a.begin(),a.end()); //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素进行从小到大排列
(2)reverse(a.begin(),a.end()); //对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1
(3)copy(a.begin(),a.end(),b.begin()+1); //把a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开        始复制,覆盖掉原有元素
(4)find(a.begin(),a.end(),10); //在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置
*/ 

2.迭代器的使用

vector<int> a = {10,20,5};
//使用时两种写法 vector<int>::iterator ite = a.begin();
vector<int>::iterator ite;
for(ite = a.begin();ite != a.end(); ite++){
	cout << *ite;
}

3.vector容器的使用

#include <vector> 
#include <iostream>
#include <algorithm>

using namespace std;
 
void fun(int i){
	cout << i << endl;
}
 
int main(){
	vector<int> vec = {1,5,9};
	for (int i = 0; i < 10; i++){	//将0~9依次放入vector容器中 
		vec.push_back(i);
	}
	for (int i = 0; i < 10; i++){
		cout << " " << vec[i];		//输出  1 5 9 0 1 2 3 4 5 6 
	}
	cout << endl;
	for (int i = 0; i < 10; i++){
		cout << vec.at(i);		//输出1590123456。如果访问越界,会抛出异常,比下标运算更安全,流畅
	}
	cout << endl;
	cout << vec.back() << endl;	//输出9,即原容器最后一个 

//输出全部元素   1590123456789
	vector<int>::iterator ite = vec.begin();
	for (; ite != vec.end(); ite++){
		cout << *ite;
	}  
	cout << endl;
 
	for_each(vec.begin(), vec.end(), fun);	//for_each(开始元素,结束元素,调用函数),for_each函数可对每个元素操作 
	cout << endl;
 

	return 0;
}

4.vector的赋值(将一个容器中的内容复制到另一个容器中)

int main(){
	vector<int> vc = {1,1,2,2,2,3};
	vector<int> b;
	b.assign(vc.begin(),vc.end());
	//输出b容器中的内容
	for(int i=0; i<b.size(); i++){
		cout << b[i] << endl;
	}
	return 0}

5.如何调用vector作返回值的函数

`

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<int> box(vector<int>& nums) {
		sort(nums.begin(), nums.end());
        return nums;
    }
};



int main(){
	vector<int> vc = {1,1,5,2,2,3};
	vector<int> b;
	Solution s;
	b=s.box(vc);
	//方法一:(数组输出)输出b容器中的内容
	for(int i=0; i<b.size(); i++){
		cout << b[i] ;
	}
	cout << endl;
	//方法二:(迭代器输出)输出b容器中的内容
	for(vector<int>::iterator it = b.begin(); it!=b.end(); it++){
		cout << *it << endl;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值