码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 题解代码整理 21-25题

时间:2015-08-14 13:48:23      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:

Merge Two Sorted Lists


Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

合并两个有序链表

/**
 * 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 *ans,*head;
        if (l1==NULL && l2==NULL ) return NULL;
        
        if (l1==NULL)
        {
            head=l2;
            l2=l2->next;
        }
        else 
        if (l2==NULL)
        {
            head=l1;
            l1=l1->next;
        }
        else 
        {
            if (l1->val < l2->val)
            {
                head=l1;
                l1=l1->next;
            }
            else 
            {
                head=l2;
                l2=l2->next;
            }
        }
        ans=head;
        while (l1!=NULL || l2!=NULL)
        {
            if (l1==NULL)
            {
                ans->next=l2;
                ans=l2;
                l2=l2->next;
            }
            else 
            if (l2==NULL)
            {
                ans->next=l1;
                ans=l1;
                l1=l1->next;
            }
            else 
            {
                if (l1->val < l2->val)
                {
                    ans->next=l1;
                    ans=l1;
                    l1=l1->next;
                }
                else
                {
                    ans->next=l2;
                    ans=l2;
                    l2=l2->next;
                }
            }
        }
        return head;
    }
};

Generate Parentheses

 

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"


输出所有的括号匹配
class Solution 
{
vector<string>ans;
public:
    vector<string> generateParenthesis(int n) 
    {
        
        string mark="";
        dfs(mark,n,n);
        return ans;
    }
private:
    void dfs(string mark,int x,int y)
    {
        if (x+y==0) 
        {
            ans.push_back(mark);
            return ;
        }
        if (x!=0) 
            dfs(mark+"(",x-1,y);
        if (x<y)
            dfs(mark+")",x,y-1);
    }
};

Merge k Sorted Lists

 
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
对K个有序链表排序
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class ListNodeCompare
{
public:
    bool operator()(ListNode *l1,ListNode *l2)
    {
        return l1->val > l2->val;
    }
};
class Solution 
{
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) 
    {
        ListNode *ans,*head,*p;
        priority_queue<ListNode*,vector<ListNode*>,ListNodeCompare>q;
        if (lists.size()==0) return NULL;
        ans=(ListNode*)malloc(sizeof(ListNode));
        ans->next=NULL;
        head=ans;   
        for (int i=0;i<lists.size();i++)
        if (lists[i]!=NULL)
            q.push(lists[i]);
            
        while (!q.empty())
        {
            p=q.top();
            q.pop();
            ans->next=p;
            ans=ans->next;
            if (p->next!=NULL)
                q.push(p->next);
        }
        return head->next;    
    }
};

Swap Nodes in Pairs

 

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.


对链表中每两个节点交换一下
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution 
{
public:
    ListNode* swapPairs(ListNode* head) 
    {
        ListNode *p1,*pre,*p2;
        if (head==NULL || head->next==NULL) return head;
        
        p1=head;
        pre=head;
        
        while (1)
        {
            p2=p1->next;
            p1->next=p2->next;
            p2->next=p1;
            if (pre==head)
                head=p2;
            else 
                pre->next=p2;
                
            pre=p1;
            p1=p1->next;
            if (p1==NULL || p1->next==NULL) break;
        }
        return head;
    }
};


Reverse Nodes in k-Group

 

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

对链表中每k个节点进行一次反转

class Solution 
{
public:
    ListNode* reverseKGroup(ListNode* head, int k) 
    {
        int n=k;
        int len=0;
        ListNode *p=head;
        if (head==NULL || head->next==NULL || k<=1)     return head;
        while (p)
        {
            len++;
            p=p->next;
        }
        if (n>len) return head;
        ListNode *q=head;
        while (n--)
        {
            ListNode *ne=q->next;
            q->next=p;
            p=q;
            q=ne;
        }
        
        if (len-k>=k) 
            head->next= reverseKGroup(q,k);
        else
            head->next=q;
        return p;
    }
};



 

版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 题解代码整理 21-25题

标签:

原文地址:http://blog.csdn.net/u011932355/article/details/47658597

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!