-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xuqingshan
committed
Aug 4, 2020
1 parent
c321b09
commit 0b66e25
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
34 changes: 34 additions & 0 deletions
34
剑指offer/leetcode/3.longest-substring-without-repeating-characters.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<nums1.length && j<nums2.length) { | ||
if (nums1[i] < nums2[j]) { | ||
arr.push(nums1[i]); | ||
i++; | ||
} else { | ||
arr.push(nums2[j]); | ||
j++; | ||
} | ||
} else if (i<nums1.length ) { | ||
arr = arr.concat(nums1.slice(i)); | ||
break; | ||
} else if (j<nums2.length ) { | ||
arr = arr.concat(nums2.slice(j)); | ||
break; | ||
} | ||
} | ||
if (arr.length % 2) { | ||
return arr[Math.floor(arr.length / 2)] | ||
} else { | ||
return (arr[arr.length / 2] + arr[arr.length / 2 - 1]) / 2; | ||
} | ||
}; | ||
// @lc code=end | ||
console.log(findMedianSortedArrays([1,2], [3, 4])) |