Huawei 4.16 algorithm three questions

Keywords: Java Algorithm

Huawei 04.16 reply to three algorithm questions

1. Invert the substring between each pair of parentheses

Give a string s (containing only lowercase letters and parentheses).

Please reverse each pair of matching strings in parentheses layer by layer from inside to outside of parentheses, and return the final result.

Note that your results should not contain any parentheses.

Input: s = "(u(love)i)"
Output:"iloveu"
Explanation: reverse the rotor string first "love" ,Then invert the entire string.
package HuaWei;

import java.util.*;
import java.io.*;

public class Demo01_4_16 {
    public static void main(String[] args) {
        /*
        Inverts the substring between each pair of parentheses
        Idea:
        1: Reverse the characters in parentheses from inside to outside. First, define a string to store the temporary array, and create a StringBuilder object to facilitate string splicing
        2: If it is' (', put the characters in str on the stack, set STR to 0, and enter the next layer
        3: If it is') ', it indicates the end of this layer, reverses the character, takes out the top element of the stack and adds it to the head of str, indicating the last time
        4: If it is a character element, add the character element to str,
         */
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        Stack<String> stack = new Stack<String>();
       //A string is used to store temporary arrays and create a StringBuilder object to facilitate string splicing
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < s.length() ; i++) {
            char ch = s.charAt(i);//The characters in the string are found based on the index
            if (ch == '(') {        //If the character is' (', it means that it enters the next layer of parentheses, so put the previously saved string into the stack
                stack.push(sb.toString());
                sb.setLength(0);    //And empty sb
            } else if (ch == ')') { //If you traverse to ')', it means that this layer ends. You need to return to the previous layer, directly reverse the elements of this layer, and take out the characters placed on the stack at the upper layer and put them in the head of this layer
                sb.reverse();
                sb.insert(0,stack.pop());
            } else {
                sb.append(ch);
            }
        }
        System.out.println(sb.toString());
    }
}
Example:
(ed(et(oc))el)
leetcode

Second question

Establish a cloud database, the car reports its own operation information to the cloud, and the car generates speed data every 0.5s.

1. Periodic report: report every 30s, calculate the first speed after startup, and report the first frame.

2.AEB (automatic emergency braking) reporting: when the vehicle speed is 9 or more lower than the last generated speed, it is considered to trigger process A. if AEB status is maintained for 2 consecutive seconds, AEB reporting is triggered. The reporting contents include:

(1) all speed data in this AEB process, including data 2s before triggering AEB and data 2s after AEB

(2) if the data within the scope includes the data that has been reported periodically, repeat the reporting;

(3) if the data reported by two AEB S overlap, the overlapping data shall be reported once.

3. When the AEB reporting conditions are met, the periodic reporting will be suspended immediately, that is, the periodic reporting will not be reported even if it enters the periodic reporting cycle

Restart cycle reporting after reporting, and the new cycle starts from the last data reported by AEB.

Please output the content reported to the cloud according to the input speed information.

Once, the ABEflag is greater than or equal to 4, but the input data group ve ends and exits while, so it is necessary to add a judgment to while; then, it is necessary to add ABEflag < 4 after the judgment of count equal to 60.

Question 3

In the process of wireless device transmission, data often needs to be transferred through various relay devices. There is a certain transmission path, and one relay device is placed every 1km for data transfer. Now an array is used to describe the maximum transmission distance of all relay devices including the starting point. It is required to complete the minimum transfer times of signal transmission from the starting point to the end point.

Enter Description:
4
2 3 1 1
The first line represents the total number of transfer equipment, 4 sets
The second line indicates the maximum transmission capacity of each transfer equipment.

package HuaWei;

import java.util.Scanner;

/*
Title Description: the number of steps that can be jumped in each step from the starting point to the end point is represented by a one-dimensional array, and the minimum number of transitions from seven points to the end point
 Starting from the first step, select the longest distance that may jump in the next step as the boundary, and select the longest distance that can jump in each step within the scope of the next step. If the boundary is reached, step++
 */
public class Demo03_4_16 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        int end = 0;    //Define a boundary value to record the maximum boundary that can be jumped at each step
        int maxPosition = 0;    //Define a maximum position maxPosition to record the maximum position of the current zoneng jump
        int step = 0;           //Used to record the number of steps jumped
        for (int i = 0; i < nums.length-1; i++) {  //Only the penultimate bit of the array is traversed here because the number of steps has been + 1 when i= 0. If it is traversed to the end, i= end step will continue to add one or more steps
            maxPosition = Math.max(maxPosition,nums[i] + i); //Maximum value of the next step of each step
            if (i == end) {                        //Traverse to the boundary value of each step, and then deliver the maximum number of steps that can be reached by this layer to the boundary value of the next layer, the number of steps plus 1
                end = maxPosition ;
                step++;
            }
        }
        System.out.println(step);
    }
}

Posted by RJDavison on Tue, 23 Nov 2021 20:05:56 -0800