#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#define max(a, b) ((a) > (b) ? (a) : (b))
struct ListNode{
int val;
ListNode* next;
ListNode() :val(0), next(nullptr) {}
ListNode(int x) :val(x), next(nullptr) {}
ListNode(int x, ListNode* next) :val(x), next(next) {}
};
class Solution {
public:
std::vector<int>subnums(std::vector<int>& nums, int target)
{
int size = nums.size();
for (int i = 0; i < size; i++)
{
for (int j = i; j < size; j++)
{
if ((nums[i] + nums[j]) == target)
{
std::cout << i << " " << j << std::endl;
return{ i,j };
}
}
}
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head = nullptr, * tail = nullptr;
int n1, n2, sum, carry = 0;
while (l1 || l2)
{
n1 = l1 ? l1->val : 0;
n2 = l2 ? l2->val : 0;
sum = n1 + n2 + carry;
if (!head)
{
head = tail = new ListNode(sum % 10);
}
else
{
tail->next = new ListNode(sum % 10);
tail = tail->next;
}
carry = sum / 10;
if (l1)
{
l1 = l1->next;
}
if (l2)
{
l2 = l2->next;
}
}
if (carry)
{
tail->next = new ListNode(carry);
tail = tail->next;
}
return head;
}
void insert(ListNode* head, int dat)
{
ListNode* newnode = new ListNode(dat);
ListNode* node = head;
while (node->next != 0)
{
node = node->next;
}
node->next = newnode;
}
void print(ListNode* head)
{
ListNode* node = head;
while (node != 0)
{
int dat = node->val;
std::cout << dat << " ";
node = node->next;
}
}
int lengthOfLongestSubstring(std::string s) {
// 哈希集合,记录每个字符是否出现过
std::unordered_set<char> occ;
int n = s.size();
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
int rk = -1, ans = 0;
// 枚举左指针的位置,初始值隐性地表示为 -1
for (int i = 0; i < n; ++i) {
if (i != 0) {
// 左指针向右移动一格,移除一个字符
occ.erase(s[i - 1]);
}
while (rk + 1 < n && !occ.count(s[rk + 1])) {
// 不断地移动右指针
occ.insert(s[rk + 1]);
++rk;
}
// 第 i 到 rk 个字符是一个极长的无重复字符子串
ans = max(ans, rk - i + 1);
}
return ans;
}
};
int main()
{
std::vector<int> nums = { 1,2,3,4,6,9 };
Solution s;
// s.subnums(nums, 15);
ListNode* l1 = new ListNode();
l1->val = 2;
ListNode* l2 = new ListNode();
l2->val = 5;
s.insert(l1, 4);
s.insert(l1, 3);
s.insert(l2, 6);
s.insert(l2, 4);
ListNode *node = s.addTwoNumbers(l1, l2);
// s.print(node);
std::string s1{ "abcabcbb" };
s.lengthOfLongestSubstring(s1);
return 0;
}