Skip to content

128. Longest Consecutive Sequence#1

Open
yamashita-ki wants to merge 2 commits intomainfrom
leetcode/128
Open

128. Longest Consecutive Sequence#1
yamashita-ki wants to merge 2 commits intomainfrom
leetcode/128

Conversation

@yamashita-ki
Copy link
Owner

@yamashita-ki yamashita-ki changed the title Leetcode/128 128. Longest Consecutive Sequence Sep 12, 2025
- 空間計算量:O(n)
- 「You must write an algorithm that runs in O(n) time.」と記載があったがSortする方法しか思い浮かばず
- 考慮漏れしエラーになったケース
- 与えられたnumsが空の場合を考慮できずエラー
Copy link
Owner Author

Choose a reason for hiding this comment

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

submit後に修正して現在は修正済みです

```java
class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length == 0) return 0;
Copy link

Choose a reason for hiding this comment

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

step1とstep2で、if文のスペースがあったりなかったりしていますね。
体裁面ではPythonであればPEP8や、C++ではgoogleのコーディング規約がよく引用されていると思います。
Javaだとhttps://google.github.io/styleguide/javaguide.htmlなどがあるようです。

```java
public class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> numSet = new HashSet<>();
Copy link

Choose a reason for hiding this comment

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

numsから重複を取り除いたものへの命名としては、distinctNums, uniqueNums とかもいいかもです。

命名関係はdiscordだと、https://discord.com/channels/1084280443945353267/1230079550923341835/1230201155619913728 に色々とまとめてくださっています。

## step2 他の回答を見る
- 感想:
- 入力値を数直線上に置いたときに、連続値の始まりかどうかの判別は「左隣の数値が存在するかどうか」という観点を持てていなかった
- 重複する数値はノイズでしかないのでHash Setでもつという観点もなかった
Copy link

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.

2 participants