Last active
January 16, 2021 15:03
-
-
Save skawnkk/02a998dba6c0442a96d5f20717dc13ed to your computer and use it in GitHub Desktop.
week2알고리즘
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function ListNode(val, next) { | |
| this.val = (val === undefined ? 0 : val) | |
| this.next = (next === undefined ? null : next) | |
| } | |
| var deleteDuplicates = function (head) { | |
| let curr = head; | |
| //head and next exist | |
| while (curr !== null && curr.next !== null) { | |
| if (curr.val === curr.next.val) { | |
| curr.next = curr.next.next; | |
| } else { | |
| curr = curr.next; | |
| }; | |
| } | |
| return head; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var hasCycle = function (head) { | |
| let fastOne = head; | |
| let slowOne = head; | |
| while (fastOne !== null && fastOne.next !== null) { | |
| fastOne = fastOne.next.next; | |
| slowOne = slowOne.next; | |
| if (fastOne === slowOne) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var longestCommonPrefix = function (strs) { | |
| let answer = ''; | |
| //no value | |
| if (strs.length === 0) { | |
| return answer; | |
| } | |
| //1개의 prefix | |
| if (strs.length === 1) { | |
| answer += strs; | |
| return answer; | |
| } | |
| //여러 prefix 중 제일 짧은 것 | |
| strs.sort((a, b) => a.length - b.length); | |
| shortstrs = strs[0].split(""); | |
| for (let str of strs) { | |
| for (let i = 0; i < shortstrs.length; i++) { | |
| if (str[i] === (shortstrs[i])) { | |
| answer += shortstrs[i]; | |
| } else { | |
| break; | |
| } | |
| } | |
| answer += '/'; | |
| } | |
| answer = answer.split('/'); | |
| answer.sort((a, b) => a.length - b.length); | |
| return answer[1] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var isPalindrome = function (x) { | |
| let answer = true; | |
| x = String(x); | |
| let middle = Math.floor(x.length / 2); //중간값 | |
| for (let i = 0; i < middle; i++) { | |
| if (x[i] !== x[x.length - 1 - i]) { | |
| return false | |
| } else { | |
| answer = true; | |
| } | |
| } | |
| return answer; | |
| }; | |
| let x = -1221; | |
| console.log(isPalindrome(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let x = -123; | |
| var reverse = function (x) { | |
| let answer = []; | |
| let splicedX; | |
| x = String(x).split("") | |
| if (x[0] === '-') { | |
| splicedX = x.splice(0, 1); | |
| } | |
| for (let i of x) { | |
| answer.unshift(i); | |
| } | |
| if (splicedX) { | |
| answer.unshift(splicedX); | |
| } | |
| answer = parseInt(answer.join('')); | |
| return ((-(2 ** 31) <= answer) && (answer <= 2 ** 31 - 1)) ? answer : 0; | |
| }; | |
| console.log(reverse(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var twoSum = function (nums, target) { | |
| let sum = 0; | |
| for (let i = 0; i < nums.length - 1; i++) { | |
| for (let j = i + 1; j < nums.length; j++) { | |
| sum = nums[i] + nums[j]; | |
| if (sum === target) { | |
| return [i, j]; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment