-
Notifications
You must be signed in to change notification settings - Fork 53
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
[highball] week2 solutions #371
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,63 @@ | ||
function TreeNode(val, left, right) { | ||
this.val = val === undefined ? 0 : val; | ||
this.left = left === undefined ? null : left; | ||
this.right = right === undefined ? null : right; | ||
} | ||
|
||
const buildTree = function (preorder, inorder) { | ||
if (preorder.length === 0) return null; | ||
|
||
const rootNode = new TreeNode(preorder[0]); | ||
const rootValueIndexAtInorder = inorder.indexOf(preorder[0]); | ||
|
||
const leftLength = rootValueIndexAtInorder - 0; | ||
const rightLength = inorder.length - 1 - rootValueIndexAtInorder; | ||
|
||
const leftPreorder = preorder.slice(1, 1 + leftLength); | ||
const leftInorder = inorder.slice(0, 0 + leftLength); | ||
rootNode.left = buildTree(leftPreorder, leftInorder); | ||
|
||
const rightPreorder = preorder.slice( | ||
1 + leftLength, | ||
1 + leftLength + rightLength | ||
); | ||
const rightInorder = inorder.slice( | ||
rootValueIndexAtInorder + 1, | ||
rootValueIndexAtInorder + 1 + rightLength | ||
); | ||
rootNode.right = buildTree(rightPreorder, rightInorder); | ||
|
||
return rootNode; | ||
}; | ||
|
||
function bfsTraversal(root) { | ||
const treeValues = []; | ||
|
||
const queue = []; | ||
queue.push(root); | ||
|
||
while (queue.length > 0) { | ||
const n = queue.length; | ||
|
||
for (let i = 0; i < n; i++) { | ||
const currentNode = queue.shift(); | ||
|
||
if (currentNode) { | ||
treeValues.push(currentNode.val); | ||
queue.push(currentNode.left); | ||
queue.push(currentNode.right); | ||
} else { | ||
treeValues.push(null); | ||
queue.push(null); | ||
queue.push(null); | ||
} | ||
} | ||
|
||
if (queue.every((el) => el === null)) break; | ||
} | ||
|
||
return treeValues; | ||
} | ||
|
||
// console.log(bfsTraversal(buildTree([3, 9, 20, 15, 7], [9, 3, 15, 20, 7]))); | ||
console.log(bfsTraversal(buildTree([1, 2], [2, 1]))); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const countBits = function (n) { | ||
const arr = new Array(n + 1); | ||
arr[0] = 0; | ||
if (n === 0) return arr; | ||
arr[1] = 1; | ||
if (n === 1) return arr; | ||
|
||
for (i = 2; i <= n; i++) { | ||
if (i % 2 === 0) arr[i] = arr[i / 2]; | ||
else arr[i] = arr[(i - 1) / 2] + 1; | ||
} | ||
|
||
return arr; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const encode = function (arr) { | ||
const encodedStr = arr.reduce((acc, cur) => acc + "#" + cur); | ||
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. arr.join('#') 도 가능하겠네용! 달레님 코멘트처럼 아스키문자를 피해야 될 것 같아여! 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. 문제에서 입력 문자열에 256개의 ASCII 문자가 들어있을 수 있다고 했는데요. |
||
return encodedStr; | ||
}; | ||
|
||
const decode = function (str) { | ||
const decodedArr = str.split("#"); | ||
return decodedArr; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const isAnagram = function (s, t) { | ||
const sCharCount = new Array(26).fill(0); | ||
const tCharCount = new Array(26).fill(0); | ||
|
||
Array.from(s).forEach((char) => { | ||
const charAsInt = char.charCodeAt(0) - 97; | ||
sCharCount[charAsInt]++; | ||
}); | ||
|
||
Array.from(t).forEach((char) => { | ||
const charAsInt = char.charCodeAt(0) - 97; | ||
tCharCount[charAsInt]++; | ||
}); | ||
Comment on lines
+5
to
+13
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. s와 t의 길이가 같지 않으면 anagram이 아니기 때문에 early retrun 하고 하나의 루프로 체크하는 것도 가능할 것 같아여! |
||
|
||
return sCharCount.reduce((acc, cur, i) => acc && cur === tCharCount[i], true); | ||
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. sCharCount.every((count, i) => count === tCharCount[i]); 로 확인할 수도 있겠네용! 검증이라면 every로 모든 값이 일치하는게 메소드가 조금 더 어울리는 것 같아여! |
||
}; |
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.
특정 인덱스 값에 대입이어서 값에 대한 삼항으로 처리해도 좋을 것 같네영!