Weekly leetcode - NC13/NC70/NC62/NC73/NC112/NC96/NC34/62/NC57/7/NC16/NC25/237/82/NC9/NC55

Posted by tarlejh on Tue, 25 Jan 2022 18:37:53 +0100

simple

Niuke Tiba - NC13 maximum depth of binary tree

Find the maximum depth of a given binary tree. Depth refers to the number of nodes on the path from the root node of the tree to any leaf node. The maximum depth is the maximum depth of all leaf nodes.

public class Solution {
    public int maxDepth (TreeNode root) {
        if(root==null) return 0;
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return Math.max(leftDepth,rightDepth)+1;
    }
}

Ranking of Niuke Tiba NC70 single linked list

Given an unordered single linked list with n nodes, it is sorted in ascending order.

public class Solution {
    public ListNode sortInList (ListNode head) {
        if(head==null || head.next==null){
            return head;
        }

        // Find the middle node of the linked list
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode head2 = slow.next;
        slow.next = null;
        ListNode list1 = sortInList(head);
        ListNode list2 = sortInList(head2);
        // Merge two linked lists
        return mergeSortList(list1,list2);
    }

    private ListNode mergeSortList(ListNode list1, ListNode list2) {
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (cur1!=null && cur2!=null){
            if(cur1.val<cur2.val){
                cur.next = cur1;
                cur1 = cur1.next;
            }else{
                cur.next = cur2;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        if (cur1!=null){
            cur.next = cur1;
        }
        if (cur2!=null){
            cur.next = cur2;
        }
        return dummy.next;
    }
}

Niuke Tiba - NC62 judge whether it is a balanced binary tree

Enter a binary tree with n nodes to judge whether the binary tree is a balanced binary tree.
Balanced Binary Tree has the following properties: it is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and both the left and right subtrees are a Balanced Binary Tree.

/**
 * It is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and both the left and right subtrees are a balanced binary tree.
 */
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        // depth()
        if(root==null) return true;
        return Math.abs(depth(root.left)-depth(root.right))<=1 && IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
    }

    private int depth(TreeNode root) {
        if(root==null) return 0;
        int leftDepth = depth(root.left);
        int rightDepth = depth(root.right);
        return Math.max(leftDepth,rightDepth)+1;
    }
}

Niuke Tiba - numbers that appear more than half times in NC73 array

Give an array with a length of n. a number in the array appears more than half the length of the array. Please find out this number.
For example, enter an array of length 9 [1,2,3,2,2,2,5,4,2]. Since the number 2 appears five times in the array, more than half the length of the array, 2 is output.

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        int count = 0;
        int mainNum = 0;
        for(int i=0;i<array.length;i++){
            if(count==0){
                mainNum = array[i];
            }
            if(array[i]==mainNum){
                count++;
            }else {
                count--;
            }
        }
        return mainNum;
    }
}

Niuke Tiba NC112 binary conversion

Give A decimal number M and the decimal number N to be converted. Convert decimal number M to N-ary number. When N is greater than 10, capital letters shall be used in the result to indicate A bit greater than 10. For example, 'A' indicates that this bit is 10 and 'B' indicates that this bit is 11.


How do I get every bit of a decimal system?
For example: for decimal number 178, we want to obtain 1, 7 and 8. First use 178 to modulo 10 to obtain single digit 8, then divide 178 by 10, and then touch 10 to obtain 10 digit 7
Therefore, as long as the number is modulo 10 and then divided by 10 every time, each bit of the number can be obtained. Similarly, for n-ary numbers, as long as the number is modulo N and then divided by n every time, each bit of n-ary can be obtained.

Note: the title says that M is a 32-bit integer, so it may be negative and 0. If it is 0, it will be returned directly. If it is negative, it will be converted into a positive number first, and then the sign bit will be added.

public class Solution {
    /**
     * Binary conversion
     * @param M int Integer given integer
     * @param N int Integer converted to base
     * @return string character string
     */
    public String solve (int M, int N) {
        String s = "0123456789ABCDEF";
        if(M==0){
            return "0";
        }
        StringBuilder stringBuilder = new StringBuilder();
        // Used to identify whether it is a negative number
        boolean flag = false;
        if(M<0){
            // Represents a negative number
            flag = true;
            // If it is a negative number, replace it with a positive number, and finally add the sign bit
            M = -M;
        }
        while (M != 0){
            stringBuilder.append(s.charAt(M % N));
            M = M / N;
        }
        if(flag){
            stringBuilder.append("-");
        }
        return stringBuilder.reverse().toString();
    }
}

Niuke Tiba - NC96 judges whether a linked list is palindrome structure

Given a linked list, please judge whether the linked list is palindrome structure. Palindrome means that the positive and negative order of the string are completely consistent.

public class Solution {
    public boolean isPail (ListNode head) {
        if(head.next==null){
            return true;
        }
         // 1. Find the intermediate node of the linked list
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        
        ListNode cur = slow.next;
        slow.next = null;
        ListNode head2 = reverseNode(cur);

        // 2. Comparison linked list
        while (head!=null && head2!=null){
            if(head.val==head2.val){
                head = head.next;
                head2 = head2.next;
            }else{
                return false;
            }
        }
        return true;
    }

