zoukankan      html  css  js  c++  java
  • 蘑菇街2018实习笔试题

    前面面试华为状态不佳,然后就挂了,华为不聊技术只聊人生,莫名其妙地没有进二面,应该是水平不够吧。等了很久,蘑菇街给了笔试机会。

    太久没有做笔试题了,突然来了一份笔试,想了很久,两道题都没有AC,还不知道有没有面试机会,暂且记录一下笔试题吧。都是宝贵的经验!都是常规题目!

    • 重复元素的全排列

    基本原理:比如输入123,则1开头,对[2,3]全排列,2开头,对[1,3]全排列,3开头,对[2,1]全排列。。。如此类推,构成一个树。数组中所有的值都和第一个值交换后,在对第一个数组后面的数组进行全排列。注意考虑重复元素的状况,先对数组进行排序,如果前后值相同且前一个值被交换过,则跳过当前值。

    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;
            }
        }
    }
    
    • 分糖果问题

    基本原理:定义一个全为1的数组mincandy,先从左往右遍历rate,如果rate[i]>rate[i-1],则mincandy[i]=mincandy[i-1]+1,然后从右往左遍历rate,如果rate[i]>rate[i+1]&&mincandy[i]<mincandy[i+1]+1,则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;
        }
    }
    
    
  • 相关阅读:
    写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
    工作1个月+1个星期
    矫正骨盆前倾!平坦小腹!解决腰痛!
    《非暴力沟通》
    了不起的盖茨比
    X战警系列
    Docker常用命令大全
    学习笔记12
    电子公文传输系统1个人贡献
    实验四 Web服务器2
  • 原文地址:https://www.cnblogs.com/bingo2-here/p/8981214.html
Copyright © 2011-2022 走看看