简单的leetcode题

简单的leetcode题

环绕字符串中唯一的子字符串

把字符串 s 看作是\(“abcdefghijklmnopqrstuvwxyz”\)的无限环绕字符串,所以 s 看起来是这样的\(“zabcdefghijklmnopqrstuvwxyzabcdefghijklm....”\)
现在我们有了另一个字符串 p 。你需要的是找出 s 中有多少个唯一的 p 的非空子串,尤其是当你的输入是字符串 p ,你需要输出字符串 s 中 p 的不同的非空子串的数目。

注意: p 仅由小写的英文字母组成,p 的大小可能超过 10000。
分析:我们以26字母分别结尾为划分,这样可以保证子字符串不重复。答案就是每个字母结尾的最大子字符串长度加起来。

class Solution {
public:
    int findSubstringInWraproundString(string p) {
        if(p.size()==0)
            return 0;
        int dp[27]={0};//以每个字母结尾的字串长度
        int last=1;//此时结尾处的长度
        dp[p[0]-'a']=1;//边界条件
        for(int i=1;i<p.size();i++)
        {
            if(dp[p[i]-'a']==0)
                dp[p[i]-'a']=1;
            int j=i-1;
            if((p[i]-p[j]+26)%26==1)//判断条件
            {
                if(last+1>=dp[p[i]-'a'])//出现更加长的子串
                {
                    dp[p[i]-'a']=last+1;
                }
                last++;
            }
            else
                last=1;
        }
        int ans=0;
        for(int i=0;i<26;i++)
        {
            ans+=dp[i];
        }
        return ans;
    }
};

二叉树的右视图

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

非递归版本

分析:二叉树层次遍历,然后只加最后一个点的值即可。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
       vector<int>ans;
       queue<TreeNode* >Q;
         if(root)
            Q.push(root);
        while(Q.size())
        {
            int n=Q.size();
            for(int i=0;i<n;i++)
            {
                TreeNode* cur=Q.front();
                Q.pop();
                if(i==n-1)
                    ans.push_back(cur->val);
                if(cur->left)
                    Q.push(cur->left);
                if(cur->right)
                    Q.push(cur->right);
            }
        }
        return ans;
    }
};

递归版本

分析:当递归到第k层时(设根节点为第0层),如果这时ans数组里面有k个元素,加把此时的节点的值加入ans数组中。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
       vector<int>ans;
       dfs(root,ans,0);
        return ans;
    }
    void dfs(TreeNode* root,vector<int> &ans,int cnt)
    {
        if(!root)
            return ;
        if(ans.size()==cnt)
            ans.push_back(root->val);
        dfs(root->right,ans,cnt+1);
        dfs(root->left,ans,cnt+1);
    }
};

合并两个有序链表

递归版本

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1==NULL)
            return l2;
        else if(l2==NULL)
            return l1;
        else if(l1->val<l2->val)
            {
                l1->next=mergeTwoLists(l2,l1->next);
                return l1;
            }
        else 
        {
            l2->next=mergeTwoLists(l1,l2->next);
            return l2;
        }
    }
};

非递归版本

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* prehead = new ListNode(-1);
        ListNode* prev = prehead;
        while(l1&&l2)
        {
            if(l1->val<l2->val)
            {
                prev->next=l1;
                l1=l1->next;
            }
            else
            {
                prev->next=l2;
                l2=l2->next;
            }
            prev=prev->next;
        }
        prev->next=l1!=NULL?l1:l2;
        return prehead->next;
    }
};

排序链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
       ListNode Head(-1);
       Head.next=head;
       ListNode* p=head;
       int len=0;
       while(p)
       {
           len++;
           p=p->next;
       }
       for(int siz=1;siz<len;siz=siz*2)
       {
           ListNode* cur=Head.next;
            ListNode* tail=&Head;
            while(cur)
            {
                ListNode* left=cur;
                ListNode* right=cut(left,siz);
                cur=cut(right,siz);
                tail->next=merge(left,right); 
                while(tail->next)
                {
                     tail= tail->next;
                }
            }
           
       }
       return Head.next;
}
    ListNode* cut(ListNode* p,int n)
    {
        while(p&&--n)
        {
            p=p->next;
        }
        if(!p)
            return NULL;//返回空指针
        ListNode* r=p->next;
        p->next=NULL;
        return r;
    }
    ListNode* merge(ListNode* l1,ListNode* l2)
    {
        ListNode head(-1);
        ListNode* p=&head;
        while(l1&&l2)
        {
            if(l1->val<l2->val)
            {
                p->next=l1;
                l1=l1->next;
            }
            else
            {
                p->next=l2;
                l2=l2->next;
            }
            p=p->next;
        }
        p->next=(l1?l1:l2);
        return head.next;
    }
};
posted @ 2019-11-19 01:45  hh13579  阅读(145)  评论(0编辑  收藏  举报