Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // let strForNumber = "234";
- // resStrForNumber = (5 * 10) + (strForNumber[2] - 0);
- // console.log(strForNumber[1] + strForNumber[2]); // 34
- // console.log(resStrForNumber); // 54
- // console.log(-11 - 0); // 54
- // console.log('Nana'.repeat(7));
- // let numbers = [4, 2, 5, 1, 3];
- // numbers.sort((a, b) => a - b);
- // console.log('sorted numbers', numbers);
- // let value = 3
- // let arr = [1, 2, 3, 4, 5, 3]
- // arr = arr.filter(item => item !== value)
- // console.log('after filter', arr); // [ 1, 2, 4, 5 ]
- // const animals = ['pigs', 'goats', 'sheep'];
- // console.log(animals.push('chickens', 'cats', 'dogs')); // 6
- // console.log(animals.pop()); // dogs
- // let a, b, rest;
- // [a, b] = [10, 20];
- // console.log(a);// 10
- // console.log(b);// 20
- // const [a, b, ...resting] = [10, 20, 30, 40, 50];
- // console.log(resting);// [30,40,50]
- // reverse array
- function reverseHelper(arr, start, end) {
- while (start < end) {
- [arr[start], arr[end]] = [arr[end], arr[start]];
- start++;
- end--;
- }
- return arr;
- }
- // console.log(reverseHelper([3,4,2], 0, ([3,4,2].length - 1)));
- // console.log(reverseHelper([3,4,2], 0, ([3,4,2].length - 1)));
- // console.log(reverseHelper([3,4,2], 0, ([3,4,2].length - 1)));
- // 1
- let twoSum = function(nums, target) {
- const seenNumsWithIndexes = {};
- for (let i = 0; i < nums.length; i++) {
- const num = nums[i];
- if (seenNumsWithIndexes[target - num] !== undefined) {
- return [seenNumsWithIndexes[target - num], i];
- }
- seenNumsWithIndexes[num] = i;
- }
- };
- // console.log(twoSum([2,8,3,7],9)); // [ 0, 3 ]
- /** 2 @param {ListNode} l1 | @param {ListNode} l2 | @return {ListNode} */
- var addTwoNumbers = function(l1, l2) {
- var carry = 0;
- var digitSum = 0;
- var head = new ListNode();
- var now = head;
- while (l1 || l2) {
- digitSum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
- carry = digitSum > 9 ? 1 : 0;
- now.next = new ListNode(digitSum % 10);
- now = now.next;
- l1 = l1 ? l1.next : null;
- l2 = l2 ? l2.next : null;
- }
- if (carry) now.next = new ListNode(carry);
- return head.next;
- };
- // Time complexity : O(max(m,n)). Space complexity : O(max(m,n)).
- // console.log(listToArray(addTwoNumbers(arrToList([2,4,3]), arrToList([5,6,4])))); // [ 7, 0, 8 ]
- // // listToArray(addTwoNumbers(arrToList([9,9,9,9,9,9,9]), arrToList([9,9,9,9]))); // [8,9,9,9,0,0,0,1]
- // Helper functions
- function ListNode(val, next) {
- this.val = (val ? val : 0)
- this.next = (next ? next : null);
- }
- function listToArray(list) {
- let arr = []; // Initialise an array to return
- while (list) {
- arr = [...arr, list.val];
- list = list.next;
- };
- return arr;
- }
- function arrToList (arr) {
- let obj = null;
- for(i=0;i<arr.length;i++){
- obj = {val: arr[i], next: arrToList(arr.slice(i+1,arr.length))}
- }
- return obj;
- }
- /** 3 @param {string} s| @return {number} */
- let lengthOfLongestSubstring = function(s) {
- let map = {};
- let max = 0;
- let start = 0;
- for (let i = 0; i < s.length; i++) {
- if (map[s[i]] !== undefined) {
- start = Math.max(start, map[s[i]] + 1); // start from latest instance of char
- }
- map[s[i]] = i;
- max = Math.max(max, i - start + 1);
- }
- return max;
- };
- // console.log(lengthOfLongestSubstring("pwwkew")); // 3
- /** 8 @param {string} str | @return {number} */
- let myAtoi = function(str) {
- let i = 0;
- let sign = 1;
- let res = 0;
- // str = str.trim();
- let INT_MAX = Math.pow(2, 31) - 1;
- let INT_MIN = - Math.pow(2, 31); // 2147483648
- while (str[i] === ' ') i++; // str.trim();
- if (str[i] === '+' || str[i] === '-') {
- sign = str[i] === '+' ? 1 : -1;
- i++;
- }
- while (str[i] >= '0' && str[i] <= '9') {
- res = (res * 10) + (str[i] - 0);
- if (sign === 1 && res > INT_MAX) return INT_MAX;
- if (sign === -1 && res > INT_MIN) return INT_MIN;
- i++;
- }
- return res * sign;
- };
- // Time complexity : O(n). Space complexity : O(1).
- // console.log(myAtoi("-2147483649")); // -2147483648
- // console.log(myAtoi("lala -2147483649")); // 0
- // console.log(myAtoi("83649 jin")); // 0
- /** 11 | @param {number[]} height |@return {number} */
- var maxArea = function(height) {
- var max = 0;
- var l = 0; // left
- var r = height.length - 1; // right
- while (l < r) {
- max = Math.max(max, Math.min(height[l], height[r]) * (r - l));
- if (height[l] < height[r]) {
- l++;
- } else {
- r--;
- }
- }
- return max;
- };
- // Time complexity : O(n) | Space complexity : O(1).
- // console.log(maxArea([1,8,6,2,5,4,8,3,7]));
- /** 12 2 @param {number} num | @return {string} */
- var intToRoman1 = function(num) {
- var str = [
- ['I', 'V'], // 1s
- ['X', 'L'], // 10s
- ['C', 'D'], // 100s
- ['M'] // 1000s
- ];
- var res = '';
- var i = 0;
- var digit = 0;
- for ( let i = 0 ; num > 0; i++) {
- digit = num % 10;
- if (digit === 9) {
- res = str[i][0] + str[i + 1][0] + res; // IX + res
- } else if (digit >= 5) {
- res = str[i][1] + str[i][0].repeat(digit - 5) + res;
- } else if (digit === 4) {
- res = str[i][0] + str[i][1] + res; // IV + res
- } else { // 0, 1, 2, 3
- res = str[i][0].repeat(digit) + res;
- }
- num = Math.floor(num / 10);
- // i++;
- }
- return res;
- };
- // Time complexity : O(log(n)) | Space complexity : O(1).
- // console.log(intToRoman1(402)); // CDII
- // console.log(intToRoman1(3000)); // MMM
- // console.log(intToRoman1(93)); // XCIII
- // console.log(intToRoman1(48)); // XLVIII
- // console.log(intToRoman1(4)); // IV
- // console.log(intToRoman1(900)); // CMXCIX
- /** 12 @param {number} num | @return {string} */
- var intToRoman2 = function(num) {
- var map = [
- [1, "I"],
- [4, "IV"],
- [5, "V"],
- [9, "IX"],
- [10, "X"],
- [40, "XL"],
- [50, "L"],
- [90, "XC"],
- [100, "C"],
- [400, "CD"],
- [500, "D"],
- [900, "CM"],
- [1000, "M"]
- ];
- var res = '';
- var i = 12;
- var tmp = 0;
- while (num > 0) {
- res += map[i][1].repeat(Math.floor(num / map[i][0]));
- num %= map[i][0];
- console.log(map[i][0]);
- i--;
- }
- return res;
- };
- //Time complexity : O(log(n)) | Space complexity : O(1).
- // console.log(intToRoman2(402)); // CDII
- // console.log(intToRoman2(3000)); // MMM
- // console.log(intToRoman2(93)); // XCIII
- // console.log(intToRoman2(4)); // IV
- /** 15 @param {number[]} nums | @return {number[][]} */
- var threeSum = function(nums) {
- var len = nums.length;
- var res = [];
- var l = 0; // left
- var r = 0; // right
- nums.sort((a, b) => (a - b));
- for (var i = 0; i < len; i++) {
- if (i > 0 && nums[i] === nums[i - 1]) continue;
- l = i + 1;
- r = len - 1;
- while (l < r) {
- if (nums[i] + nums[l] + nums[r] < 0) {
- l++;
- } else if (nums[i] + nums[l] + nums[r] > 0) {
- r--;
- } else {
- res.push([nums[i], nums[l], nums[r]]);
- while (l < r && nums[l] === nums[l + 1]) l++;
- while (l < r && nums[r] === nums[r - 1]) r--;
- l++;
- r--;
- }
- }
- }
- return res;
- };
- //Time complexity : O(n^2) | Space complexity : O(1)
- // console.log(threeSum([-1,0,1,2,-1,-4]));
- /** 16 @param {number[]} nums | @param {number} target | @return {number} */
- var threeSumClosest = function(nums, target) {
- var len = nums.length;
- var res = nums[0] + nums[1] + nums[2];
- var sum = 0;
- var l = 0; // left
- var r = 0; // right
- nums.sort((a, b) => (a - b));
- for (var i = 0; i < len - 2; i++) {
- if (i > 0 && nums[i] === nums[i - 1]) continue;
- l = i + 1;
- r = len - 1;
- while (l < r) {
- sum = nums[i] + nums[l] + nums[r];
- if (sum < target) {
- l++;
- } else if (sum > target) {
- r--;
- } else {
- return sum;
- }
- if (Math.abs(sum - target) < Math.abs(res - target)) res = sum;
- }
- }
- return res;
- };
- // Time complexity : O(n^2) Space complexity : O(1)
- // console.log(threeSumClosest([-1, 2, 1, -4],1));
- /** 20 @param {string} s | @return {boolean} */
- var isPranthesisValid = function(s) {
- var stack = [];
- var len = s.length;
- var map = {
- '(': ')',
- '[': ']',
- '{': '}'
- };
- for (var i = 0; i < len; i++) {
- if (stack.length > 0 && map[stack[stack.length - 1]] === s[i]) { // value at map's key complements s[i]
- // if (stack.length > 0 && s[i] != s[i+1]) { // generic solution
- stack.pop();
- } else {
- stack.push(s[i]);
- }
- }
- return stack.length === 0;
- };
- // Time complexity : O(n) Space complexity : O(n)
- // console.log(isPranthesisValid("()[]{}}"));
- // console.log(isPranthesisValid("([{}])"));
- /** 21 @param {ListNode} l1 | @param {ListNode} l2 | @return {ListNode} */
- var mergeTwoLists = function(l1, l2) {
- var head = new ListNode();
- var now = head;
- while (l1 || l2) {
- if (l1 === null || (l2 !== null && l2.val < l1.val)) {
- now.next = l2;
- l2 = l2.next;
- } else {
- now.next = l1;
- l1 = l1.next;
- }
- now = now.next;
- }
- return head.next;
- };
- // Time complexity : O(m+n) | Space complexity : O(m+n)
- // console.log(listToArray(mergeTwoLists(arrToList([1,2,4]), arrToList([1,3,4])))); // [ 1,1,2,3,4,4 ]
- /** 28 @param {string} haystack | @param {string} needle | @return {number} */
- var strStr = function(haystack, needle) {
- var len1 = haystack.length;
- var len2 = needle.length;
- if (!len2) return 0;
- for (var i = 0; i < len1; i++) {
- for (var j = 0; j < len2; j++) {
- if (i + j === len1) return -1;
- if (haystack[i + j] !== needle[j]) break; // chars don't match
- if (j === len2 - 1) return i;
- }
- }
- return -1; // not found
- };
- // Time complexity : O(n*m) | Space complexity : O(1)
- // console.log(strStr('Hello', 'llp')); // 2
- /* 42 @param {number[]} height | @return {number} */
- var trap = function(height) {
- var res = 0;
- var left = 0;
- var right = height.length - 1;
- var leftMax = 0;
- var rightMax = 0;
- while (left < right) {
- if (height[left] < height[right]) {
- if (height[left] >= leftMax) {
- leftMax = height[left];
- } else {
- res += leftMax - height[left];
- }
- left++;
- } else {
- if (height[right] >= rightMax) {
- rightMax = height[right];
- } else {
- res += rightMax - height[right];
- }
- right--;
- }
- }
- return res;
- };
- // Time complexity : O(n) | Space complexity : O(1)
- // console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1])); // 6
- // console.log(trap([1,0,1]));
- /** 48 @param {number[][]} matrix | @return {void} Do not return anything, modify matrix in-place instead. */
- var rotate = function(matrix) {
- var n = matrix.length;
- var n2 = Math.floor(n / 2);
- // 1 2 3 7 8 9
- // 4 5 6 => 4 5 6
- // 7 8 9 1 2 3
- for (var i = 0; i < n2; i++) {
- for (var j = 0; j < n; j++) {
- swap(matrix, i, j, n - 1 - i, j);
- }
- }
- // 7 8 9 7 4 1
- // 4 5 6 => 8 5 2
- // 1 2 3 9 6 3
- for (var i = 0; i < n; i++) {
- for (var j = i + 1; j < n; j++) {
- swap(matrix, i, j, j, i);
- }
- }
- return matrix;
- };
- var swap = function (matrix, x1, y1, x2, y2) {
- var tmp = matrix[x1][y1];
- matrix[x1][y1] = matrix[x2][y2];
- matrix[x2][y2] = tmp;
- };
- // Clockwise 90°: first upside down, then diagonally upside down
- // Counterclockwise 90°: first inverted left and right, and then diagonally inverted
- // Time complexity : O(n^2) | Space complexity : O(1).
- console.log(rotate([
- [ 5, 1, 9,11],
- [ 2, 4, 8,10],
- [13, 3, 6, 7],
- [15,14,12,16]
- ]));
- /** 206 | @param {ListNode} head |@return {ListNode} */
- var reverseList = function(head) {
- var newHead = null;
- var tmp = null;
- while (head) {
- tmp = head.next;
- head.next = newHead;
- newHead = head;
- head = tmp;
- }
- return newHead;
- };
- // console.log(listToArray(reverseList(arrToList([2,4,3]))));
- /* @param {ListNode} head | @return {ListNode} */
- var reverseListRecursion = function(head) {
- return reverse(null, head);
- };
- var reverseRecursion = function (newHead, head) {
- if (!head) return newHead;
- var tmp = head.next;
- head.next = newHead;
- return reverse(head, tmp);
- };
- // Time complexity : O(n). Space complexity : O(n).
- // console.log(listToArray(reverseListRecursion(arrToList([2,4,3]))));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement