-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum (from Grind 75 questions Easy-Array) #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java見るのかなり久しぶりなので、変なこと言ってたらすみません
5分26秒 | ||
|
||
### 感想(他の方のコードなどを読んで) | ||
- HashTableよりHashMapのほうが1ms早くなった。このあたりも仕様を把握して使い分けできるようにしたい。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしleetcodeで出力されたRuntimeの値を見てるんでしたら、結構ブレが大きいので大きな差でなければ気にしないでいいかもです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにおっしゃるとおりですねmm
細かい差はあまり気にしないようにしますmm
```Java | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
HashMap<Integer, Integer> map = new HashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここは Map<Integer, Integer> map = new HashMap<>();
のように広いインターフェースを使ってもいいかもです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
特別な理由がない限り、広いinterfaceで定義したほうが良さそうですね!
ありがとうございますmm
課題の言語化でだいぶコードのイメージがついた。 | ||
とりあえず書いてみる。 | ||
ここまでで11分。 | ||
今回は値が見付からなかったときは`[-1, -1]`で返すことにする。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Javaならnullを返すのも一つかもですね
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにそうですね!
nullだと呼び出し側はどんな印象を受けるのでしょうか。
個人的には戻り値が配列のシグネチャならnullは返ってこないことを期待したい(Optionalで返してほしい、nullチェックを呼び出し側にさせるのは嫌だなという感覚)ですが、[-1, -1]で返しても結局呼び出し側で値が見つかったかどうかのチェックするので、一緒ですね...
(独り言ですmm)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的には戻り値が配列のシグネチャならnullは返ってこないことを期待したい(Optionalで返してほしい、nullチェックを呼び出し側にさせるのは嫌だなという感覚)ですが、[-1, -1]で返しても結局呼び出し側で値が見つかったかどうかのチェックする
うーむ難しいですよね...正直、よく分からないです。入力が前提条件を満たさないときに特殊な値を返すとして、nullが返るのと[-1, -1]が返るのどちらがびっくりしないかという話かなと思っていて、Javaだったら前者だろうか...?と思ってコメントした感じでした。Optionalで返したいの分かります。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
びっくりしないかという捉え方すごくわかりやすいです!
そういう観点で戻りを考えることもしていこうと思いましたmm
ありがとうございます!
@fhiyo san |
|
||
### 感想(他の方のコードなどを読んで) | ||
- 今回は必ず解が見つかる条件だったが、見つからないときにどのような値を返すかは考慮してよかった | ||
- 謎にbreakで抜けてからreturnしているが、普通にその場でreturnで良かった |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これは違和感が結構ありますね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですよねmm
普段あまりしないのですが、がむしゃらに考えてコード書いていたら最終的によくわからないコードが誕生していましたmm
自分もかなり違和感を持つコードだったので、すぐに気付けるようにトレーニングしますmm
Question
https://leetcode.com/problems/two-sum/description/