Conversation
dohyeon2
left a comment
There was a problem hiding this comment.
๊ฐ๋จํ ๋ฌธ์ ์ง๋ง, Rust๋ฅผ ์ฌ๋ฏธ์๊ฒ ์ ์ดํด๋ณผ ์ ์์์ต๋๋ค.
์ข์ ํ์ด ๊ฐ์ฌํฉ๋๋ค.
| for (i, &num) in nums.iter().enumerate() { | ||
| let complement = target - num; | ||
| if let Some(&j) = indices.get(&complement) { | ||
| return vec![j as i32, i as i32]; |
There was a problem hiding this comment.
'์ด ๋ถ๋ถ์์ ์ ์ ๋ ฌ์ ์ํ์ง?'๋ผ๊ณ ์๊ฐํ๋๋ฐ
You can return the answer in any order.
์ต๋ฉด์ ๊ฑธ๋ฆฐ ๊ฒ ์ฒ๋ผ ์ ๋ ฌ์ ํด๋ฒ๋ ธ๋๋ฐ,,
์ด๋ฐ ์กฐ๊ฑด์ด ์๋ค์.
์ฌ์ง์ด ์์ฐจ์ ์ผ๋ก ์กฐํํ๋ฉฐ index๋ฅผ ๋ฐํํ๋๊ฑฐ๋ผ
์ ๋ ฌ์ ์ํด๋ ๊ฒฐ๊ณผ๊ฐ ๊ฐ๋ค๋ ๋น์ฐํ ์ฌ์ค์ ์๊ฒ๋์์ต๋๋ค..
| // TC: O(n) | ||
| // SC: O(n) | ||
| impl Solution { | ||
| pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { |
There was a problem hiding this comment.
Vec<i32>์ ์๋ฏธ๋ ์๊ฒ ๋๋ฐ,
i32๋ผ๋ ํํ์ด ๊ฝค๋ ๋ฌด์ญ๋ค์.
ํ์คํ ๋ค๋ฅธ ์ธ๊ธฐ์๋ ์ธ์ด๋ค์ ๋นํด ์ ์์ค์ ๋ค๋ฃฌ๋ค๋ ๊ฑธ ์ ์ ์๋ค์.
There was a problem hiding this comment.
๋ค, ์ฐธ ๊ฐ๋ ฅํ ๋งํผ ์์ด ๋ง์ด ๊ฐ๋ ์ธ์ด์ ๋๋ค ๐ฅฒ
| 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.
complement๋ผ๋ ํํ์ ์์๊ฐ๋๋ค.
There was a problem hiding this comment.
ํ์ ์์๋ ๊ฑฐ์ ์ธ ์ผ์ด ์๋ ๋จ์ด์ธ๋ฐ, ์ ๋ ์ฝ๋ฉ ํ ์คํธ ์ค๋นํ๋ฉด์ ๋ฐฐ์ ์ต๋๋ค :)
| let mut indices = HashMap::new(); | ||
| for (i, &num) in nums.iter().enumerate() { | ||
| let complement = target - num; | ||
| if let Some(&j) = indices.get(&complement) { |
There was a problem hiding this comment.
rust์ null์ฐธ์กฐ ๋ฐฉ์ง๊ฐ ๊ฝค ํธ๋ฆฌํ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
๋ค, Rust์ ๊ฐ์ ์ค ํ๋์ฃ ใ ใ
|
@dohyeon2 ๋, ๋ฆฌ๋ทฐ ๊ฐ์ฌํ๋๋ค! ์ฐ์๋ ์ธ์ด๊ฐ ์๋์ ๋ฐ๋ ์ฝ๋ฉํธ๊น์ง ๋ฌ์์ฃผ์๊ณ ๋ฆฌ๋ทฐ ํ๋๊ฐ ๋ ์ข์ผ์ ๊ฒ ๊ฐ์์. ์์ผ๋ก ๋ค๋ฅธ ๋ฉค๋ฒ๋ค๊ป๋ ์ด๋ฐ ์ข์ ๊ฒฝํ ๋๋ ์ฃผ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. ๋๋ถ์ ๊ธฐ๋ถ ์ข๋ค์ :) |
์ข๊ฒ ๋ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค! ์ข์ ์ํฅ๋ ฅ์ ๋ผ์น ์ ์๋๋ก ๊ด์ฌ๊ฐ์ง๊ณ ์ํ๊ฒ ์ต๋๋ค~ |
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!