- 题目一:
/*
题目:加起来和为目标值的组合(二):
给出一组候选数c和一个目标数t,找出候选数中加起来和等于target的所有组合。
c中的每个数字在一个组合中只能使用一次。
注意:
1. 题目中所有的数字(包括目标数target)都是正整数
2. 组合中的数字要按非递减排序
3. 结果中不能包含重复的组合
4. 组合之间的排序按照索引从小到大依次比较,小的排在前面,如果索引相同的情况下数值相同,则比较下一个索引
要求:空间复杂度O(n),时间复杂度O(2^n)
示例1:
输入:[100, 10, 20, 70, 60, 10, 50], 80
输出:[[10, 10, 60], [10, 20, 50], [10, 70], [20, 60]]
说明:给定的候选数集是[100, 10, 20, 70, 60, 10, 50],目标数是80
示例2:
输入:[2], 1
输出:[]
class Solution
{
public:
std::vector<std::vector<int>> combinationSum2(std::vector<int> &num, int target)
{
}
};
*/
/*
解题思路:
回溯算法
*/
#include <iostream>
#include <vector>
#include <algorithm>
bool myfun(int i, int j) {return (i < j);}
class Solution
{
public:
std::vector<std::vector<int>> res;
std::vector<std::vector<int>> combinationSum2(std::vector<int> &num, int target)
{
if (num.empty())
{
return res;
}
std::sort(num.begin(), num.end(), myfun);
std::vector<int> temp;
back_tracking(num, temp, 0, 0, target);
return res;
}
void back_tracking(std::vector<int> &num_, std::vector<int> &temp_, int cur_sum, int begin_, int target_)
{
if (cur_sum >= target_)
{
if (cur_sum == target_)
{
res.emplace_back(temp_);
}
return;
}
for (int i = begin_; i != num_.size(); ++i)
{
if (i > begin_ && num_[i] == num_[i - 1])
{
continue;
}
temp_.emplace_back(num_[i]);
back_tracking(num_, temp_, cur_sum + num_[i], i + 1, target_);
temp_.pop_back();
}
return;
}
};
int main()
{
Solution so;
std::vector<int> a = {1, 2, 4, 3, 2, 1, 4, 6, 8, 4};
std::vector<std::vector<int>> res_;
res_ = so.combinationSum2(a, 12);
for (int i = 0; i != res_.size(); ++i)
{
std::cout << "[";
for (int j = 0; j != res_[i].size(); ++j)
{
if (j != res_[i].size() - 1)
{
std::cout << res_[i][j] << ", ";
}
else
{
std::cout << res_[i][j];
}
}
std::cout << "]";
std::cout << std::endl;
}
return 0;
}
- 题目二:
/*
题目:LeetCode 141. 环形链表
给你一个链表的头节点head,判断链表中是否有环。
如果链表中有某个节点,可以通过连续判断next指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数pos来表示链表尾连接到链表中的位置(索引从0开始)。注意:pos不作为参数进行传递。仅仅是为了标识链表的实际情况。
如果链表中存在环,则返回true。否则,返回false。
示例1:
输入:head = [3, 2, 0, -4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
示例2:
输入:head = [1, 2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
示例3:
输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
class Solution {
public:
bool hasCycle(ListNode* head) {
}
};
*/
/*
解题思路:
快慢指针
*/
#include <iostream>
struct ListNode
{
ListNode *next;
int val;
ListNode(int x): val(x), next(nullptr){};
};
class Solution {
public:
bool hasCycle(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return false;
}
ListNode* slow = head;
ListNode* fast = head;
do {
if (fast == nullptr || fast->next == nullptr) {
return false;
}
slow = slow->next;
fast = fast->next->next;
} while (slow != fast);
return true;
}
};
int main()
{
Solution so;
ListNode *head = new ListNode(1);
ListNode *node_1 = new ListNode(7);
ListNode *node_2 = new ListNode(8);
ListNode *node_3 = new ListNode(5);
ListNode *node_4 = new ListNode(5);
ListNode *node_5 = new ListNode(2);
head->next = node_1;
node_1->next = node_2;
node_2->next = node_3;
node_3->next = node_4;
node_4->next = node_5;
node_5->next = node_2;
std::cout << "has_cycle: " << so.hasCycle(head) << std::endl;
delete head;
return 0;
}