LeetCode 21 merges two ordered linked lists & 22 bracket generation

Wechat search bigsai, reply to the group, join the clock. Maintenance is not easy, welcome to like and support!

Insert picture description here

Merge two ordered lists

Combine two ascending linked lists into a new ascending linked list and return. The new linked list is composed by splicing all the nodes of the given two linked lists.

Example:

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

Analysis: The
idea, the idea of ​​this question is relatively simple, merge two ordered linked lists, you can create a new linked list, and then the two sub-linked lists are traversed and compared and inserted into the current smaller one.

The specific code is:

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
      ListNode value=new ListNode(Integer.MIN_VALUE);
      ListNode team=value;
      while (l1!=null||l2!=null) {
    
    
		if(l1==null)
		{
    
    
			team.next=l2;break;
		}
		else if (l2==null) {
    
    
			team.next=l1;break;
		}
		else {
    
    
			if(l1.val<l2.val)
			{
    
    
				team.next=new ListNode(l1.val);
				team=team.next;
				l1=l1.next;
			}
			else {
    
    
				team.next=new ListNode(l2.val);
				team=team.next;
				l2=l2.next;
			}
		  }
	   }
       return value.next;
  }

Insert picture description here

Of course, you can also set one of the linked lists L1 to be fixed, and then insert the other L2 to merge. The specific realization is that each time to the next step, if it needs to be inserted, another linked list is inserted in its entirety, which is equivalent to swapping L1 and L2 node positions.
Insert picture description here
The specific implementation is:

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
	  if(l1==null)return l2;
	  if(l2==null)return l1;
	  if(l1.val>l2.val)//l1更小
	  {
    
    
		  ListNode team=l1;
		  l1=l2;
		  l2=team;
	  }
      ListNode value=l1;
      while (l2!=null) {
    
    
		if(l1.next==null)
		{
    
    
			l1.next=l2;break;
		}
		else if (l1.next.val<l2.val) {
    
    
			l1=l1.next;
		}
		else {
    
    
			ListNode node=l1.next;
			l1=l1.next=l2;
			l2=node;
			//l1=l1.next;
			
		}
	   }
       return value; 
  }

Insert picture description here

Bracket generation

The number n represents the logarithm of generating parentheses. Please design a function to generate all possible and effective parentheses combinations.

Example:

Input: n = 3
Output: [
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()( )"
]

Analysis
This question may not have any ideas to solve as soon as you get it. You don't know what mathematical method to use, but you have to see all possible bracket combinations . All the two words are clearly search questions, which can be dfs or bfs, and dfs is used here.

dfs search needs to consider traversing all situations, as well as the initial and stopping conditions. We know that dfs is backtracking. If the string is frequently created and deleted, the efficiency is very low, so using the backtracking process to restore the string (StringBuilder) can greatly improve effectiveness. And specifically need to think like this:

  • N pairs of brackets, indicating that there are n (and n ), two numbers can be used to mark two numbers.
  • (The number cannot be less than )the number, otherwise the valid brackets are not satisfied.
  • If it is (used up, it can only be added ). If it is (not used up and the )number is not satisfied , then it can be added (or added currently ).
  • Pay attention to the scope, parameters and other issues

The specific implementation code is:

 List<String>list;
	public List<String> generateParenthesis(int n) {
    
    
		list=new ArrayList<String>();
		StringBuilder sBuilder=new StringBuilder();
		dfs(sBuilder,0,0,n);
		return list;	

    }
	private void dfs(StringBuilder sBuilder, int i, int j,int n) {
    
    
		if(j==n) {
    
    list.add(sBuilder.toString());return;}
		if(i<j)return;
		if(i<n)
		{
    
    
			sBuilder.append('(');
			dfs(sBuilder, i+1, j, n);
			sBuilder.deleteCharAt(i+j);
		}
		if(i>j)
		{
    
    
			sBuilder.append(')');
			dfs(sBuilder, i, j+1, n);
			sBuilder.deleteCharAt(i+j);
		}		
	}

Insert picture description here

In this way, the two questions are all over. If you feel okay, please like and support. Welcome to search and bigsaireply on WeChat to enter the group and check in together.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40693171/article/details/108432953