dfs
112:路径总和
解1:递归
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr) return false;
if(root->left==nullptr&&root->right==nullptr){
return targetSum==root->val;
}
return hasPathSum(root->left,targetSum-root->val)
||hasPathSum(root->right,targetSum-root->val);
}
};
解2:dfs
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr) return false;
queue<TreeNode*> proot;
queue<int> temp;
proot.push(root);
temp.push(root->val);
while(!proot.empty()){
TreeNode* now=proot.front();
int num=temp.front();
proot.pop();temp.pop();
if(now->left==nullptr&&now->right==nullptr){
if(num==targetSum)
{
return true;
}
continue;
}
if(now->left!=nullptr){
proot.push(now->left);
temp.push(now->left->val+num);
}
if(now->right!=nullptr){
proot.push(now->right);
temp.push(now->right->val+num);
}
}
return false;
}
};
线性表
16.最接近的三数之和
双指针
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
int ans = nums[0]+nums[1]+nums[2];
for(int i=0;i<nums.size();i++){
int start=i+1;
int end=nums.size()-1;
while(start<end){
int sum=nums[i]+nums[start]+nums[end];
if(abs(sum-target)<abs(ans-target)){
ans=sum;
}
if(sum>target) end--;
else if(sum<target) start++;
else{
return ans;
}
}
}
return ans;
}
};
27.移除元素
双指针
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int left=0,right=0;
for(int i=right;i<nums.size();i++){
if(nums[i]!=val){
nums[left]=nums[i];
left++;
}
}
return left;
}
};
86.分割链表
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* small = new ListNode(0);
ListNode* smallhead = small;
ListNode* large = new ListNode(0);
ListNode* largehead = large;
while(head!=0){
if(head->val<x){
small->next=head;
small=small->next;
}
else{
large->next=head;
large=large->next;
}
head = head->next;
}
large->next=nullptr;
small->next = largehead->next;
return smallhead->next;
}
};
101.对称二叉树
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return issame(root,root);
}
bool issame(TreeNode* root1,TreeNode* root2){
if(!root1&&!root2) return true;
if(!root1||!root2) return false;
return (root1->val==root2->val)&&issame(root1->left,root2->right)
&&issame(root1->right,root2->left);
}
};
bfs
1091.二进制矩阵中最短路径
class Solution {
public:
vector<vector<int>>dir={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if(grid[0][0]==1)return -1;
int n=grid.size();
queue<pair<int,int>>q;//建队列
q.push(make_pair(0,0));//把起点扔进去
int length=1;//层数
grid[0][0]=2;//起始点标记为阻塞
while(!q.empty()){
int l=q.size();
for(int i=0;i<l;i++){
int x=q.front().first;
int y=q.front().second;
q.pop();
if(x==n-1&&y==n-1)return length;//来到终点
for(int j=0;j<8;j++){//判断8个方向
int x1=x+dir[j][0];
int y1=y+dir[j][1];
if(x1<0||y1<0||x1>=n||y1>=n||grid[x1][y1])continue;
grid[x1][y1]=2;//标记
q.push(make_pair(x1,y1));
}
}
length++;//遍历完一层
}
return -1;
}
};
字符串
3.无重复字符的最长子串
class Solution {//采用滑动窗口和set
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> wind;
int end=-1,max_num=0;
for(int i=0;i<s.size();i++){
if(i!=0){
wind.erase(s[i-1]);
}
while(end+1<s.size()&&!wind.count(s[end+1]))
{
wind.insert(s[end+1]);
end++;
}
max_num=max(max_num,end-i+1);
}
return max_num;
}
};
30.串联所有单词的子串
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ret;
if(words.size() == 0)//判断words为空,因为下面用到了words[0]
return ret;
int word_size = words[0].size();
int word_num = words.size();
unordered_map<string,int> m1;//构造hashmap
for(int i=0;i<word_num;i++)
m1[words[i]]++;
unordered_map<string,int> m2;
for(int i=0; (i + word_size * word_num) <= s.size() ; i++){//截取的s符合题意
int j = 0;
for(j=i;j < (i + word_size * word_num) ; j=j+word_size){//分段判断
string temp_str = s.substr(j,word_size);
if(m1[temp_str] == 0){//m1中没有截取的串,直接跳出
break;
}else{
m2[temp_str]++;
if(m1[temp_str] < m2[temp_str])//重复次数过多,也跳出
break;
}
}
if(j == (i + word_size * word_num))//每一段都符合,则加入答案
ret.push_back(i);
m2.clear();//清空m2
}
return ret;
}
};
49.字母异位词分组
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map <string,vector<string>> mp;
for(string &str:strs){
string key =str;
sort(key.begin(),key.end());
mp[key].push_back(str);
}
vector<vector<string>> ans;
for(auto it=mp.begin();it!=mp.end();++it){
ans.push_back(it->second);
}
return ans;
}
};
链表
24.两交换链表中节点
递归
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* next;
if(head==nullptr||head->next==nullptr) return head;
next =head->next;
head->next=swapPairs(next->next);
next->next = head;
return next;
}
};
迭代
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode temp = dummyHead;
while (temp.next != null && temp.next.next != null) {
ListNode node1 = temp.next;
ListNode node2 = temp.next.next;
temp.next = node2;
node1.next = node2.next;
node2.next = node1;
temp = node1;
}
return dummyHead.next;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/solution/liang-liang-jiao-huan-lian-biao-zhong-de-jie-di-91/
来源:力扣(LeetCode)
背包
416.分割等和子集
class Solution {
public:
bool canPartition(vector<int>& nums) {
int n=nums.size();
if(n<2) return false;
int sum=0,maxnum=0;
for(int &num:nums){
sum+=num;
maxnum=max(maxnum,num);
}
if(sum&1) return false;
int target=sum/2;
if(target<maxnum) return false;
vector<int> dp(target+1,0);
dp[0]=true;
for(int i=0;i<n;i++){
int num=nums[i];
for(int j=target;j>=num;j--){
dp[j] |=dp[j-num];
}
}
return dp[target];
}
};
494.目标和
背包
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int target) {
int n=nums.size();
int sum_max=0;
for(int& num:nums){
sum_max+=num;
}
int diff=sum_max-target;
if(diff<0||diff%2!=0){
return 0;
}
int neg=diff/2;
vector<int> dp(neg+1,0);
dp[0]=1;
for(int i=0;i<n;i++){
int num=nums[i];
for(int j=neg;j>=num;j--){
dp[j]+=dp[j-num];
}
}
return dp[neg];
}
};
回溯
class Solution {
public:
int count = 0;
int findTargetSumWays(vector<int>& nums, int target) {
backtrack(nums, target, 0, 0);
return count;
}
void backtrack(vector<int>& nums, int target, int index, int sum) {
if (index == nums.size()) {
if (sum == target) {
count++;
}
} else {
backtrack(nums, target, index + 1, sum + nums[index]);
backtrack(nums, target, index + 1, sum - nums[index]);
}
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/target-sum/solution/mu-biao-he-by-leetcode-solution-o0cp/
来源:力扣(LeetCode)