Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 272cb1c

Browse files
author
zongyanqi
committed
format filenames
1 parent b768505 commit 272cb1c

File tree

82 files changed

+2824
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2824
-259
lines changed

001-Two-Sum.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum/description/
3+
* Difficulty:Easy
4+
*
5+
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
6+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
* Example:
8+
* Given nums = [2, 7, 11, 15], target = 9,
9+
* Because nums[0] + nums[1] = 2 + 7 = 9,
10+
* return [0, 1].
11+
*/
12+
13+
/**
14+
* @param {number[]} numbers
15+
* @param {number} target
16+
* @return {number[]}
17+
*/
18+
var twoSum = function (numbers, target) {
19+
20+
for (var i = 0; i < numbers.length - 1; i++) {
21+
for (var j = i + 1; j < numbers.length; j++) {
22+
if (numbers[i] + numbers[j] === target) return [i, j];
23+
}
24+
}
25+
};
26+
27+
var twoSum2 = function (numbers, target) {
28+
var map = {};
29+
for (var i = 0; i < numbers.length; i++) {
30+
var n = numbers[i];
31+
if (map[target - n] !== undefined) {
32+
return [map[target - n], i];
33+
} else {
34+
map[n] = i;
35+
}
36+
}
37+
};
38+
39+
console.log(twoSum([2, 11, 15, 7], 9));
40+
console.log(twoSum2([2, 7, 11, 15], 9));
41+
console.log(twoSum2([2, 7, 11, 15], 26));
42+
console.log(twoSum2([2, 7, 11, 15], 26));
43+

002-Add-Two-Numbers.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* https://leetcode.com/problems/add-two-numbers/description/
3+
* Difficulty:Medium
4+
*
5+
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
6+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
7+
* Example
8+
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9+
* Output: 7 -> 0 -> 8
10+
* Explanation: 342 + 465 = 807.
11+
*/
12+
13+
// Definition for singly-linked list.
14+
function ListNode(val) {
15+
this.val = val;
16+
this.next = null;
17+
}
18+
19+
/**
20+
* @param {ListNode} l1
21+
* @param {ListNode} l2
22+
* @return {ListNode}
23+
*/
24+
var addTwoNumbers = function (l1, l2) {
25+
26+
var c = 0;
27+
var ret = new ListNode(0);
28+
var curr = ret;
29+
30+
while (l1 || l2) {
31+
var a = l1 ? l1.val : 0;
32+
var b = l2 ? l2.val : 0;
33+
var sum = a + b + c;
34+
c = Math.floor(sum / 10);
35+
curr.next = new ListNode(sum % 10);
36+
if (l1) {
37+
l1 = l1.next;
38+
}
39+
if (l2) {
40+
l2 = l2.next;
41+
}
42+
curr = curr.next;
43+
}
44+
if (c) {
45+
curr.next = new ListNode(c);
46+
}
47+
48+
return ret.next;
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a string, find the length of the longest substring without repeating characters.
6+
* Examples:
7+
* Given "abcabcbb", the answer is "abc", which the length is 3.
8+
* Given "bbbbb", the answer is "b", with the length of 1.
9+
* Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
10+
*
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @return {number}
16+
*/
17+
var lengthOfLongestSubstring = function (s) {
18+
19+
var max = 0;
20+
var i = 0;
21+
var j = 0;
22+
var n = s.length;
23+
var map = {};
24+
25+
while (i < n && j < n) {
26+
if (map[s[j]] === undefined) {
27+
map[s[j]] = 1;
28+
j++;
29+
max = Math.max(max, j - i);
30+
} else {
31+
delete map[s[i]];
32+
i++;
33+
}
34+
35+
}
36+
37+
return max;
38+
};
39+
40+
console.log(lengthOfLongestSubstring('c'), 1);
41+
console.log(lengthOfLongestSubstring(''), 0);
42+
console.log(lengthOfLongestSubstring('abcabcbb'), 3);
43+
console.log(lengthOfLongestSubstring('bbbbb'), 1);
44+
console.log(lengthOfLongestSubstring('pwwkew'), 3);
45+
console.log(lengthOfLongestSubstring('xhhyccrcbdczkvzeeubynglxfdedshtpobqsdhufkzgwuhaabdzrlkosnuxibrxssnkxuhcggkecshdvkcmymdqbxolbfjtzyfw'), 14);

004-Median-of-Two-Sorted-Arrays.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* https://leetcode.com/problems/median-of-two-sorted-arrays/description/
3+
* Difficulty:Hard
4+
*
5+
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
6+
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7+
*
8+
* Example 1:
9+
* nums1 = [1, 3]
10+
* nums2 = [2]
11+
* The median is 2.0
12+
*
13+
* Example 2:
14+
* nums1 = [1, 2]
15+
* nums2 = [3, 4]
16+
* The median is (2 + 3)/2 = 2.5
17+
* *
18+
*/
19+
20+
21+
function kth(arr1, s1, n1, arr2, s2, n2, k) {
22+
// console.log(arr1, s1, n1, arr2, s2, n2, k);
23+
// console.log('-----------');
24+
if (k < 1 || k > n1 + n2) return -1;
25+
26+
if (n1 > n2) {
27+
return kth(arr2, s2, n2, arr1, s1, n1, k);
28+
}
29+
30+
if (n1 === 0) {
31+
return arr2[s2 + k - 1];
32+
}
33+
34+
if (k === 1) {
35+
return arr1[s1] < arr2[s2] ? arr1[s1] : arr2[s2];
36+
}
37+
38+
var newK = k >> 1;
39+
40+
if (n1 < newK) {
41+
newK = n1;
42+
}
43+
44+
if (arr1[s1 + newK - 1] < arr2[s2 + newK - 1]) {
45+
return kth(arr1, s1 + newK, n1 - newK, arr2, s2, n2, k - newK);
46+
} else {
47+
return kth(arr1, s1, n1, arr2, s2 + newK, n2 - newK, k - newK);
48+
}
49+
50+
}
51+
52+
// var arr1 = [2, 3, 6, 7, 9];
53+
// var arr2 = [1, 4, 8, 10];
54+
// console.log([...arr1, ...arr2].sort(function (a, b) {
55+
// if (a > b) return 1;
56+
// if (a < b) return -1;
57+
// return 0;
58+
// }));
59+
//
60+
// console.log('=======');
61+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 1), 1);
62+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 2), 2);
63+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 3), 3);
64+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 4), 4);
65+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 5), 6);
66+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 6), 7);
67+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 7), 8);
68+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 8), 9);
69+
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 9), 10);
70+
71+
/**
72+
* @param {number[]} nums1
73+
* @param {number[]} nums2
74+
* @return {number}
75+
*/
76+
var findMedianSortedArrays = function (nums1, nums2) {
77+
78+
var n1 = nums1.length;
79+
var n2 = nums2.length;
80+
81+
var mid = Math.floor((n1 + n2) / 2);
82+
if ((n1 + n2) % 2 === 0) {
83+
return (kth(nums1, 0, n1, nums2, 0, n2, mid) + kth(nums1, 0, n1, nums2, 0, n2, mid + 1)) / 2;
84+
} else {
85+
return kth(nums1, 0, n1, nums2, 0, n2, mid + 1);
86+
}
87+
};
88+
89+
console.log(findMedianSortedArrays([1, 3, 4], [2, 5]));
90+
console.log(findMedianSortedArrays([1, 3, 4], [2, 5, 6]));

