每日算法--2023.3.2

1.剑指offer46--把数字翻译成字符串

class Solution {
    public int translateNum(int num) {
        List<Integer> container = new LinkedList<>();
        while(num!=0){
            container.add(num%10);
            num = num/10;
        } 
        int n = container.size();
        if(n == 0){
            return 1;
        }
        int[] nums = new int[n];
        for(int i = 0;i<n;i++){
            nums[i] = container.get(n-i-1); 
        } 
        int[] dp = new int[n+1];
        dp[0] = 1;dp[1] = 1;
        for(int i = 2;i<=n;i++){
            int a = nums[i-2], b = nums[i-1];
            int sum = a*10+b;
            if(sum>=10&&sum<=25){
                dp[i] = dp[i-1] + dp[i-2];
            }else{
                dp[i] = dp[i-1];
            }
        }
        return dp[n];
    }
}

2.面试题05.02.二进制数转字符串

class Solution {
    public String printBin(double num) {
        int cnt = 0;
        String res = new String("0.");
        while(cnt<32){
            num = num*2;
            if(num>1.0){
                num = num-1;
                res += "1";
            }else if(num == 1.0){
                 res += "1";
                 break;   
            }else{
                res += "0";
            }
            cnt++;    
        }
        if(num!=1.0){
            return new String("ERROR");
        }
        return res;
    }
}

 3.手写异步生产者消费者模式

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

class Message{
    int value;
    Message(int value) {
        this.value = value;
    }
}

class MesssaeQueue{
    private int capacity;
    private Deque<Message> queue;
    Object lock;
    MesssaeQueue(int capacity){
        this.capacity = capacity;
        queue = new LinkedList<>();
        lock = new Object();
    }
    public void put(Message message){
        synchronized (lock){
            while(queue.size() == capacity){
                System.out.println("消息队列满了");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.addLast(message);
            lock.notifyAll();
        }
    }
    public Message get(){
        synchronized (lock) {
            while (queue.isEmpty()) {
                System.out.println("暂时没有消息,请等待");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Message res = queue.removeFirst();
            lock.notifyAll();
            return res;
        }
    }

}
public class ProduceConsumer {
    public static void main(String[] args) {
        MesssaeQueue messsaeQueue = new MesssaeQueue(100);
        for(int i = 0;i<1000;i++){
            int id = i;
            new Thread(()->{
                messsaeQueue.put(new Message(id));
            }).start();
        }

        new Thread(()->{
            while(true){
                System.out.println(messsaeQueue.get().value);
            }
        }).start();

    }
}

  

  

posted @ 2023-03-02 21:47  lyjps  阅读(18)  评论(0)    收藏  举报