    private ListNode reverseNode(ListNode cur) {
        ListNode pre = null;
        while (cur!=null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

Niuke Tiba - NC34 seeking path

A robot in m × The upper left corner (starting point) of the n-size map. The robot can move down or right at a time. The robot will reach the lower right corner of the map (end point).
How many different paths can you take from the beginning to the end?

public class Solution {
    /**
     *
     * @param m int integer 
     * @param n int integer 
     * @return int integer
     */
    public int uniquePaths (int m, int n) {
         int[][] dp = new int[m][n];
         for(int i=0;i<m;i++){
             dp[i][0] = 1;
         }
         for(int i=0;i<m ;i++){
             dp[0][i] = 1;
         }
         for(int i=1;i<m;i++){
             for(int j=1;j<n;j++){
                 dp[i][j] = dp[i-1][j]+ dp[i][j-1];
             }
         }
         return dp[m-1][n-1];
    }
}

leetcode - 62. Different paths

A robot is located in the upper left corner of an m x n grid (the starting point is marked "Start" in the figure below). The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid (marked "Finish" in the figure below). How many different paths are there altogether?

public class Solution {
    /**
     *
     * @param m int integer 
     * @param n int integer 
     * @return int integer
     */
    public int uniquePaths (int m, int n) {
         int[][] dp = new int[m][n];
         for(int i=0;i<m;i++){
             dp[i][0] = 1;
         }
         for(int i=0;i<n;i++){
             dp[0][i] = 1;
         }
         for(int i=1;i<m;i++){
             for(int j=1;j<n;j++){
                 dp[i][j] = dp[i-1][j]+ dp[i][j-1];
             }
         }
         return dp[m-1][n-1];
    }
}

Niuke Tiba - NC57 reverse number

Given a 32-bit signed integer num, the number part in num is reversed, and finally the result of inversion is returned
1. Only the digital part is reversed, and the symbol bit part is not reversed
2. After inversion, the integer num exceeds the range of 32-bit signed integers [− 231, 231 − 1], and returns 0
3. Suppose this question is not allowed to store 64 bit integers (signed or unsigned, that is, C + + cannot use long, Java cannot use long, etc.)

class Solution {
    public int reverse(int x) {
        long res = 0;
        while (x!=0){
            res = res*10 + res%10;
            x = x/10;
        }
        if(res>Integer.MAX_VALUE || res<Integer.MIN_VALUE){
            return 0;
        }
        return (int) res;
    }
}

leetcode - 7. Integer inversion

Give you a 32-bit signed integer x and return the result after reversing the number part in X. If the inverted integer exceeds the range of 32-bit signed integers [− 2 ^ 31, 2 ^ 31 − 1], it returns 0. Assume that the environment does not allow the storage of 64 bit integers (signed or unsigned).

class Solution {
    public int reverse(int x) {
        long res = 0;
        while (x!=0){
            res = res * 10 +x%10;
            x=x/10;
        }
        if(res>Integer.MAX_VALUE || res <Integer.MIN_VALUE){
            return 0;
        }
        return (int) res;
    }
}

Niuke Tiba NC16 symmetric binary tree

Given a binary tree, judge whether it is its own mirror image (i.e. whether it is symmetrical)

public class Solution {
    boolean isSymmetrical(TreeNode pRoot) {
        if(pRoot==null){
            return true;
        }
        return check(pRoot.left,pRoot.right);
    }

    private boolean check(TreeNode p, TreeNode q) {
        if(p==null && q==null){
            return true;
        }
        if(p==null || q==null){
            return false;
        }
        return p.val==q.val && check(p.left,q.right) && check(p.right,q.left);
    }
}

Niuke Tiba - NC25 delete duplicate elements in the ordered linked list - I

Delete the duplicate elements in the given list (the elements in the list are ordered from small to large), so that all elements in the list appear only once

public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        if(head==null){
            return head;
        }
        ListNode cur = head;
        while (cur.next!=null){
            if(cur.val==cur.next.val){
                cur.next = cur.next.next;
            }else {
                cur = cur.next;
            }
        }
        return head;
    }
}

leetcode - 237. Delete nodes in the linked list

Please write a function to delete a specific node in the single linked list. When designing the function, you should pay attention to that you cannot access the head node of the linked list. You can only directly access the node to be deleted. The topic data ensures that the node to be deleted is not the end node.

class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

leetcode - 82. Delete duplicate Element II in the sorting linked list

Given the head of a sorted linked list, delete all nodes with duplicate numbers in the original linked list, leaving only different numbers. Returns the sorted linked list.

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = dummy;
        while (cur.next!=null && cur.next.next!=null){
            if(cur.next.val==cur.next.next.val){
                int val = cur.next.val;
                while (cur.next!=null && cur.next.val==val){
                    cur.next = cur.next.next;
                }
            }else{
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}

Topics: data structure leetcode linked list