Skip to content

20. valid parentheses#5

Open
lightbanana wants to merge 7 commits into
mainfrom
20.-Valid-Parentheses
Open

20. valid parentheses#5
lightbanana wants to merge 7 commits into
mainfrom
20.-Valid-Parentheses

Conversation

@lightbanana

Copy link
Copy Markdown
Owner

Comment thread memo.md

## Step 1
### 考えたこと
スタックに([{が来るたびに一つずつ積んでいって、)]}が来るたびに一つずつスタックからpopしていく。スタックの名前がスタックなのは少し嫌だと思ったが、他に思いつかなかったのでとりあえずstackにした。時間計算量はO(n)、空間計算量もO(n)。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!これから意識するようにしてみます。
Pythonでのこのような単純な処理であれば1回0.1us-1us程度と考えて、O(n)でsのlengthが最大10^4のため、最悪でも100us-1000usの範囲内と見積もるイメージでやってみようと思います。
実際にsのlengthを最大にして計測したところ、260usだったため見積もりの範囲内でした。

Comment thread memo.md
last = open_brackets.pop()
if open_to_close[last] != c:
return False
return (len(open_brackets) == 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        return not open_brackets

のほうが空かどうかを判定しているという意図が伝わりやすいと思います。

こちらもご参照ください。
mamo3gr/arai60#6 (comment)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8でも同様の記述がなされていますね。

https://peps.python.org/pep-0008/

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

Correct:

    if not seq:
    if seq:

Wrong:

    if len(seq):
    if not len(seq):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。PEP 8のほうも確認させていただきました。何度か指摘されているので、まさにソフトウェアエンジニアの常識の典型例なのかなと感じています。

Comment thread memo.md
if not last or bracket != open_to_close[last]:
return False
return (len(open_brackets) == 1) # Check only the sentinel left
```

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体的にとても読みやすかったです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants