-
-
Notifications
You must be signed in to change notification settings - Fork 311
[DaleSeo] WEEK 1 Solutions #2348
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) | ||
| impl Solution { | ||
| pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { | ||
| let mut indices = HashMap::new(); | ||
| for (i, &num) in nums.iter().enumerate() { | ||
| let complement = target - num; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. complement라는 표현을 알아갑니다. |
||
| if let Some(&j) = indices.get(&complement) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rust의 null참조 방지가 꽤 편리한 것 같습니다. |
||
| return vec![j as i32, i as i32]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '이 부분에서 왜 정렬을 안하지?'라고 생각했는데
최면에 걸린 것 처럼 정렬을 해버렸는데,, 심지어 순차적으로 조회하며 index를 반환하는거라 |
||
| } | ||
| indices.insert(num, i); | ||
| } | ||
| vec![] | ||
| } | ||
| } | ||
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.
Vec<i32>의 의미는 알겠는데,i32라는 표현이 꽤나 무섭네요.
확실히 다른 인기있는 언어들에 비해 저수준을 다룬다는 걸 알 수 있네요.