Niuke.com - Brushing the Questions

ced485cbb11e458d81a746890b32cf3f.gif

Author: Rukawa Maple Knock Code

Blog Homepage: Rukawa Kaede's Blog

Column: Learn java with me

Quote: Stay hungry stay foolish

If you want to do good things, you must first sharpen your tools. Let me introduce you to a super powerful tool for winning offers from big manufacturers - Niuke.com

Click to register for free and brush the questions with me    

Article directory

Compare version numbers

 Inorder traversal of binary tree

Two numbers that appear only once in an array

merge interval


Compare version numbers

describe

When the Niuke project releases the project version, there will be a version number, such as 1.02.11, 2.14.4, etc.

Now give you 2 version numbers version1 and version2, please compare their sizes

The version number is composed of revision numbers, which are connected by a "." between revision numbers and revision numbers. A revision number may consist of multiple digits, and revision numbers may contain leading 0s and are legal. For example, 1.02.11, 0.1, 0.2 are all valid version numbers

Each version number contains at least 1 revision number.

Revision numbers are numbered from left to right, subscripts start at 0, the leftmost revision number is subscripted 0, the next revision number is subscripted 1, and so on.

Comparison rules:

1. When comparing version numbers, compare their revision numbers in order from left to right. When comparing revision numbers, just compare the integer values ​​ignoring any leading zeros. For example, the version numbers of "0.1" and "0.01" are the same

2. If the version number does not specify a revision number at a subscript, the revision number is regarded as 0. For example, "1.1" has a version number less than "1.1.1". Because the version number of "1.1" is equivalent to "1.1.0", the subscript of the 3rd revision number is 0, which is less than 1

3. version1 > version2 returns 1, if version1 < version2 returns -1, otherwise returns 0.

data range:

1 <= version1.length, version2.length <= 10001<=version1.length,version2.length<=1000

The revision numbers of version1 and version2 will not exceed the expression range of int, that is, the range of 32-bit integers.
Advanced: Time complexity O(n)O(n)

 

Knowledge points: strings, double pointers

answer:

import java.util.*;

public class Solution {
    
    public int compare (String version1, String version2) {
        // write code here
        String[] numsOfV1 = version1.split("\\."); // 记得这里分割的时候需要加两个斜杠
        String[] numsOfV2 = version2.split("\\.");
        
        int index = 0;
        
        while (index < numsOfV1.length && index < numsOfV2.length) {
            int num1 = Integer.parseInt(numsOfV1[index]);
            int num2 = Integer.parseInt(numsOfV2[index]);
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
            index ++;
        }
        
        while (index < numsOfV1.length) {
            int num1 = Integer.parseInt(numsOfV1[index]);
            if (num1 > 0) {
                return 1;
            }
            index ++;
        }
        
        while (index < numsOfV2.length) {
            int num2 = Integer.parseInt(numsOfV2[index]);
            if (num2 > 0) {
                return -1;
            }
            index ++;
        }
        
        return 0;
    }
}

 Inorder traversal of binary tree

describe

Given the root node root of a binary tree, return its inorder traversal result.

Data range: The number of nodes on the tree satisfies 0 \le n \le 10000≤n≤1000, and the value of each node on the tree satisfies 0 \le val \le 10000≤val≤1000
Advanced: Space complexity O(n)O (n), time complexity O(n)O(n)

 

Knowledge points: trees, recursion, breadth-first search (BFS)

 answer:

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        if(root == null)return new int[0];
        ArrayList<Integer> ans = new ArrayList<>();
        inOrder(root, ans);
        int[] res = new int[ans.size()];
        int i=0;
        for(int n: ans){
            res[i++] = n;
        }
        return res;
    }
    public void inOrder(TreeNode node, ArrayList<Integer> ans){
        if(node == null)return;
        inOrder(node.left, ans);
        ans.add(node.val);
        inOrder(node.right, ans);
    }
}

Two numbers that appear only once in an array

describe

In an integer array, except for two numbers that appear only once, all other numbers appear twice. Please write a program to find these two numbers that appear only once.

Data range: array length 2\le n \le 10002≤n≤1000, size of each number in the array 0 < val \le 10000000<val≤1000000
Requirements: space complexity O(1)O(1), time complexity Degree O(n)O(n)

Hint: The output is in non-descending order.

 

Knowledge points: bit operations, hashing

 answer:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        int tmp = 0, n = array.length;
        for(int i=0; i<n; i++){
            tmp ^= array[i];
        }
        int div = 1;
        while(tmp != 0){
            if((tmp & 1) == 1) div <<= 1;
            tmp >>= 1;
        }
        int a = 0, b = 0;
        for(int i=0; i<n; i++){
            if((array[i] & div) != 0) a ^= array[i];
            else b ^= array[i];
        }
        return new int[]{b, a};
    }
}

merge interval

describe

Given a set of intervals, merge all overlapping intervals.

Please ensure that the merged intervals are arranged in ascending order from the starting point of the interval.

Data range: interval group number 0 \le n \le 2 \times 10^50≤n≤2×105, the values ​​in the interval satisfy 0 \le val \le 2 \times 10^50≤val≤2×105

Requirements: space complexity O(n)O(n), time complexity O(nlogn)O(nlogn)

Advanced: space complexity O(val)O(val), time complexity O(val)O(val)

Knowledge points: sorting, arrays

answer:

import java.util.*;
/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
        if (intervals.size() == 0) {
            return new ArrayList<>();
        }
        Collections.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval o1, Interval o2) {
                if (o1.start != o2.start) {
                    return o1.start - o2.start;
                } else {
                    return o1.end - o2.end;
                }
            }
        });
        
        ArrayList<Interval> result = new ArrayList<>();
        result.add(intervals.get(0));
        int count = 0;
        for (int i = 1; i < intervals.size(); i++) {
            Interval o1 = intervals.get(i);
            Interval origin = result.get(count);
            if (o1.start > origin.end) {
                result.add(o1);
                count++;
            } else {
                result.remove(count);
                Interval s = new Interval(origin.start, o1.end);
                if (o1.end < origin.end) {
                    s.end = origin.end;
                }
                result.add(s);
            }
        }
        return result;
        
    }
}

"The sharing of this issue is here, remember to give the blogger a three-link, your support is the biggest driving force for my creation!

ced485cbb11e458d81a746890b32cf3f.gif

Guess you like

Origin blog.csdn.net/chenchenchencl/article/details/126611559