2018华为校园招聘笔试题


总共三道题,第一道考察栈,第二道考察队列,第三道考察动态规划,半小时写完。

语言:Java

1.括号匹配

package campus2018;

import java.util.*;

public class ParenthesisMatching {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LinkedList<Character> stack = new LinkedList<>();
        String input = in.nextLine();
        in.close();
        boolean flag = true;
        for(int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{') 
                stack.push(ch);
            if(ch == ')') {
                if(!stack.isEmpty() && stack.peek() == '(') 
                    stack.pop();
                else {
                    System.out.print("false");
                    flag = false;
                    break;
                }
            } else if(ch == ']') {
                if(!stack.isEmpty() && stack.peek() == '[') 
                    stack.pop();
                else {
                    System.out.print("false");
                    flag = false;
                    break;
                }
            } else if(ch == '}') {
                if(!stack.isEmpty() && stack.peek() == '{') 
                    stack.pop();
                else {
                    System.out.print("false");
                    flag = false;
                    break;
                }
            } else
                continue;
        }
        if(flag)
            System.out.println("true");
    }
}

2.打印队列

package campus2018;

import java.util.*;

public class PrintQueue {
    private static class Pair {
        Integer task;
        final Integer index;
        Pair(int i, int t) {
            index = i;
            task = t;
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; i++)
            a[i] = in.nextInt();
        in.close();
        if(a.length == 0)
            System.out.println("Illegal input!");
        int pointer = 0;
        Pair[] output = new Pair[n];
        Queue<Pair> queue = new LinkedList<>();
        Queue<Pair> maxQueue = new PriorityQueue<>(
                new Comparator<Pair>() {
                    public int compare(Pair p1, Pair p2) {
                        if(p1.task > p2.task)
                            return -1;
                        else if(p1.task < p2.task)
                            return 1;
                        else
                            return 0;
                    }
                });
        for(int i = 0; i < n; i++) {
            Pair p = new Pair(i, a[i]);
            queue.offer(p);
            maxQueue.offer(p);
        }
        while(!queue.isEmpty()) {
            while(maxQueue.peek() != queue.peek()) {
                Pair temp = queue.poll();
                queue.offer(temp);
            }
            maxQueue.poll();
            output[pointer++] = queue.poll();
        }
        for(int i = 0; i < n-1; i++)
            System.out.print(output[i].index + ", ");
        System.out.print(output[n-1].index);
    }
}

3.平安果

package campus2018;

import java.util.Scanner;

public class EvaApple {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt(), n = in.nextInt();
        int[][] array = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                array[i][j] = in.nextInt();
        in.close();
        int[] dp = new int[n];
        int max = Integer.MIN_VALUE;
        for(int i = 0; i < n; i++)
            dp[i] = i == 0 ? array[0][i] : dp[i-1] + array[0][i];
        for(int i = 1; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(j == 0)
                    dp[j] += array[i][0];
                else {
                    dp[j] = Math.max(dp[j-1], dp[j]) + array[i][j];
                }
                if(dp[j] > max)
                    max = dp[j];
            }
        }
        System.out.println(max);
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM