英文:
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 "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){}
}
}
}
答案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 "x", where x is an integer.
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;
}
}
}
}
For a complete reference of synchronized
, visit this
专注分享java语言的经验与见解,让所有开发者获益!
评论