Closed
Conversation
colorbox
reviewed
Sep 3, 2024
GC がそれなりに重いからだろうと思いますね。 |
oda
reviewed
Sep 6, 2024
| if root == nil { | ||
| return treeBalance{true, 0} | ||
| } | ||
| left, right := checkBalanceStep3(root.Left), checkBalanceStep3(root.Right) |
oda
reviewed
Sep 6, 2024
| } | ||
| left, right := checkBalanceStep3(root.Left), checkBalanceStep3(root.Right) | ||
| isBalanced := left.isBalanced && right.isBalanced && | ||
| math.Abs(float64(left.height-right.height)) <= 1.0 |
There was a problem hiding this comment.
math.Abs(float64()) <= 1 も整数のまま処理したいですね。一回変数を別にいれておいて、-1と1と比較するだけですから。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Balanced Binary Treeを解きました。レビューをお願い致します。
問題:https://leetcode.com/problems/balanced-binary-tree/
言語:Go
すでに解いている方々:
Kitaken0107/GrindEasy#16
colorbox/leetcode#13
NobukiFukui/Grind75-ProgrammingTraining#19
Mike0121/LeetCode#4
値渡しについて
Go Decisionsに下記のように書かれている通り、大きなサイズの構造体や、小さいサイズの構造体だがサイズが大きくなるものでない限り、単に数バイト節約するためにポインタを渡すのは良くない。
参照渡しでは、複数のスコープから参照されることからヒープ上にデータが確保されるため、その結果、GCがそのメモリを追跡し、不要になったタイミングで解放する必要が出てくる。GCはそれなりに重いので、パフォーマンスが悪くなる。
一方、値渡しの場合、特に小さなデータ構造やプリミティブ型では、スタック上で処理されることが多い。スタックのメモリ管理はスコープから外れたときに自動的に解放されるため、GCの介入が必要なく、処理が軽くなる。
もちろん、大きなデータ構造や頻繁なコピーが発生する場合は、参照渡しの方が適しているケースもあるため、常に値渡しが優れているわけではない。