Print Zero Even Odd problem of leetcode shows error Time Limit Exceeded

huangapple 未分类评论47阅读模式
英文:

Print Zero Even Odd problem of leetcode shows error Time Limit Exceeded

问题

class ZeroEvenOdd {

    private int n;
    
    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    boolean z = true;
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i <= n; i++) {
            if(z) {
                printNumber.accept(0);
            }
            z = false;
            odd = !odd;
            try { notifyAll(); } catch(Exception e){}
            do { try { wait(); } catch(Exception e){ } } while(!z);
        }
    }
    boolean odd = false;
    public void even(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i <= n; i++) {
            do { try { wait(); } catch(Exception e){ } } while(z || odd);
            if(!odd && !z && i%2 == 0) {
                printNumber.accept(i);
            }
            z = true;
            try { notifyAll(); } catch(Exception e){}
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i <= n; i++) {
            do { try { wait(); } catch(Exception e){ } } while(z || !odd);
            if(odd && !z && i%2 == 1) {
                printNumber.accept(i);
            }
            z = true;
            try { notifyAll(); } catch(Exception e){}
        }
    }
}
英文:

I tried the following code for leetcode problem of Print Zero Even Odd problem.
It is showing me Time Limit Exceeded error while testing for simplest input.
Can anybody give some suggestions?
https://leetcode.com/problems/print-zero-even-odd

class ZeroEvenOdd {

    private int n;
    
    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs &quot;x&quot;, where x is an integer.
    boolean z = true;
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i &lt;= n; i++) {
            if(z) {
                printNumber.accept(0);
            }
            z = false;
            odd = !odd;
            try { notifyAll(); } catch(Exception e){}
            do { try { wait(); } catch(Exception e){ } } while(!z);
        }
    }
    boolean odd = false;
    public void even(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i &lt;= n; i++) {
            do { try { wait(); } catch(Exception e){ } } while(z || odd);
            if(!odd &amp;&amp; !z &amp;&amp; i%2 == 0) {
                printNumber.accept(i);
            }
            z = true;
            try { notifyAll(); } catch(Exception e){}
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i &lt;= n; i++) {
            do { try { wait(); } catch(Exception e){ } } while(z || !odd);
            if(odd &amp;&amp; !z &amp;&amp; i%2 == 1) {
                printNumber.accept(i);
            }
            z = true;
            try { notifyAll(); } catch(Exception e){}
        }
    }
   }

答案1

得分: 0

尝试使用synchronized:代码将会更加简洁。

class ZeroEvenOdd {
    private int n;
    
    public ZeroEvenOdd(int n) {
        this.n = n;
    }
    
    private enum STATUS{
        ZERO, ODD, EVEN;
    }
    
    private STATUS status = STATUS.ZERO;
    private int x = 0;
    private Object lock = new Object();

    // printNumber.accept(x) 输出“x”,其中 x 是一个整数。
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while(x <= n){
            switch(status){
                case ZERO:
                    synchronized(lock){
                        if(x < n){
                            printNumber.accept(0);    
                        }
                        if(x % 2 == 0){
                            status = STATUS.ODD;
                        }else{
                            status = STATUS.EVEN;
                        }
                        x++;
                    }
                    break;
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while(x <= n){
            switch(status){
                case EVEN:
                    if (x <= n){
                        printNumber.accept(x);
                    }
                    status = STATUS.ZERO;
                break;
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        
        while(x <= n){
            switch(status){
                case ODD:
                    synchronized(lock){
                        if(x <= n){
                            printNumber.accept(x);
                        }
                        status = STATUS.ZERO;
                    }
                    break;
            }
        }
    }
}

有关synchronized的完整参考,请访问此链接

英文:

Try using synchronized: the code will looks more simple

class ZeroEvenOdd {
    private int n;
    
    public ZeroEvenOdd(int n) {
        this.n = n;
    }
    
    private enum STATUS{
        ZERO, ODD, EVEN;
    }
    
    private STATUS status = STATUS.ZERO;
    private int x = 0;
    private Object lock = new Object();

    // printNumber.accept(x) outputs &quot;x&quot;, where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while(x&lt;=n){
            switch(status){
                case ZERO:
                    synchronized(lock){
                        if(x &lt; n){
                            printNumber.accept(0);    
                        }
                        if(x%2==0){
                            status = STATUS.ODD;
                        }else{
                            status = STATUS.EVEN;
                        }
                        x++;
                    }
                    break;
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while(x&lt;=n){
            switch(status){
                case EVEN:
                    if (x&lt;=n){
                        printNumber.accept(x);
                    }
                    status = STATUS.ZERO;
                break;
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        
        while(x&lt;=n){
            switch(status){
                case ODD:
                    synchronized(lock){
                        if(x &lt;= n){
                            printNumber.accept(x);
                        }
                        status = STATUS.ZERO;
                    }
                    break;
            }
        }
    }
}

For a complete reference of synchronized, visit this

huangapple
  • 本文由 发表于 2020年3月16日 02:41:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/60696390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定