Jeff的隨手筆記

學習當一個前端工程師

0%

用LeetCode寫日記-Day4

今天的心情:
看到題目:感覺好簡單。
到中段:為什麼這樣寫不行。
結束後:媽的!我國文真的有夠差。

Day 4: Counter II

問題描述:

Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.

The three functions are:

  • increment() increases the current value by 1 and then returns it.
  • decrement() reduces the current value by 1 and then returns it.
  • reset() sets the current value to init and then returns it.

問題難度:Easy

問題限制:

  • 1000 <= init <= 1000
  • 0 <= calls.length <= 1000
  • calls[i] is one of “increment”, “decrement” and “reset”

我的解題思路

分析:

題目要我寫一個function,然後回傳三個功能不一的function,一個是+1、一個是-1及一個是原本的數字。

先return

1
2
3
4
5
return {
increment: () => {},
decrement: () => {},
reset: () => {},
}

之後設定裡面的邏輯:

1
2
3
4
5
6
7
8
9
10
11
12
13
let count = init
return {
increment: () => {
return ++count
},
decrement: () => {
return --count
},
reset: () => {
return init
},
}
};

第二個測試過了,但第一個測試沒過。

這邊我就有點搞不清楚為什麼會這樣,因此看了解答,看到了這段:

  • Firstly we can’t return the init value on reset because the reset function is intended to reset the counter to its initial value, not to return that value.

  • Secondly In the current implementation of the reset function, it updates the value of presentCount to init, which effectively resets the counter to its initial value. If we were to modify the reset function to also return the init value, it would no longer be performing its intended functionality.

  • Finally, it’s worth noting that the init value may not necessarily be the same as the current value of presentCount at the time the reset function is called. If the increment or decrement functions have been called multiple times, the value of presentCount would have changed from its initial value.

  • Thus, it’s more appropriate for the reset function to update the value of presentCount to the initial value specified when the counter was created, rather than to return that value.

  • 翻譯

    首先,我們不能在重置函數中傳回初始值,因為重置函數的目的是將計數器重置為其初始值,而不是傳回該值。

    其次,在目前的重置函數實作中,它將 presentCount 的值更新為 init,這實際上會將計數器重設為其初始值。 如果我們修改重置函數以傳回 init 的值,它將不再執行其預期的功能。

    最後,值得注意的是,init 的值不一定與呼叫重置函數時的 presentCount 的目前值相同。 如果多次呼叫增加或減少函數,presentCount 的值會從其初始值變更。

    因此,更合適的做法是使重置函數將 presentCount 的值更新為在建立計數器時指定的初始值,而不是傳回該值。

雖然還不是很懂,但我大概整理了一下:

重置函式的目標是將計數器的值恢復到初始值,而不是返回初始值。而且當我們如果有執行其他兩個函式,初始值會有所改變,就如同我剛剛的程式碼。

因此,最正確的做法應該是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var createCounter = function(init) {
let count = init
return {
increment: () => {
return ++count
},
decrement: () => {
return --count
},
reset: () => {
return count = init
},
}
};

在打完這段後,我終於發現問題在哪裡了….他的執行順序是:

1
2
3
counter.increment(); 
counter.reset();
counter.decrement();

答案的要求:計數器+1—>重置 計數器歸0 —> 計數器 -1.

我原本的程式碼:計數器 +1 —> 回傳初始值(但此時計數器還是+1的狀況) —> (計數器+1) -1

其他解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Counter {
constructor(init) {
this.init = init;
this.presentCount = init;
}

increment() {
this.presentCount += 1;
return this.presentCount;
}

decrement() {
this.presentCount -= 1;
return this.presentCount;
}

reset() {
this.presentCount = this.init;
return this.presentCount;
}
}

var createCounter = function(init) {
return new Counter(init);
};

結語

真的誤會大了…完全沒有理解題目的意思。
這次的教訓是真的要好好的理解題目!!!