NC22 merges two ordered arrays

Topic link

1
  • Double pointer, but the double pointer needs to be fetched from behind
2, code,
public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        int index = A.length - 1;
        m--;
        n--;
        while(m >= 0 || n >= 0) {if(n < 0) {break;
            }else if(m < 0){
                A[index--] = B[n--];
            }else if(A[m] >= B[n]){
                A[index--] = A[m--];
            }else{ A[index--] = B[n--]; }}}}Copy the code

NC3 linked list central entry node

Topic link

1
  • The fast and slow hands, when they meet, start the fast hands from the beginning,
  • When two lists meet, one pointer points to where they met and the other to the head node of the list. When the two Pointers are moved backwards, it is judged that when they are the same, the node is the entry node from the linked list to the ring.
2, code,
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }} * /
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode slow = pHead;
        ListNode fast = pHead;
        while(fast ! =null&& fast.next ! =null){
            slow = slow.next;
            fast = fast.next.next;
            if(fast == slow){
                break; }}if(fast == null || fast.next == null) {return null;
        }
        slow = pHead;
        while(fast ! = slow){ fast = fast.next; slow = slow.next; }returnfast; }}Copy the code

NC52 parenthesis sequence

Topic link

1
  • The basic operation of the stack, the left parentheses stack, the right parentheses are compared to see if they match
2, code,
import java.util.*;


public class Solution {
    / * * * *@paramS string The character string *@returnBool Bool */
    public boolean isValid (String s) {
        Stack<Character> stack = new Stack<>();
        for(char c:s.toCharArray()){
            if(c == '(' || c == '{' || c == '['){
                stack.push(c);
            }else if(c == '] ') {if(stack.isEmpty()){
                    return false;
                }
                char tmp = stack.pop();
                if(tmp ! ='[') {return false; }}else if(c == '} ') {if(stack.isEmpty()){
                    return false;
                }
                char tmp = stack.pop();
                if(tmp ! ='{') {return false; }}else{
                if(stack.isEmpty()){
                    return false;
                }
                char tmp = stack.pop();
                if(tmp ! ='(') {return false; }}}returnstack.isEmpty(); }}Copy the code