본문 바로가기

Web development131

[LeetCode] Reverse Linked List (javascript) 연결리스트 뒤집기. 좋은 풀이는 아니라고 생각하지만.. 배열로 변환한 후 새로운 리스트를 만드는 방식으로 품. /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @return {ListNode} */ var reverseList = function(head) { if(head==null) return head; let items = []; let curr = head; while(curr){ items.. 2021. 2. 26.
[LeetCode] Remove Nth Node from End of List (javascript) 뒤에서 n번째 노드 삭제하기. 우선 리스트 길이를 구한 후 삭제할 노드의 인덱스에 다다르면 삭제하는 방법으로 풀었다. /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @param {number} n * @return {ListNode} */ var removeNthFromEnd = function(head, n) { var curr = head; var prev, index = 0; // ListNo.. 2021. 2. 26.
[LeetCode] Delete Node in a Linked List (javascript) 주어진 노드를 삭제하는 간단한 문제다. 삭제할 노드가 이미 주어져 있으므로 주어진 노드의 현재 값(val)에 next를, 주어진 노드의 next 값에 next.next를 할당하면 된다. /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} node * @return {void} Do not return anything, modify node in-place instead. */ var deleteNode = function(node) { // val과 next만 정의해주면 되므로, // node.val(현재 값)은 .. 2021. 2. 26.
[LeetCode] Longest Common Prefix (javascript) 공통으로 겹치는 문자열을 찾아내는 문제다. /** * @param {string[]} strs * @return {string} */ var longestCommonPrefix = function(strs) { let result = ''; if(strs.length 2021. 2. 26.
Binary Tree 보호되어 있는 글 입니다. 2021. 2. 22.
Linked List 보호되어 있는 글 입니다. 2021. 2. 22.
[javascript] index를 찾아 반환하는 메서드 const arr = [2, 5, 9, 2]; findIndex 주어진 판별함수를 만족하는 첫번째 요소의 인덱스를 반환 없으면 -1을 반환한다. arr.findIndex((item) => item > 3); // 1 arr.findIndex((item) => item > 10); // -1 indexOf 정해진 요소의 첫번째 인덱스를 반환 없으면 -1을 반환한다. arr.indexOf(2) // 0 arr.indexOf(10) // -1 // 두 번째 인자로 start index를 지정할 수 있다. arr.indexOf(2, 1) // 3 lastIndexOf 정해진 요소를 배열 맨 뒤에서부터 찾아 인덱스를 반환 없으면 -1을 반환한다. arr.lastIndexOf(2) // 3 arr.indexOf(1.. 2021. 1. 23.
[LeetCode] Count and Say leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/886/ "1"로 시작해서 재귀적으로 n번만큼 숫자를 세어 나가는 문제다. 문자열을 같은 문자끼리 몇 개 있는지 쪼개주는 함수와 세어주는 함수로 분리하여 구현했다. 복잡한 기능을 하는 문제의 경우엔 이렇게 필요할 것 같은 함수부터 조각조각 구현해 나가는 방식이 좋은 것 같다. /** * @param {number} n * @return {string} */ var countAndSay = function (n) { let str = "1"; for (let j = 1; j < n; j++) { const splitted = split(str); let temp = "".. 2021. 1. 23.
[Javscript] 알고리즘 풀이에 자주 사용되는 숫자 관련 메서드 정리 정확히 말하면 Math, Number객체, 프로퍼티, 메서드가 혼용되어 있지만... 아무튼 자주 활용되는 것들을 정리하고자 한다. 수의 범위 관련 javascript에서 다룰 수 있는 수의 범위 -9007199254740991 ~ 9007199254740991 -Math.pow(2, 53) + 1 === Number.MIN_SAFE_INTEGER; // -9007199254740991 Math.pow(2, 53) - 1 === Number.MAX_SAFE_INTEGER; // -9007199254740991 BigInt -2^53이하 2^53이상의 수를 다룰 때 사용 +, -, *, /, %, ** 연산이 가능하다. const a = 2n ** 53n; // 9007199254740992n // inte.. 2021. 1. 20.