LeetCode
字符串
左旋转字符串
- 申请空间,取模运算
class Solution {
public:
string reverseLeftWords(string s, int n) {
string ans=s;
for(int i=0;i!=s.size();++i)
{
ans[(i-n+s.size())%s.size()]=s[i];
}
return ans;
}
};
- 子串合并
class Solution {
public:
string reverseLeftWords(string s, int n) {
return s.substr(n,s.size()-n)+s.substr(0,n);
}
};
状态机
剑指 Offer 20. 表示数值的字符串
剑指 Offer 67. 把字符串转换成整数
链表
反转
- 头插法
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
ListNode* h = new ListNode;
while(head)
{
auto temp = h->next;
h->next=head;
auto next = head->next;
head->next=temp;
head=next;
}
vector<int>ans;
h=h->next;
while(h)
{
ans.push_back(h->val);
h=h->next;
}
return ans;
}
};
- 栈
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
deque<ListNode*>que;
while(head)
{
que.push_back(head);
head=head->next;
}
vector<int> ans;
while(!que.empty())
{
ans.push_back(que.back()->val);
que.pop_back();
}
return ans;
}
};
- 递归
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
if(!head) return {};
auto temp = reversePrint(head->next);
temp.push_back(head->val);
return temp;
}
};
- 头插法
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* h = new ListNode;
while(head)
{
auto hnext = h->next;
auto headnext = head->next;
h->next=head;
head->next=hnext;
head=headnext;
}
return h->next;
}
};
- 递归
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next) return head;
auto h = reverseList(head->next);
head->next->next=head;
head->next=nullptr;
return h;
}
};
- 栈
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head)return head;
deque<ListNode*>que;
while(head)
{
que.push_back(head);
head=head->next;
}
ListNode* ans = nullptr;
ans = que.back();
que.pop_back();
auto p =ans;
while(!que.empty())
{
p->next=que.back();
p=p->next;
que.pop_back();
}
p->next = nullptr;
return ans;
}
};
复杂链表复制
- 拼接拆分
class Solution {
public:
Node* copyRandomList(Node* head) {
if(!head)
return nullptr;
for(Node* i=head;i!=nullptr;i=i->next->next)
{
Node* newNode = new Node(i->val);
newNode->next=i->next;
i->next=newNode;
}
for(Node*i=head;i!=nullptr;i=i->next->next)
{
i->next->random=(i->random==nullptr)?nullptr:i->random->next;
}
Node* outNode = head->next;
for(Node*i=head;i!=nullptr;i=i->next)
{
Node* newNode = i->next;
i->next=newNode->next;
newNode->next=(newNode->next==nullptr)?nullptr:newNode->next->next;
}
return outNode;
}
};
双指针
删除链表节点
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* h = new ListNode;
h->next = head;
auto pre = h;
while(head)
{
if(head->val == val)
{
pre->next=head->next;
return h->next;
}
pre=pre->next;
head=head->next;
}
return h->next;
}
};
链表中倒数第k个节点
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
auto p1=head;
for(int i=0;i!=k;++i) p1=p1->next;
auto p2 = head;
while(p1)
{
p1=p1->next;
p2=p2->next;
}
return p2;
}
};
合并有序链表
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode;
auto p =head;
while(l1 && l2)
{
if(l1->val<l2->val)
{
p->next = l1;
p=p->next;
l1=l1->next;
}
else
{
p->next = l2;
p=p->next;
l2=l2->next;
}
}
if(l1)
{
p->next = l1;
}
if(l2)
{
p->next = l2;
}
return head->next;
}
};
两个链表的第一个公共节点
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
auto p1=headA;
auto p2=headB;
while(p1!=p2)
{
p1=p1?p1->next:headA;
p2=p2?p2->next:headB;
}
return p1;
}
};
调整数组顺序使奇数位于偶数前面
class Solution {
public:
vector<int> exchange(vector<int>& nums) {
int l=0;
int r=nums.size()-1;
while(l<r)
{
while(l<r && nums[l]%2==1) ++l;
while(l<r && nums[r]%2==0) --r;
swap(nums[l],nums[r]);
}
return nums;
}
};
和为s的两个数字
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int l=0;
int r=nums.size()-1;
while(l<r)
{
while(l<r && nums[l]+nums[r]<target) ++l;
while(l<r && nums[l]+nums[r]>target) --r;
if(l<r && nums[l]+nums[r]==target) return {nums[l],nums[r]};
}
return {};
}
};
翻转单词顺序
class Solution {
public:
string reverseWords(string s) {
if(s.empty()) return s;
int i=s.size()-1;
int j=s.size()-1;
while(i>=0 && s[i]==' ')
{
--i;--j;
}
string ans;
while(j>=0)
{
while(i>=0 && s[i]!=' ') --i;
ans+=s.substr(i+1,j-i);
ans+=" ";
while(i>=0 && s[i]==' ') --i;
j=i;
}
if(!ans.empty()) ans.pop_back();
return ans;
}
};
栈与队列
辅助栈
class CQueue {
public:
stack<int>q1;
stack<int>q2;
CQueue() {
}
void appendTail(int value) {
q2.push(value);
}
int deleteHead() {
if(!q1.empty())
{
auto ans = q1.top();
q1.pop();
return ans;
}
while(!q2.empty())
{
q1.push(q2.top());
q2.pop();
}
if(!q1.empty())
{
auto ans = q1.top();
q1.pop();
return ans;
}
else
{
return -1;
}
}
};
class MinStack {
public:
/** initialize your data structure here. */
stack<int>que;
stack<int>minque;
MinStack() {
minque.push(INT_MAX);
}
void push(int x) {
que.push(x);
minque.push(minque.top()<x?minque.top():x);
}
void pop() {
que.pop();
minque.pop();
}
int top() {
return que.top();
}
int min() {
return minque.top();
}
};
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int>que;
vector<int> ans;
for(int i=0;i!=nums.size();++i)
{
while(!que.empty() && que.back()<nums[i]) que.pop_back();
que.push_back(nums[i]);
if(i>=k && que.front()==nums[i-k]) que.pop_front();
if(i>=k-1) ans.push_back(que.front());
}
return ans;
}
};
class MaxQueue {
public:
deque<int>que;
deque<int>maxque;
MaxQueue() {
}
int max_value() {
if(que.empty())return -1;
return maxque.front();
}
void push_back(int value) {
while(!maxque.empty() && maxque.back()<value) maxque.pop_back();
maxque.push_back(value);
que.push_back(value);
}
int pop_front() {
if(que.empty()) return -1;
if(maxque.front()==que.front()) maxque.pop_front();
auto ans = que.front();
que.pop_front();
return ans;
}
};
模拟
顺时针打印矩阵
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.empty())return {};
int l=0,r=matrix[0].size()-1,up=0,down=matrix.size()-1;
vector<int>ans;
while(l<=r && up<=down)
{
for(int i=l;i<=r;++i)
{
ans.push_back(matrix[up][i]);
}
if(++up>down) break;
for(int i=up;i<=down;++i)
{
ans.push_back(matrix[i][r]);
}
if(--r<l)break;
for(int i=r;i>=l;--i)
{
ans.push_back(matrix[down][i]);
}
if(--down<up)break;
for(int i=down;i>=up;--i)
{
ans.push_back(matrix[i][l]);
}
if(++l>r)break;
}
return ans;
}
};
栈的压入、弹出序列
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int>que;
int i=0;
for(auto num:pushed)
{
que.push(num);
while(i<popped.size() && !que.empty() && popped[i]==que.top())
{
++i;
que.pop();
}
}
return que.empty();
}
};
查找
原地交换
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
for(int i=0;i!=nums.size();++i)
{
while(nums[i]!=i)
{
if(nums[nums[i]]==nums[i]) return nums[i];
swap(nums[i],nums[nums[i]]);
}
}
return -1;
}
};
二分查找
class Solution {
public:
int search(vector<int>& nums, int target) {
int i=0,j=nums.size()-1;
while(i<=j)
{
int mid=(i+j)/2;
if(nums[mid]<target) ++i;
else --j;
}
auto left = i;
i=0;
j=nums.size()-1;
while(i<=j)
{
int mid=(i+j)/2;
if(nums[mid]>target) --j;
else ++i;
}
auto right = j;
return right-left+1;
}
};
class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
int i=0,j=nums.size()-1;
while(i<=j)
{
int mid = (i+j)/2;
if(nums[mid]==mid) ++i;
else --j;
}
return i;
}
};
矩阵查找
class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
if(matrix.empty())return false;
int right = matrix[0].size()-1, up=0;
while(right>=0 && up<=matrix.size()-1)
{
if(matrix[up][right]==target) return true;
if(matrix[up][right]<target) ++up;
else --right;
}
return false;
}
};
旋转数组
class Solution {
public:
int minArray(vector<int>& numbers) {
int l=0;
int len = numbers.size();
while(l<len && numbers[l]==numbers[(l-1+len)%len]) ++l;
if(l==len) return numbers[0];
int r=(l-1+len)%len;
int i=0,j=len-1;
while(i<=j)
{
int mid=(i+j)/2;
if(numbers[(l+mid)%len]>=numbers[l]) ++i;
else --j;
}
return numbers[(l+i)%len];
}
};
第一个只出现一次的字符
class Solution {
public:
char firstUniqChar(string s) {
unordered_map<char,int>map;
for(auto c:s)
{
++map[c];
}
for(auto c:s)
{
if(map.at(c)==1)return c;
}
return ' ';
}
};
搜素回溯
树搜索
class Solution {
public:
deque<TreeNode*>que;
vector<int> levelOrder(TreeNode* root) {
if(!root)return {};
que.push_back(root);
vector<int>ans;
while(!que.empty())
{
ans.push_back(que.front()->val);
if(que.front()->left)que.push_back(que.front()->left);
if(que.front()->right)que.push_back(que.front()->right);
que.pop_front();
}
return ans;
}
};
剑指 Offer 32 - II. 从上到下打印二叉树 II
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(!root) return {};
deque<TreeNode*>que;
vector<vector<int>>ans;
que.push_back(root);
while(!que.empty())
{
auto size = que.size();
for(int i=0;i!=size;++i)
{
if(que[i]->left) que.push_back(que[i]->left);
if(que[i]->right) que.push_back(que[i]->right);
}
ans.push_back(vector<int>(size));
for(int i=0;i!=size;++i)
{
ans.back()[i]=que.front()->val;
que.pop_front();
}
}
return ans;
}
};
剑指 Offer 32 - III. 从上到下打印二叉树 III
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(!root) return {};
deque<TreeNode*>que;
vector<vector<int>>ans;
que.push_back(root);
while(!que.empty())
{
auto size = que.size();
for(int i=0;i!=size;++i)
{
if(que[i]->left) que.push_back(que[i]->left);
if(que[i]->right) que.push_back(que[i]->right);
}
ans.push_back(vector<int>(size));
for(int i=0;i!=size;++i)
{
ans.back()[i]=que.front()->val;
que.pop_front();
}
}
for(int i=0;i!=ans.size();++i)
{
if(i%2==1)
{
reverse(ans[i].begin(),ans[i].end());
}
}
return ans;
}
};
class Solution {
public:
bool re(TreeNode* A, TreeNode* B)
{
if(!B) return true;
if(!A) return false;
return A->val == B->val && re(A->left,B->left) && re(A->right, B->right);
}
bool isSubStructure(TreeNode* A, TreeNode* B) {
if(!B || !A) return false;
return re(A,B) || isSubStructure(A->left,B)||isSubStructure(A->right,B);
}
};
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(!root)return nullptr;
auto left = mirrorTree(root->right);
auto right = mirrorTree(root->left);
root->left=left;
root->right=right;
return root;
}
};
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(!root)return nullptr;
deque<TreeNode*> que;
que.push_back(root);
while(!que.empty())
{
auto temp = que.front()->left;
que.front()->left=que.front()->right;
que.front()->right=temp;
if(que.front()->left)que.push_back(que.front()->left);
if(que.front()->right)que.push_back(que.front()->right);
que.pop_front();
}
return root;
}
};
class Solution {
public:
bool test(TreeNode* left,TreeNode* right)
{
if(!left && !right)return true;
else if(!left || !right) return false;
return left->val==right->val && test(left->left,right->right) && test(left->right,right->left);
}
bool isSymmetric(TreeNode* root) {
if(!root)return true;
return test(root->left,root->right);
}
};
矩阵查找
class Solution {
public:
vector<vector<int>>delta={{-1,0},{1,0},{0,-1},{0,1}};
vector<vector<bool>>set;
bool fi(vector<vector<char>>& board, string word, int id, int x, int y)
{
if(board[x][y]!=word[id])return false;
if(id==word.size()-1)return true;
set[x][y]=false;
bool ans=false;
auto I = board.size() ;
auto J = board[0].size();
if(x-1>=0 && x-1<I && set[x-1][y] && fi(board,word,id+1,x-1,y))
{
set[x][y]=true;
return true;
}
if(x+1>=0 && x+1<I && set[x+1][y] && fi(board,word,id+1,x+1,y))
{
set[x][y]=true;
return true;
}
if(y-1>=0 && y-1<J && set[x][y-1] && fi(board,word,id+1,x,y-1))
{
set[x][y]=true;
return true;
}
if(y+1>=0 && y+1<J && set[x][y+1] && fi(board,word,id+1,x,y+1))
{
set[x][y]=true;
return true;
}
set[x][y]=true;
return false;
}
bool exist(vector<vector<char>>& board, string word) {
set=vector<vector<bool>>(board.size(),vector<bool>(board[0].size(),true));
for(int i=0;i!=board.size();++i)
{
for(int j=0;j!=board[0].size();++j)
{
if(fi(board,word,0,i,j)) return true;
}
}
return false;
}
};
class Solution {
public:
int com(int x)
{
return x/100+x/10+x%10;
}
bool test(int x, int y, int k)
{
return com(x)+com(y)<=k;
}
vector<vector<int>>delta = {{-1,0},{1,0},{0,-1},{0,1}};
int movingCount(int m, int n, int k) {
if(m==0 && n==0)return 0;
vector<vector<bool>>ans(m,vector<bool>(n,false));
ans[0][0]=true;
deque<pair<int,int>>que;
int out=1;
que.push_back({0,0});
while(!que.empty())
{
for(auto d:delta)
{
int x=que.front().first+d[0];
int y=que.front().second+d[1];
if(x>=0 && x<m && y>=0 && y<n && !ans[x][y] && test(x,y,k))
{
ans[x][y]=true;
que.push_back({x,y});
++out;
}
}
que.pop_front();
}
return out;
}
};
class Solution {
public:
int com(int x)
{
return x/100+x/10+x%10;
}
bool test(int x, int y, int k)
{
return com(x)+com(y)<=k;
}
int count=0;
void fi(int m, int n, int i, int j, int k, vector<vector<bool>>&ans)
{
if(i<0 || i>=m || j<0 || j>=n || ans[i][j] || !test(i,j,k)) return;
ans[i][j]=true;
++count;
fi(m,n,i-1,j,k,ans);
fi(m,n,i+1,j,k,ans);
fi(m,n,i,j-1,k,ans);
fi(m,n,i,j+1,k,ans);
}
vector<vector<int>>delta = {{-1,0},{1,0},{0,-1},{0,1}};
int movingCount(int m, int n, int k) {
if(m==0 && n==0)return 0;
vector<vector<bool>>ans(m,vector<bool>(n,false));
fi(m,n,0,0,k,ans);
return count;
}
};
class Solution {
public:
vector<int>temp;
vector<vector<int>>ans;
void dfs(TreeNode* r,int target)
{
if(!r)return;
if(!r->left && !r->right)
{
if(target==r->val)
{
temp.push_back(r->val);
ans.push_back(temp);
temp.pop_back();
}
return;
}
temp.push_back(r->val);
dfs(r->left,target-r->val);
dfs(r->right,target-r->val);
temp.pop_back();
return;
}
vector<vector<int>> pathSum(TreeNode* root, int target) {
dfs(root,target);
return ans;
}
};
class Solution {
public:
pair<Node*, Node*> dfs(Node* r)
{
if(!r) return {nullptr,nullptr};
if(!r->left && !r->right) return{r,r};
auto left = dfs(r->left);
auto right = dfs(r->right);
r->left = left.second;
r->right = right.first;
if(left.second) left.second->right = r;
if(right.first) right.first->left = r;
auto le = left.first?left.first:r;
auto ri = right.second?right.second:r;
return {le,ri};
}
Node* treeToDoublyList(Node* root) {
if(!root) return nullptr;
auto p = dfs(root);
p.first->left=p.second;
p.second->right = p.first;
return p.first;
}
};
class Solution {
public:
int count=0;
int ans=0;
void kth(TreeNode* root, int k) {
if(!root) return;
kth(root->right,k);
++count;
if(count == k) ans=root->val;
if(count>k)return;
kth(root->left,k);
}
int kthLargest(TreeNode* root, int k) {
kth(root,k);
return ans;
}
};
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root)return 0;
deque<TreeNode*>que;
que.push_back(root);
int level=0;
while(!que.empty())
{
++level;
int size = que.size();
for(int i=0;i!=size;++i)
{
if(que[i]->left) que.push_back(que[i]->left);
if(que[i]->right) que.push_back(que[i]->right);
}
for(int i=0;i!=size;++i)
{
que.pop_front();
}
}
return level;
}
};
class Solution {
public:
bool ans=true;
int fi(TreeNode* r)
{
if(!r) return 0;
if(!ans) return 0;
auto left = fi(r->left);
auto right = fi(r->right);
ans = ans && (abs(left-right)<=1);
return max(left,right)+1;
}
bool isBalanced(TreeNode* root) {
auto p = fi(root);
return ans;
}
};
class Solution {
public:
int ans=0;
int sumNums(int n) {
n>=1&& sumNums(n-1);
ans+=n;
return ans;
}
};
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
剑指 Offer 68 - II. 二叉树的最近公共祖先
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return nullptr;
if(root==p || root==q) return root;
auto left = lowestCommonAncestor(root->left,p,q);
auto right = lowestCommonAncestor(root->right,p,q);
if(left && right) return root;
if(left) return left;
if(right) return right;
return nullptr;
}
};
class Solution {
public:
unordered_set<string>ans;
void fi(string s, int id)
{
if(id==s.size())
{
ans.insert(s);
return;
}
for(int i=id;i<s.size();++i)
{
swap(s[id],s[i]);
fi(s,id+1);
}
}
vector<string> permutation(string s) {
fi(s,0);
vector<string>out;
for(auto p:ans)
{
out.push_back(p);
}
return out;
}
};
分治算法
重建二叉树
class Solution {
public:
TreeNode* build(vector<int>& preorder, vector<int>& inorder, int l1, int r1, int l2, int r2) {
if(l1>r1) return nullptr;
TreeNode* root = new TreeNode(preorder[l1]);
int temp;
for(temp=l2;temp<=r2;++temp)
{
if(inorder[temp]==root->val) break;
}
root->left = build(preorder,inorder,l1+1,l1+temp-l2,l2,temp-1);
root->right = build(preorder,inorder,l1+temp-l2+1,r1,temp+1,r2);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return build(preorder,inorder,0,preorder.size()-1,0,preorder.size()-1);
}
};
快速幂
class Solution {
public:
double myPow(double x, long long n) {
long long p =n>0?n:-n;
double ans=1;
while(p>0)
{
if(p&1)
{
ans*=x;
}
x*=x;
p>>=1;
}
return n>0?ans:1.0/ans;
}
};
二叉树遍历
排序
本文来自博客园,作者:ETHERovo,转载请注明原文链接:https://www.cnblogs.com/etherovo/p/17643049.html