常见简单面试算法

 

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();
        }

    }

}

  

posted @ 2019-06-20 20:40  苍穹冰尘  阅读(479)  评论(0编辑  收藏  举报
Live2D