面试题-算法篇

1 篇文章 0 订阅

一、单链表倒序排列

public static void revse(Node node){
        //拿到头结点
        Node head = node;
        //拿到当前循环的节点
        Node curNode = node.next;
        //头结点的next置为null
        head.next = null;
        //如果当前节点不为空
        while(curNode != null){
            //取出当前节点的下一个节点,作为下次循环遍历用
            Node next = curNode.next;
            //将当前节点的下一个节点,指向前一个节点
            curNode.next = head;
            //前一个节点指向当前节点
            head = curNode;
            //把取出的下一个节点作为当前节点,继续循环
            curNode = next;
        }
    }

         算法不难,网上案例比较多,但是就是在写的时候思路乱了,没写好,只把思路说出来了

二、两个数组交叉打印,线程同时start,必须从1开始打印

//题目要求必须打出1,2,3,4,5,6,7,8,9,10
private static int[] arr1 = new int[]{1,3,5,7,9};
private static int[] arr2 = new int[]{2,4,6,8,10};
//线程间通信用volatile修饰的关键字
private static volatile boolean status = false;
    public static void main(String[] args) {
        //使用jdk提供的锁
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        //开启线程并在循环开始的时候加锁
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i =0;i< arr2.length;i++){
                    lock.lock();
                    try {
                        //如果status!=true,那么当前线程阻塞(await会释放锁)
                        if(status != true){
                            condition.await();
                        }
                        //打印数据
                        System.out.println(arr2[i]);
                        //唤醒其他线程
                        condition.signalAll();
                        try {
                        //阻塞当前线程
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        lock.unlock();
                    }
                }
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < arr1.length; i++) {
                    lock.lock();
                    try {
                        //当前线程拿到锁之后打印出1
                        System.out.println(arr1[i]);
                        //status置为true
                        if(status ==false) {
                            status = true;
                        }
                        //唤醒正在阻塞的线程
                        condition.signalAll();
                        try {
                            //当前线程阻塞
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        lock.unlock();
                    }
                }
            }
        });
        thread1.start();
        thread2.start();

    }

           这道题考察的是对线程的等待唤醒机制,及线程间通信的理解

三,发红包算法,如何公平的将100元,分配给10个人(二倍均值法)

/**
     * 发红包算法,入参人数和总金额,返回,人和金额的map
     * @param person
     * @param totalMoney
     * @return
     */
    public static Map<Integer,Double> sendRedPackage(Integer person,Double totalMoney){
        if(person<=0 || totalMoney<=0.0){
            return new HashMap<>();
        }
        Map<Integer,Double> result = new HashMap<>();
        for(int i = 0;i<person;i++){
            if(i == person -1){
                result.put(i,new BigDecimal(totalMoney+"").setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
                break;
            }

            double avgMoney = totalMoney / (person-i);
            double money = new Random().nextDouble() * avgMoney * 2;
            money = new BigDecimal(money+"").setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            if(money<0.01){
                money = 0.01;
            }
            if(money>avgMoney){
                money =new BigDecimal(avgMoney+"").setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            }
            result.put(i,money);
            totalMoney = totalMoney - money;
        }
        return result;
    }

 

四,一个不知道长度的链表,输出链表中倒数第n个值

//实现思路,使用两个指针,步长为n,当第一个指针走到最后的时候,此时第二个指针的位置就是倒数第n个值
public static Node sout(Node node,int n){
        int i = 0;
        Node result =null;
        Node firstNode = node;
        Node secondNode = node;
        while (firstNode!=null){
            i++;
            if(i >= n){
                result = secondNode;
                secondNode = secondNode.next;
            }
            firstNode = firstNode.next;
            if(firstNode ==null){
                break;
            }
        }
        return result;
    }

 

五,快速排序算法及优化

     

    public static int[] arr = new int[]{4,2,3,7,6,5,0,9,1};
    public static void main(String[] args) {
        quickSort(arr,0,arr.length-1);
        System.out.println(arr);
    }
    public static void quickSort(int[] arr,int low ,int high){
        if(low > high){
            return;
        }
        int temp = arr[low];
        int i = low;
        int j = high;
        int t ;
        //将数组中按照基准值左右排开
        while(i<j){
            while(temp<=arr[j]&&i<j){
                j--;
            }
            while(temp>=arr[i]&&i<j){
                i++;
            }
            if(i<j){
                t = arr[i];
                arr[i]= arr[j];
                arr[j] = t;
            }
        }
        //交换基准值和第i或j的值
        arr[low] = arr[i];
        arr[i] = temp;
        //交换完成后,左右两边分别递归按照二分法思想做排序
        quickSort(arr,low,j-1);
        quickSort(arr,j+1,high);
    }

 

六,打印金额问题,给定的一个三位数,输出对应的中文金额,如365,输出为叁佰陆拾伍元整,尽可能多的处理一些边界值

         考察的是对事物思考的全面性,边界值的处理

//仅支持三位整数,四位及以上或者小数不支持
public static void printToString(Integer money){
        String[] capital = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        String[] integerUnit = { "", "拾", "佰", "仟", "万" };

        int length = String.valueOf(money).length();
        for(int i = length;i>0;i--){
            int every = money / (int) (Math.pow(10, (i - 1)));
            money = money % (int) (Math.pow(10, (i - 1)));
            if(every==0 && money==0){
                break;
            }
            System.out.print(capital[every]);//输出数字大写
            if(every ==0){
                continue;
            }
            System.out.print(integerUnit[i - 1]);//输出单位,
        }
        System.out.print("元整");
    }

 

      以上是关于美团和滴滴最近相关的算法面试题,有些当时只给出了思路,但是没有写出来,事后总结还是因为当时思路并

      不是太明确,导致写的时候思路不清晰只能给出大概的实现思路

      总结出来就是,看到算法题,第一步应该明确要考查的点,第二步就应该把整体的实现思路在脑子里过一遍,分析完后在

      动笔去写,算法题有很多,但是大多数的解决思路都差不太多,只要基础够好,临场发挥问题不太大,就算是最后没有实

       现,但是也要给出具体的实现思路

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值