005-Longest-Palindromic-Substring.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindromic-substring/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a string s, find the longest palindromic substring in s.
6+
* You may assume that the maximum length of s is 1000.
7+
*
8+
* Example:
9+
* Input: "babad"
10+
* Output: "bab"
11+
* Note: "aba" is also a valid answer.
12+
*
13+
* Example:
14+
* Input: "cbbd"
15+
* Output: "bb"
16+
*/
17+
/**
18+
* @param {string} s
19+
* @return {string}
20+
*/
21+
var longestPalindrome = function (s) {
22+
var a = new Date();
23+
var n = s.length;
24+
var res = '';
25+
var dp = [];
26+
while (dp.push(new Array(n).fill(-1)) < n);
27+
// console.log(dp);
28+
29+
for (var i = n - 1; i >= 0; i--) {
30+
for (var j = i; j < n; j++) {
31+
dp[i][j] = s[i] === s[j] && ((j - i < 3) || dp[i + 1][j - 1]);
32+
if (dp[i][j] === undefined) {
33+
console.log(i, j, s[i], s[j], dp[i + 1][j - 1])
34+
}
35+
if (dp[i][j]) {
36+
var tmp = s.substring(i, j + 1);
37+
if (tmp.length > res.length) res = tmp;
38+
}
39+
40+
}
41+
}
42+
// console.log(dp);
43+
console.log(new Date() - a);
44+
45+
return res;
46+
};
47+
48+
// console.log(isPalindrome(s, 1, 3));
49+
// console.log(longestPalindrome('babad'));
50+
// console.log(longestPalindrome(''));
51+
// console.log(longestPalindrome('a'));
52+
// console.log(longestPalindrome('aabbbbbb'));
53+
console.log(longestPalindrome("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));

011-Container-With-Most-Water.js

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

014-Longest-Common-Prefix.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* https://leetcode.com/problems/longest-common-prefix/
3+
* Difficulty:Easy
4+
*
5+
* Write a function to find the longest common prefix string amongst an array of strings.
6+
*/
7+
8+
/**
9+
* @param {string[]} strs
10+
* @return {string}
11+
*/
12+
var longestCommonPrefix = function (strs) {
13+
14+
var m = strs.length;
15+
if (!m) return '';
16+
17+
var min = Infinity;
18+
var minIndex = -1;
19+
for (var i = 0; i < strs.length; i++) {
20+
if (strs[i].length < min) {
21+
min = strs[i].length;
22+
minIndex = i;
23+
}
24+
}
25+
26+
var s = strs[minIndex];
27+
28+
for (i = 0; i < s.length; i++) {
29+
var ch = strs[0][i];
30+
var same = true;
31+
for (var j = 1; j < m; j++) {
32+
if (strs[j][i] !== ch) {
33+
same = false;
34+
break;
35+
}
36+
37+
}
38+
if (!same) break;
39+
40+
}
41+
42+
return s.substr(0, i);
43+
44+
};
45+
46+
console.log(longestCommonPrefix([
47+
'abdc',
48+
'abc123',
49+
'abc234'
50+
]));

0 commit comments

Comments
 (0)