Skip to content

Implement reverseWords function to reverse each word#43

Merged
argondev22 merged 1 commit intomainfrom
20251231/42-557-reverse-words-in-a-string-iii
Dec 31, 2025
Merged

Implement reverseWords function to reverse each word#43
argondev22 merged 1 commit intomainfrom
20251231/42-557-reverse-words-in-a-string-iii

Conversation

@argondev22
Copy link
Copy Markdown
Owner

@argondev22 argondev22 commented Dec 31, 2025

Status

Accepted

Runtime

15 ms

Order

O(n)

Memory

18.06 MB

Order

O(n)

Time Taken

m s

@argondev22 argondev22 self-assigned this Dec 31, 2025
Copilot AI review requested due to automatic review settings December 31, 2025 08:43
@argondev22 argondev22 linked an issue Dec 31, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

このプルリクエストは、LeetCode問題557「Reverse Words in a String III」の解答を実装しています。文字列内の各単語を個別に反転させる機能を提供します。

主な変更点:

  • SolutionクラスにreverseWordsメソッドを実装
  • 補助関数reverseStringを実装して個々の単語を反転
  • ツーポインタアルゴリズムを使用した文字列反転ロジック

Comment on lines +4 to +5
words = [reverseString(word) for word in words]
return " ".join(words)
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

メソッド内で変数を再代入する際、より明確な変数名を使用するか、再代入を避けることで可読性が向上します。

現在の実装:

words = s.split()
words = [reverseString(word) for word in words]

同じ変数名wordsを2回使用していますが、最初は元の単語リスト、2回目は反転された単語リストを保持しています。

より明確にするには:

words = s.split()
reversed_words = [reverseString(word) for word in words]
return " ".join(reversed_words)

または、中間変数を省略して1行で記述することも可能です:

return " ".join(reverseString(word) for word in s.split())

これにより、コードの意図がより明確になります。

Suggested change
words = [reverseString(word) for word in words]
return " ".join(words)
reversed_words = [reverseString(word) for word in words]
return " ".join(reversed_words)

Copilot uses AI. Check for mistakes.
Comment on lines +9 to +18
right = 0
left = len(s) - 1

while right < left:
tmp = s[left]
s[left] = s[right]
s[right] = tmp

right += 1
left -= 1
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

変数の初期化が逆になっています。right変数は左端(0)から開始し、left変数は右端(len(s) - 1)から開始していますが、これでは変数名と実際の役割が逆になっています。

現在の実装では:

  • right = 0(実際には左端を指している)
  • left = len(s) - 1(実際には右端を指している)

この混乱により、コードの可読性が著しく低下しています。変数名と実際の位置を一致させるべきです。

以下のいずれかの修正が必要です:

オプション1:変数名を修正

  • rightleftに変更
  • leftrightに変更

オプション2:初期化を修正

  • left = 0
  • right = len(s) - 1

どちらの場合も、while条件やインデックスアクセスを適切に調整する必要があります。

Suggested change
right = 0
left = len(s) - 1
while right < left:
tmp = s[left]
s[left] = s[right]
s[right] = tmp
right += 1
left -= 1
left = 0
right = len(s) - 1
while left < right:
tmp = s[left]
s[left] = s[right]
s[right] = tmp
left += 1
right -= 1

Copilot uses AI. Check for mistakes.
Comment thread src/leetcode/557_reverse-words-in-a-string-iii/20251231.py
@argondev22 argondev22 merged commit ee0b2b4 into main Dec 31, 2025
6 checks passed
@argondev22 argondev22 deleted the 20251231/42-557-reverse-words-in-a-string-iii branch December 31, 2025 09:32
@github-project-automation github-project-automation Bot moved this from Todo to Done in My Learnings Dec 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

557. Reverse Words in a String III

2 participants