-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Sehwan]Week5 solutions with javascript #98
Conversation
@nhistory 님, 다른 분들처럼 문제를 다 푸실 때 까지는 Draft PR로 해주시면 좋을 것 같은데요? |
} | ||
|
||
// 3. Sort frequency and return sliced array | ||
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k); |
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.
Cool! 😝
수정했습니다! |
@@ -0,0 +1,30 @@ | |||
var longestConsecutive = function (nums) { | |||
// Return 0 if there are no elements in nums | |||
if (nums.length === 0) return 0; |
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.
저는 이렇게 앞에 예외 처리해주는 걸 생각하지 않았는데 이렇게 미리 걸러내주는게 효율적이겠군요.
} | ||
|
||
// 3. Sort frequency and return sliced array | ||
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k); |
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.
object 앞에 ...은 뭘 의미할까요?
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.
자바스크립트에서 '...'은 spread operator로 context에 따라 다양하게 사용되곤 하는데요.
여기서는 map 오브젝트의 key들을 어레이로 받을 때 shallow copy를 하기 위해서 사용했습니다.
spread operator 없이 아래와 같은 코드로 실행해도 문제는 없는데,
return (Object.keys(map).sort((a,b)=>map[b]-map[a]).slice(0,k))
원본 오브젝트를 변경하지 않고 리턴값을 받아야 하는 상황을 가정했다고 보시면 될 것 같습니다.
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.
수고하셨어용!
Top K Frequent Elements
Encode and Decode Strings
Product of Array Except Self (added 5/27)
Longest Consecutive Sequence (added 5/28)
3sum (added 5/30)