美文网首页
2021-03-02 Leetcode笔记

2021-03-02 Leetcode笔记

作者: Cipolee | 来源:发表于2021-03-03 15:52 被阅读0次

双指针倒数第几个

public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        int n=0;
        ListNode *move=head,*lower=head;
//双指针可以起名head,begin等
        if (k==0)return NULL;
//鲁棒性,对于传参数的限制一定要先想好
        while(move!=NULL&&n<k)//条件设置想清楚
        {
        move=move->next;
        n++;
        }
        if(!move)return NULL;
        while(move!=NULL)
        {
            move=move->next;
            lower=lower->next;
        }
        return lower;
    }

冷静分析出代码写法,链表右移

 ListNode* rotateRight(ListNode* head, int k) {
        //借助循环链表的知识
        ListNode *rear=head;
        int n=0;
        while(rear->next!=nullptr)
        {
            rear=rear->next;
            n++;
            printf("hello\n");
        }
        rear->next=head;
        int i=0;
        k=k%n;
        while(i<=n-k)
        {
            head=head->next;
            rear=rear->next;
            i++;
        }
        rear->next=nullptr;
        return head;
    }

代码难写,多设变量,只要记得住就往多里设,以实现功能为baseline

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head==NULL)return NULL;
        Node *head2=new Node(head->val);
        Node *move1=head->next,*move2=head2;
  
        while(move1!=NULL)
        {
            
       Node*p=new Node(move1->val);
        move2->next=p;
        move2=move2->next;
        move1=move1->next;
        }
        Node *moveperci=head,*cur=head;
        Node *q=head2,*cur2=head2;
        while(cur!=NULL)
        {
            int i=0;
            while(moveperci!=cur->random)
            {
                i++;
                moveperci=moveperci->next;
            }
            while(i)
            {
                i--;
                cur2=cur2->next;
            }
            
            q->random=cur2;
            cur2=head2;
            moveperci=head;
            q=q->next;
            cur=cur->next;
        }
        return head2;
    }

常用变量,begin,move1、move2,cur1,cur2,head2等

二进制转10进制

 int i=0;
            while(head!=NULL)
            {
                i*=2;
                i+=head->val;
                head=head->next;
            }
            return i;

两个链表长度分别为L1+C、L2+C, C为公共部分的长度,按照楼主的做法: 第一个人走了L1+C步后,回到第二个人起点走L2步;第2个人走了L2+C步后,回到第一个人起点走L1步。 当两个人走的步数都为L1+L2+C时就两个家伙就相爱了

理解方法

关于平衡二叉树递归的思想

递归边界和递归形式

 int height(TreeNode* root)
    {
        if(root==NULL)return 0;
        if(root->left==NULL&&root->right==NULL)return 1;
        int a=height(root->left);
        int b=height(root->right);
        return a>b?a+1:b+1;
    }
    bool isBalanced(TreeNode* root) {
        if(root==NULL)return true;
        if (height(root->left)>height(root->right)+1||height(root->right)>height(root->left)+1)
        return false;
        return isBalanced(root->left)&&isBalanced(root->right);

    }

关于变量生存问题与树例题理解、如何在一个函数加入递归函数:再写一个函数

想要改变值只能加引用,只要根据运行其他函数生成结果都要加引用

//如何不被进栈出栈操作干扰,作为参数传入即可,防止作为全局变量一直改动。

  void createvector(TreeNode*root,vector<string>&vs,string s)
    {
    if(root!=nullptr)
    {
        s+=to_string(root->val);
        if(root->left==nullptr&&root->right==nullptr)
        {
            //cout<<s<<endl;
            vs.push_back(s);
        }
        else
        {
            s+="->";
        createvector(root->left,vs,s);
        createvector(root->right,vs,s);
        }
        
    }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string>vs;
        string spath="";
        createvector(root,vs,spath);
        return vs;
        }

带有返回值的无论如何也要返回,意外情况返回-1或者null也可以

返回左下角的数字

int height(TreeNode* root)
{
    if(root==NULL)return 0;
    if(root->left==NULL&&root->right==NULL)
    {
        return 1;
    }
    int a=height(root->left);
    int b=height(root->right);
    return a>b?a+1:b+1;
}
    int findBottomLeftValue(TreeNode* root) {
//最下层和最高层
if(root){
    if(root->left==nullptr&&root->right==nullptr)
    {
        return root->val;
    }
    if(height(root->left)>=height(root->right))
    return findBottomLeftValue(root->left);
    else
    return findBottomLeftValue(root->right);
}
return -1;
    }

爬台阶问题,优化,与特殊情况处理
进栈出栈模型,返回当下的值,可以使用数组存储

#include<iostream>
#include<cstring>
using namespace std;
const int Chushu=100003;
int f[100010];
int maxn(int x,int y)
{
    return  x>y?x:y;
}
int minn(int x,int y)
{
    return x<y?x:y;
}
int dfs(int x,int m)
{
    int p=0;
    if(x==0)return 1;
    if(f[x]!=-1)return f[x];

        for(int i=1;i<=maxn(minn(m,x),1);i++)
        {
           p=(p+dfs(x-i,m))%Chushu;
        }
        f[x]=p;
        return p;
}
int main()
{
    int n,m;
    memset(f,-1,sizeof(f));
    f[0]=1;
    cin>>n>>m;
    cout<<dfs(n,m)%Chushu;

}

动态规划
第i件物品装入或者不装入而获得的最大价值完全可以由前面i-1件物品的最大价值决定,暴力枚举忽略了这个事实。
背包问题,不简化使用二维数组从前向后推岛

相关文章

网友评论

      本文标题:2021-03-02 Leetcode笔记

      本文链接:https://www.haomeiwen.com/subject/ycotqltx.html