Skip to content

Implement Queue using Stacks#32

Merged
rihib merged 1 commit intomainfrom
implement_queue_using_stacks
Sep 23, 2024
Merged

Implement Queue using Stacks#32
rihib merged 1 commit intomainfrom
implement_queue_using_stacks

Conversation

@rihib
Copy link
Copy Markdown
Owner

@rihib rihib commented Sep 6, 2024

Implement Queue using Stacksを解きました。レビューをお願い致します。

問題:https://leetcode.com/problems/implement-queue-using-stacks/
言語:Go

既に解いている方々:
colorbox/leetcode#15

構造体の初期化

MyQueue{}と初期化すると、メンバ自体はゼロ値で初期化されるため、今回の場合はthis.pushStack == nil && this.popStack == nilになる。そのためthis.pushStackのようにメンバにアクセスしてもヌルポにはならない。またnilスライスに対してappend操作を行うと、新しいスライスが作成されて要素が追加されるため、これも問題ない。

MyQueue{}MyQueue{pushStack: []int{}, popStack: []int{}}MyQueue{[]int{}, []int{}}とも書くことができ、どれも同じである。

new(MyQueue)と初期化すると、MyQueue{}と同様にメンバはゼロ値で初期化されるが、MyQueue{}MyQueue型の値が返されるのに対して、MyQueue型のポインタが返される。

q := MyQueue{}var q MyQueueは実質的な違いはない。どちらもメンバがゼロ値で初期化されたMyQueue型の値がqに格納される。

Copy link
Copy Markdown

@colorbox colorbox left a comment

Choose a reason for hiding this comment

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

コメントした箇所以外は良さそうに見えます

@rihib rihib merged commit b2d8dfd into main Sep 23, 2024
@rihib rihib deleted the implement_queue_using_stacks branch September 23, 2024 13:59
rihib added a commit that referenced this pull request Mar 31, 2025
rihib added a commit that referenced this pull request Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants