/**
* Definition for singly-linked list.
* 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:
ListNode*mergeTwoLists(ListNode* list1, ListNode* list2){if(!list1 ||!list2){if(list1)return list1;return list2;}if(list1->val > list2->val)returnmergeTwoLists(list2, list1);auto p = list1, a = list1->next, b = list2;while(a && b){if(a->val <= b->val)
p->next = a, a = a->next;else
p->next = b, b = b->next;
p = p->next;}while(a) p->next = a, a = a->next, p = p->next;while(b) p->next = b, b = b->next, p = p->next;return list1;}};
22. 括号生成
class Solution {
public:voiddfs(int cur,int pre,int a,int b,int end, vector<string>& ans, string& s){if(cur == end){if(pre ==0) ans.push_back(s);return;}if(a){
s.push_back('(');dfs(cur +1, pre +1, a -1, b, end, ans, s);
s.pop_back();}if(b && pre){
s.push_back(')');dfs(cur +1, pre -1, a, b -1, end, ans, s);
s.pop_back();}}
vector<string>generateParenthesis(int n){
vector<string> ans;
string s;
s.push_back('(');dfs(1,1, n -1, n, n *2, ans, s);return ans;}};
23. 合并K个升序链表
/**
* Definition for singly-linked list.
* 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:
ListNode*merge_two(ListNode* list1, ListNode* list2){if(!list1 ||!list2){if(list1)return list1;return list2;}if(list1->val > list2->val)returnmerge_two(list2, list1);auto p = list1, a = list1->next, b = list2;while(a && b){if(a->val <= b->val)
p->next = a, a = a->next;else
p->next = b, b = b->next;
p = p->next;}while(a) p->next = a, a = a->next, p = p->next;while(b) p->next = b, b = b->next, p = p->next;return list1;}
ListNode*merge(int l,int r, vector<ListNode*>& lists){if(l == r)return lists[l];int mid = l + r >>1;
ListNode* a =merge(l, mid, lists);
ListNode* b =merge(mid +1, r, lists);returnmerge_two(a, b);}
ListNode*mergeKLists(vector<ListNode*>& lists){if(lists.size())returnmerge(0, lists.size()-1, lists);elsereturn nullptr;}};
/**
* Definition for singly-linked list.
* 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:
ListNode*rever(ListNode* head){if(head == nullptr)return nullptr;
ListNode *cur = head,*next = head->next;
head->next = nullptr;while(next){
ListNode* temp = next->next;
next->next = cur;
cur = next;
next = temp;}return cur;}
ListNode*reverseKGroup(ListNode* head,int k){
ListNode node(0, head);
ListNode* list = head;
ListNode* pre =&node;while(list){int f =1;
ListNode* cur = list,*clist = list;for(int i =0; i < k && f;++ i){if(list) clist = list, list = list->next;else f =0;}if(!f)break;
ListNode* next = list;
clist->next = nullptr;
ListNode* res =rever(cur);
pre->next = res;
cur->next = next;
pre = cur;}return node.next;}};
26. 删除有序数组中的重复项
class Solution {
public:intremoveDuplicates(vector<int>& nums){int len =0;for(int i =1; i < nums.size();++ i)if(nums[i]!= nums[i -1])
nums[++ len]= nums[i];return len +1;}};
27. 移除元素
class Solution {
public:intremoveElement(vector<int>& nums,int val){int len =-1;for(auto&i : nums)if(i != val) nums[++ len]= i;return len +1;}};
28. 实现 strStr()
class Solution {
public:intstrStr(string haystack, string needle){if(needle.size()==0)return0;for(int i =0; i + needle.size()<= haystack.size();++ i)if(haystack.substr(i, needle.size())== needle)return i;return-1;}};
29. 两数相除
class Solution {
public:intpow(int x,int c,int y){int res =0, t = x;while(c){if(c &1){if(res < y - t)return false;
res += t;}
c >>=1;if(c){if(t < y - t)return false;
t += t;}}return true;}intdivide(int dividend,int divisor){if(dividend == INT_MIN){if(divisor ==1)return INT_MIN;if(divisor ==-1)return INT_MAX;}if(divisor == INT_MIN)return dividend == INT_MIN ?1:0;if(dividend ==0)return0;int falg =0;if(dividend >0){
dividend =-dividend;
falg =!falg;}if(divisor >0){
divisor =-divisor;
falg =!falg;}int l =1, r = INT_MAX, ans =0;while(l <= r){int mid = l +((r - l)>>1);if(pow(divisor, mid, dividend)){
ans = mid;if(mid == INT_MAX)break;
l = mid +1;}else{
r = mid -1;}}return falg ?-ans: ans;}};
30. 串联所有单词的子串
class Solution {
public:
vector<int>findSubstring(string s, vector<string>& words){
unordered_map<string,int> mp, cur;
vector<int> ans;for(auto& i : words) mp[i]++;int len = words[0].size();for(int i =0; i < len;++ i){
queue<int> que;for(int j = i; j + len <= s.size(); j += len){
string st = s.substr(j, len);while(que.size()&& cur[st]== mp[st]){int t = que.front(); que.pop();
cur[s.substr(t, len)]--;}if(mp[st]){
que.push(j);
cur[st]++;}if(que.size()== words.size()) ans.push_back(que.front());}
cur.clear();}return ans;}};