Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions 2/memo.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@

```cpp
class Solution {
ListNode* addTwoNumber(ListNode* l1, ListNode* l2) {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy_head = ListNode();
ListNode* result_tail = &dummy_head;
int carryover = 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.

carry で、コンピューター用語で繰り上げを表します。こちらのほうがシンプルだと思います。 carryover でも十分伝わると思いますので、好みの問題だと思います。

while (l1 || l2 || carryover) {
int value = carryover;
int digitSum = carryover;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

snake_case と lowerCamel が混ざっているのが気になりました。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/cppguide.html#Variable_Names

The names of variables (including function parameters) and data members are snake_case (all lowercase, with underscores between words).

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

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.

コメントありがとうございます。
sanke_caseとlowerCamelという言い回しがあるんですね。
変数、関数、classなどそれぞれでの命名ルールも参考にします。

if (l1) {
value += l1->val;
digitSum += l1->val;
l1 = l1->next;
}
if (l2) {
value += l2->val;
digitSum += l2->val;
l2 = l2->next;
}

carryover = value / 10;
value %= 10;
carryover = digitSum / 10;
digitSum %= 10;
Copy link
Copy Markdown

@maeken4 maeken4 Aug 6, 2025

Choose a reason for hiding this comment

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

この段階でdigitSumは意味的にはdigitSumではなくなってしまうので、別の変数に入れるか、この後直接
result_tail->next = new ListNode(digitSum % 10);
としてもよいように思いました。


result_tail->next = new ListNode(value);
result_tail->next = new ListNode(digitSum);
result_tail = result_tail->next;
}
return dummy_head.next;
Expand All @@ -48,5 +49,24 @@ class Solution {

```

##Step2 8/1 10:50
##Step2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

マークダウンとして装飾するには##の後に半角空白を入れて## step2のようにしないとうまく表示されないです。vscodeなどであればmdファイルのプレビュー(右上の虫眼鏡マーク)ができると思うので確認してみるとよいと思います。

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.

コメントありがとうございます。
一度previewを確認するなどして、仕様も都度修正致します。


* Jikuharaさん https://github.com/Jikuhara/LeetCode/pull/11/commits/846d5c426f15565ccb1106430681b1f388b24538
-> skypenguins on Jun 14 さんのコメント 処理の始めから value に次々に l1 , l2 の値を足していく処理のようですが、変数の生存期間が長くてなんとなく頭の一時記憶を占める感覚があります。 

* new で確保したメモリは、LeetCodeでは解放の必要はないが、実務ではNG.
LeetCode テスト環境では、実行終了後プロセスごと破棄。OSが確保していたヒープを一括で回収する仕様になっている。
https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers

* コード領域、データ領域、スタック領域、ヒープ領域に関して
https://discord.com/channels/1084280443945353267/1237649827240742942/1251960606077091981

##Step3

step3は、それほど時間は掛からなかった。
丁度LeetCodeを初めて1週間ほど。やはり基礎的な知識が欠落してるので、理解に時間がかかりすぎている。
あと8-9カテゴリで https://www.learn-cpp.org/en/Welcome を一通り終える。
LeetCodeの一歩手前みたいな、書く練習と読む練習ができるものを探すか、このままLeetCodeを続けるか考えたい。