-
Notifications
You must be signed in to change notification settings - Fork 4
[imchanyo] 25.01.05 #12
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
Changes from all commits
12a8cbc
9b5b444
e3c4c2d
d0477db
a2159fd
12ffb31
082d0b9
b8611f2
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,6 @@ | ||
| function solution(strlist) { | ||
| var answer = []; | ||
|
|
||
| strlist.forEach((string) => answer.push(string.length)); | ||
| return answer; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function solution(arr1, arr2) { | ||
| let answer = []; | ||
| for (let i = 0; i < arr1.length; i++) { | ||
| let newArr = []; | ||
| for (let j = 0; j < arr1[0].length; j++) { | ||
| newArr.push(arr1[i][j] + arr2[i][j]); | ||
| } | ||
| answer.push(newArr); | ||
| } | ||
|
|
||
| return answer; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function solution(array, n) { | ||
| var answer = []; | ||
|
|
||
| array.forEach((num) => { | ||
| if (num === n) { | ||
| answer.push(num); | ||
| } | ||
| }); | ||
|
|
||
| return answer.length; | ||
|
Collaborator
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. 코드 길이를 줄이고 싶으시다면 filter라는 함수를 사용해도 좋을 것 같아요! 성능도 비슷하고!
Comment on lines
+1
to
+10
Collaborator
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. 배열을 순회하면서 특정 수의 개수를 세는 것이니까, 한번만 순회하면서 답을 구하는 건 좋은 것같아요. 그런데 정답을 도출하기위해 배열하나를 더해서 push작업을 수행하고 있는데, push도 결국 배열길이가 늘어가면 비용이 커지니까 이 문제에서는 reduce나 filter를 사용하는 것이 더 좋지 않을까 합니다. 특히 이 문제는 값하나(개수)를 도출해내는 것이니까 reduce가 제일 간단할 것 같아요 const solution = (array, n) => {
return array.reduce((acc,cur) => {
return cur === n ? ++acc : acc;
},0)
} |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| function solution(arr) { | ||
| var answer = []; | ||
|
|
||
| if (arr[0] === 10) { | ||
| return [-1]; | ||
| } | ||
|
Comment on lines
+4
to
+6
Collaborator
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. 왜 첫번째 배열이 10인경우 |
||
|
|
||
| for (let i = 0; i < arr.length; i++) { | ||
| for (let j = 0; j < arr.length; j++) { | ||
| if (arr[i] > arr[j]) { | ||
| answer.push(arr[i]); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+8
to
+14
Collaborator
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. 이렇게 하면 시간 복잡도가 올라갈 것 같은데, 최소값을 먼저 구하고 그걸로 배열을 가공하면 복잡도가 줄어들지 않을까 생각합니다!
Collaborator
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. 같은 생각입니다! |
||
|
|
||
| return [...new Set(answer)]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| function solution(numbers, direction) { | ||
| var answer = [...numbers]; | ||
|
|
||
| if (direction === "right") { | ||
| const rightNumbers = answer.pop(); | ||
| answer.unshift(rightNumbers); | ||
| } else if (direction === "left") { | ||
| const leftNumbers = answer.shift(); | ||
| answer.push(leftNumbers); | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| /* slice 연습하기 */ | ||
| function solution(numbers, direction) { | ||
| if (direction === "right") { | ||
| const last = numbers.slice(-1); | ||
| const rest = numbers.slice(0, -1); | ||
| return [...last, ...rest]; | ||
| } else if (direction === "left") { | ||
| const first = numbers.slice(0, 1); | ||
| const rest = numbers.slice(1); | ||
| return [...rest, ...first]; | ||
| } | ||
| } | ||
|
Comment on lines
+16
to
+26
Collaborator
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. slice함수에 음수 인덱스를 사용할 수 있는 것 리마인드 하고 갑니다 :) const solution = (numbers, direction) => {
if (direction === 'right'){
return [numbers[numbers.length - 1], ...numbers.slice(0,numbers.length - 1)];
}
else{
return [...numbers.slice(1), numbers[0]];
}
} |
||
|
|
||
| /* splice 연습 */ | ||
| function solution(numbers, direction) { | ||
| if (direction === "right") { | ||
| const last = numbers.splice(-1, 1); | ||
| numbers.unshift(...last); | ||
| } else if (direction === "left") { | ||
| const first = numbers.splice(0, 1); | ||
| numbers.push(...first); | ||
| } | ||
| return numbers; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| function solution(numbers, num1, num2) { | ||
| var answer = []; | ||
| answer = numbers.splice(num1, num2); | ||
| return answer; | ||
| } |
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 함수도 사용해보면 좋을 것 같아요! 그러면 좀 더 간결해집니다!