常见简单面试算法

 

1>反转列表

 
public class Node {
      int value;
      Node next;
   }

   public Node reverse(Node head) {
         Node pReversedHead = null; //反转过后的单链表存储头结点
         Node before = null;
      while (head != null) {

         Node after = head.next;

         if (after == null) {
            pReversedHead = head;
         }

         head.next = before;
         before = head;
         head = after;

      }
      return pReversedHead;
   }

  

 

2>二叉树深度

 public class Node {
      int value;
      Node left;
      Node right;
   }

   public int level(Node root) {

      if(root == null){
         return 0;
      }

      int leftLevel = level(root.left);
      int rightLeve = level(root.right);

      return leftLevel > rightLeve ? leftLevel + 1 : rightLeve + 1;

   }

  

3>多线程循环打印ABC10次

//利用synchronized   
public class CyclePrint extends Thread{

    public CyclePrint(String printContent, Integer printCount) {
        this.printContent = printContent;
        this.printCount = printCount;
    }

    private String printContent;
    private Integer printCount;
    private static Integer calCount = 1;
    private static Object lock = new Object();


    public void print() throws Exception{
        synchronized (lock) {
            for (int i = 0; i < printCount && calCount<= 30;) {

                if(printContent.equals("A")){
                    if (calCount % 3 == 1) {
                        System.out.print(printContent);
                        calCount++;
                        i++;
                        lock.notifyAll();
                    }


                }else if(printContent.equals("B")){
                    if (calCount % 3 == 2) {
                        System.out.print(printContent);
                        calCount++;
                        i++;
                        lock.notifyAll();
                    }

                }else if(printContent.equals("C")){
                    if (calCount % 3 == 0) {
                        System.out.print(printContent);
                        calCount++;
                        i++;
                        lock.notifyAll();
                    }

                }

                if (i < printCount) {
                    lock.wait();
                }
            }
        }


    }

    @Override
    public void run() {
        try {
            print();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        CyclePrint A = new CyclePrint("A", 10);
        CyclePrint B = new CyclePrint("B", 10);
        CyclePrint C = new CyclePrint("C", 10);

        A.start();
        B.start();
        C.start();


    }

  

//利用ReentrantLock Condition
public class CyclePrint2 {
    public static void main(String[] args){
        ReentrantLock lock = new ReentrantLock();
        Condition A = lock.newCondition();
        Condition B = lock.newCondition();
        Condition C = lock.newCondition();


        Thread a = new Thread(()-> {
            lock.lock();
            for (int i = 0; i < 10; i++) {
                System.out.print("A");
                try {
                    B.signal();

                    if (i < 9) {
                        A.await();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            lock.unlock();
        });




        Thread b = new Thread(() -> {
            lock.lock();
            for (int i = 0; i < 10; i++) {
                System.out.print("B");
                try {
                    C.signal();

                    if (i < 9) {
                        B.await();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            lock.unlock();
        });




        Thread c = new Thread(() -> {
            lock.lock();
            for (int i = 0; i<10; i++){
                System.out.print("C");
                try {
                    A.signal();

                    if (i < 9) {
                        C.await();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            lock.unlock();
        });


        try {
            a.start();
            Thread.sleep(100);
            b.start();
            Thread.sleep(100);
            c.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

  

转载于:https://www.cnblogs.com/cangqiongbingchen/p/11061233.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值