这道题思路确实不难,想明白了就是固定思路,五块就收,十块收了找五块,二十这里注意,可以找 10 + 5 也可以 找 5 + 5 + 5 ,如果能都能找那就返回true, 否则就是false ,我这里用的是数组记录,其实用int 类型计数就可以了,ti
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
if(bills.size() == 1) {
if(bills[0] != 5) return false;
else return true;
}
vector<int> num5;
vector<int> num10;
for(int i = 0;i < bills.size();i++) {
std::cout << "i = " << i << std::endl;
if(bills[i] == 5) {
num5.push_back(bills[i]);
}else if(bills[i] == 10) {
if(!num5.empty()) {
num5.pop_back();
num10.push_back(bills[i]);
}else return false;
}else {
if(!num10.empty() && !num5.empty()) {
num10.pop_back();
num5.pop_back();
}else if(num5.size() >=3) {
num5.pop_back();
num5.pop_back();
num5.pop_back();
}else return false;
}
}
return true;
}
};
题目2:406. 根据身高重建队列 - 力扣(LeetCode)
这里根据身高排序,有大到小,然后如果身高一样就 根据 k 由小到大,然后根据k进行插入相应的位置
class Solution {
public:
static bool cmp(vector<int>& a, vector<int>& b) {
if(a[0] == b[0]) return a[1] < b[1];
return a[0] > b[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), cmp);
vector<vector<int>> qu;
for(int i = 0;i < people.size();i++) {
int position = people[i][1];
qu.insert(qu.begin() + position, people[i]);
}
return qu;
}
};
题目3:452. 用最少数量的箭引爆气球 - 力扣(LeetCode)
class Solution {
public:
static bool cmp(vector<int>& a, vector<int>& b) {
if(a[0] == b[0]) return a[1] < b[1];
return a[0] < b[0];
}
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(), points.end(), cmp);
if(points.size() == 1) return 1;
int reslut = 1;
for(int i = 1;i < points.size();i++) {
if(points[i][0] > points[i - 1][1]) {
reslut++;
}else {
points[i][1] = min(points[i][1], points[i - 1][1]);
}
}
return reslut;
}
};