LeetCode开心刷题第十一天——21. Merge Two Sorted Lists(operate not familiar)22. Generate Parentheses...

21. Merge Two Sorted Lists
Easy
2319332FavoriteShare

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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Although this is a easy question,I'm not familiar with nodelist operator.
This code clear and nest.Perfect thing is that it add a node in front of the List,So many NULL
situation we needn't consider.So in the end we only need to return first->next,It's what we need
But this use to new ListNode each time it need toevaluation,It's not solve the input problem by
the root.As I have mentioned before,it does not embody the idea of programming from special to
general!
#include "test1.h"
#include <iostream>  
using namespace std;  
  
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
 
class Solution{
 
public:
    //LeetCode中的方法名称  
    ListNode* mergeTwoLists(ListNode* l1,ListNode* l2) {
    ListNode* head = new ListNode(0);//head指针是每次后移的  
    ListNode* firstNode = head;//需要确定好一个不变的头结点,然后返回这个头结点的next地址,才能返回完成的链表  
  
    while(l1!=NULL && l2!=NULL){  
        if(l1->val > l2->val){  
            head->next = l2;//这里只是确定好head->next是哪个结点,head还没有指向这个结点  
            l2 = l2->next;  
        }else{  
            head->next = l1;  
            l1 = l1->next;  
        }  
        head = head ->next;//head指向这个结点  
    }//while  
  
    head->next = l1?l1:l2;//遇到一个为空后,哪个不为空就next指向哪一个  
    //return head->next;//输出是:7 8 ,因为head指针也在往后移动  
    return firstNode->next;  
   }
};
ListNode* initList1(ListNode* head){//初始化第一个链表  
    ListNode* node1  = new ListNode(2);  
    head->next = node1;  
  
    ListNode* node2  = new ListNode(3);  
    node1->next = node2;  
  
    ListNode* node3  = new ListNode(6);  
    node2->next = node3;  
  
    ListNode* node4  = new ListNode(8);  
    node3->next = node4;  
    return head;  
}//initList1  
  
ListNode* initList2(ListNode* head){//初始化第二个链表  
    ListNode* node1  = new ListNode(1);  
    head->next = node1;  
  
    ListNode* node2  = new ListNode(2);  
    node1->next = node2;  
  
    ListNode* node3  = new ListNode(4);  
    node2->next = node3;  
  
    ListNode* node4  = new ListNode(5);  
    node3->next = node4;  
    return head;  
}//initList2  
  
//从头至尾遍历单链表  
void visitList(ListNode* head){  
    while(head!= NULL){  
        cout<<head->val<<endl;  
        head= head->next;  
    }  
}//visitList  
  
int main()    
{    
    //函数中初始化  
     cout<<"第一个链表:"<<endl;  
     ListNode* head1  = new ListNode(0);
     head1 = initList1(head1);  
     visitList(head1);  
  
     cout<<"第二个链表:"<<endl;  
     ListNode* head2  = new ListNode(-1);  
     head2 = initList2(head2);  
     visitList(head2);  
 
     cout<<"合并后的新链表:"<<endl;  
     Solution solution;//正确
     ListNode* head = solution.mergeTwoLists(head1,head2);//调用成员方法
     visitList(head);
  
     return 0;    
}
 
//第一个链表:
//0
//2
//3
//6
//8
//第二个链表:
//-1
//1
//2
//4
//5
//合并后的新链表:
//-1
//0
//1
//2
//2
//3
//4
//5
//6
//8
//请按任意键继续. . .
 
   

My solve:

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<vector>
#include<list>
#include<queue>
#include<algorithm>
#include<stack>
#include<map>
using namespace std;


  struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
  };

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* head=NULL;
        ListNode* next=NULL;
        if(l1==NULL)
            return l2;
        if(l2==NULL)
            return l1;
        if(l1->val<=l2->val)
        {
            head=l1;
            l1=l1->next;
        }
        else
        {
            head=l2;
            l2=l2->next;
        }
        next=head;
        while(true)
        {
            if(l1==NULL)
            {
                next->next=l2;  
                return head; 
            }
            if(l2==NULL)
            {
                next->next=l1;
                return head;
            }
            if(l1->val<=l2->val)
            {
                next->next=l1;
                next=next->next;
                l1=l1->next;
            }
            else
            {
                next->next=l2;
                next=next->next;
                l2=l2->next;
                
            }
        }
        return head;
    }
};
int main()
{
    ListNode *p,*head,*l,*head2,*l2;
    int n;
    p=(ListNode*)malloc(sizeof(ListNode*));
    head=p;
    head2=l2;
    p->val=0;
    p->next=NULL;
    char ch1;
    while((ch1=cin.get())!='\n' )
    {
        cin.putback(ch1);
        scanf("%d",&p->val);
        l=(ListNode*)malloc(sizeof(ListNode*));
        l->val=0;
        l->next=NULL;
        p->next=l;
        p=p->next;
    }
    while((ch1=cin.get())!='\n' )
    {
        cin.putback(ch1);
        scanf("%d",&p->val);
        l=(ListNode*)malloc(sizeof(ListNode*));
        l->val=0;
        l->next=NULL;
        p->next=l;
        p=p->next;
    }
    return 0;
}

In this way,you can use input to evaluate ListNode

//ll1 is the head,finally use it to return
ll1 = l1 = new ListNode(0); for (int i = 0; i < n1; i++) { l1->next = new ListNode(0); l1 = l1->next; scanf("%d", &;(l1->val)); }
 22. Generate Parentheses
Medium
2857 177 Favorite Share

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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]
In this code I use a new iterator const_iterator,but in fact normal iterator can do it.Only when
we need to traverse vector we can use this const one.
In fact,when I contact DFS&BFS,I thought it was very complex,maybe it's a bit complex in thinking
But when we use recursion execute DFS,you will find it's very clear and neat.
if(in>0) means still can output "("
if(in<out) means from this time,we can output ")"
because we cannot output")" from the start,only when we get this point can we start to output“)”
#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<vector>
#include<list>
#include<queue>
#include<algorithm>
#include<stack>
#include<map>
using namespace std;

class Solution {
public:
    vector<string> res;
    vector<string> generateParenthesis(int n) {
        getByDFS(n,n,"");
        return res;
    }
    void getByDFS(int in,int out,string a)
    {
        if(in==0&&out==0)
        {
            string one=a;
            res.push_back(one);
        }
        else
        {
            if(in>0)
            {
                getByDFS(in-1,out,a+"(");
            }
            if(in<out)
            {
                getByDFS(in,out-1,a+")");
            }
        }
    }
};

int main()
{
    Solution s;
    int n;
    vector<string> res;
    cin>>n;
    res=s.generateParenthesis(n);
    vector<string>::const_iterator iter=res.begin();
    for(;iter!=res.end();++iter)
    {
        cout<<*iter<<endl;
    }
    return 0;
}
if you use C11 this would be perfect   
for(auto i : res) { // process i cout << i <<endl; // this will print all the contents of *features*
}
22. Generate Parentheses
Medium
2857 177 Favorite Share

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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

转载于:https://www.cnblogs.com/Marigolci/p/11088949.html

猜你喜欢

转载自blog.csdn.net/weixin_34259559/article/details/93805381
今日推荐