From 0b66e25fc2c9c10aa5e979a76de769abade38399 Mon Sep 17 00:00:00 2001 From: xuqingshan Date: Wed, 5 Aug 2020 06:50:49 +0800 Subject: [PATCH] feat: leetcode --- .../leetcode/1.two-sum.js" | 24 ++++++++++ .../leetcode/2.add-two-numbers.js" | 37 ++++++++++++++++ ...substring-without-repeating-characters.js" | 34 ++++++++++++++ .../4.median-of-two-sorted-arrays.js" | 44 +++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 "\345\211\221\346\214\207offer/leetcode/1.two-sum.js" create mode 100644 "\345\211\221\346\214\207offer/leetcode/2.add-two-numbers.js" create mode 100644 "\345\211\221\346\214\207offer/leetcode/3.longest-substring-without-repeating-characters.js" create mode 100644 "\345\211\221\346\214\207offer/leetcode/4.median-of-two-sorted-arrays.js" diff --git "a/\345\211\221\346\214\207offer/leetcode/1.two-sum.js" "b/\345\211\221\346\214\207offer/leetcode/1.two-sum.js" new file mode 100644 index 0000000..be72f18 --- /dev/null +++ "b/\345\211\221\346\214\207offer/leetcode/1.two-sum.js" @@ -0,0 +1,24 @@ +/* + * @lc app=leetcode id=1 lang=javascript + * + * [1] Two Sum + */ + +// @lc code=start +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + const obj = {}; + for (let i = 0; i < nums.length; i++) { + if (obj[target - nums[i]] !== undefined) { + return [obj[target - nums[i]], i]; + } else { + obj[nums[i]] = i; + } + } +}; +// @lc code=end + diff --git "a/\345\211\221\346\214\207offer/leetcode/2.add-two-numbers.js" "b/\345\211\221\346\214\207offer/leetcode/2.add-two-numbers.js" new file mode 100644 index 0000000..4e094f3 --- /dev/null +++ "b/\345\211\221\346\214\207offer/leetcode/2.add-two-numbers.js" @@ -0,0 +1,37 @@ +/* + * @lc app=leetcode id=2 lang=javascript + * + * [2] Add Two Numbers + */ + +// @lc code=start +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var addTwoNumbers = function(l1, l2, carry=0) { + if (!l1 && !l2) { + return null; + } else { + l1 = l1 ? l1 : new ListNode(0); + l2 = l2 ? l2 : new ListNode(0); + } + let total = l1.val + l2.val + carry; + let next = null; + if (l1.next || l2.next) { + next = addTwoNumbers(l1.next, l2.next, total <= 9 ? 0 : 1); + } else if (total > 9) { + next = new ListNode(1); + } + return new ListNode(total % 10, next); +}; +// @lc code=end + diff --git "a/\345\211\221\346\214\207offer/leetcode/3.longest-substring-without-repeating-characters.js" "b/\345\211\221\346\214\207offer/leetcode/3.longest-substring-without-repeating-characters.js" new file mode 100644 index 0000000..73caa04 --- /dev/null +++ "b/\345\211\221\346\214\207offer/leetcode/3.longest-substring-without-repeating-characters.js" @@ -0,0 +1,34 @@ +/* + * @lc app=leetcode id=3 lang=javascript + * + * [3] Longest Substring Without Repeating Characters + */ + +// @lc code=start +/** + * @param {string} s + * @return {number} + * 递归,如果当前已有3个不一样,则将当前位置和前面三个比较,算出来新的结果和下标; + */ +var lengthOfLongestSubstring = function(s) { + if (!s || s.length === 1) return s.length; + function recur (s, current, prev, max) { + // 如果已到最后,则终止; + max = Math.max(max, current - prev); + if (current >= s.length) { + return max; + } else { + for (let i = prev; i < current; i++) { + if (s[i] === s[current]) { + prev = i + 1; + } + } + } + current++; + return recur(s, current, prev, max) + } + return recur(s, 1, 0, 1); +}; +// @lc code=end + + diff --git "a/\345\211\221\346\214\207offer/leetcode/4.median-of-two-sorted-arrays.js" "b/\345\211\221\346\214\207offer/leetcode/4.median-of-two-sorted-arrays.js" new file mode 100644 index 0000000..19278b8 --- /dev/null +++ "b/\345\211\221\346\214\207offer/leetcode/4.median-of-two-sorted-arrays.js" @@ -0,0 +1,44 @@ +/* + * @lc app=leetcode id=4 lang=javascript + * + * [4] Median of Two Sorted Arrays + */ + +// @lc code=start +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number} + */ +var findMedianSortedArrays = function(nums1, nums2) { + // 情况1,num1和num2无重叠,取合并数组中间值即可。 [1,2] [3,4] + // 情况2,nums1和2有重叠,一方完全包含另一方 [1,2,3,4,5,6] [2,3,4,5] + // 情况3,有重叠,[1,2,3,4] [2,3,4,5] + // 时间复杂度太高,如何降下来 + let arr = []; + let i = 0; j = 0; + while(i + j < nums1.length + nums2.length) { + if (i