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
17 changes: 17 additions & 0 deletions two-sum/DaleSeo.rs
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> {

Choose a reason for hiding this comment

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

Vec<i32>의 의미는 알겠는데,
i32라는 표현이 꽤나 무섭네요.

확실히 다른 인기있는 언어들에 비해 저수준을 다룬다는 걸 알 수 있네요.

let mut indices = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;

Choose a reason for hiding this comment

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

complement라는 표현을 알아갑니다.

if let Some(&j) = indices.get(&complement) {

Choose a reason for hiding this comment

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

rust의 null참조 방지가 꽤 편리한 것 같습니다.

return vec![j as i32, i as i32];

Choose a reason for hiding this comment

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

'이 부분에서 왜 정렬을 안하지?'라고 생각했는데

You can return the answer in any order.

최면에 걸린 것 처럼 정렬을 해버렸는데,,
이런 조건이 있네요.

심지어 순차적으로 조회하며 index를 반환하는거라
정렬을 안해도 결과가 같다는 당연한 사실을 알게되었습니다..

}
indices.insert(num, i);
}
vec![]
}
}