Mogujie 2018 Internship Written Exam Questions

The previous interview with Huawei was not good, and then I hung up. Huawei didn't talk about technology but only about life. Inexplicably, it did not enter the second side. It must be that the level is not enough. After waiting for a long time, Mogujie gave a written test opportunity.

I haven't done the written test questions for a long time, and suddenly there is a written test. After thinking about it for a long time, there is no AC for both questions. I don't know if there is any opportunity for an interview. All are valuable experiences! It's all regular topics!

  • full array of repeating elements

Basic principle: For example, if you enter 123, start with 1, arrange all of [2,3], start with 2, arrange all of [1,3], start with 3, and arrange all of [2,1]. . . And so on to form a tree. After all the values ​​in the array are exchanged with the first value, the arrays after the first array are all permuted. Note that considering the situation of duplicate elements, sort the array first, and skip the current value if the previous value is the same and the previous value has been swapped.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class NoPermutation{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        char[] chars = s.toCharArray();
        //char[] chars = new char[]{'a','c','c'};
        Arrays.sort(chars);
        boolean[] booleans = new boolean[chars.length];
        List<StringBuilder> stringBuilders = new ArrayList<>();
        permute(chars,0,chars.length,stringBuilders,booleans);
        System.out.print(stringBuilders.toString());
    }
    public static void swap(char[] chars,int i,int j){
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
    }
    public static void permute(char[] chars,int m,int n,List<StringBuilder> stringBuilders,boolean[] booleans){
        if (m==n){
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < n; i++) {
                stringBuilder.append(chars[i]);
            }
            stringBuilders.add(stringBuilder);
            return;
        }
        for (int i = m; i < n; i++) {
            if (i>0&&chars[i]==chars[i-1]&&!booleans[i-1]) continue;
            booleans[i] = true;
            swap(chars,i,m);//关键
            permute(chars,m+1,n,stringBuilders,booleans);
            swap(chars,i,m);
            booleans[i] = false;
        }
    }
}
  • candy distribution problem

Basic principle: define an array mincandy with all 1s, first traverse rate from left to right, if rate[i]>rate[i-1], then mincandy[i]=mincandy[i-1]+1, then from Traverse rate from right to left, if rate[i]>rate[i+1]&&mincandy[i]<mincandy[i+1]+1, then mincandy[i] = mincandy[i+1]+1.

import java.util.Arrays;
import java.util.Scanner;

public class Candy {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String in = scanner.nextLine();
        String[] ratings = in.split(" ");
        System.out.print(mincandy(ratings));
    }
    public static int mincandy(String[] ratings){
        int[] rates = new int[ratings.length];
        for (int i = 0; i < ratings.length; i++) {
            rates[i] = Integer.parseInt(ratings[i]);
        }

        int[] mincandy = new int[ratings.length];

        Arrays.fill(mincandy,1);

        for (int i = 1; i < ratings.length; i++) {
            if (rates[i]>rates[i-1]){
                mincandy[i] = mincandy[i-1]+1;
            }
        }
        int sum = mincandy[rates.length-1];
        for (int i = rates.length-2; i >= 0 ; i--) {
            if (rates[i]>rates[i+1]&&mincandy[i]<mincandy[i+1]+1){
                mincandy[i] = mincandy[i+1]+1;
            }
            sum+=mincandy[i];
        }
        return sum;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325131370&siteId=291194637