Skip to content

Commit

Permalink
format filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
zongyanqi committed Dec 3, 2017
1 parent b768505 commit 272cb1c
Show file tree
Hide file tree
Showing 82 changed files with 2,824 additions and 259 deletions.
43 changes: 43 additions & 0 deletions 001-Two-Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* https://leetcode.com/problems/two-sum/description/
* Difficulty:Easy
*
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
* Example:
* Given nums = [2, 7, 11, 15], target = 9,
* Because nums[0] + nums[1] = 2 + 7 = 9,
* return [0, 1].
*/

/**
* @param {number[]} numbers
* @param {number} target
* @return {number[]}
*/
var twoSum = function (numbers, target) {

for (var i = 0; i < numbers.length - 1; i++) {
for (var j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) return [i, j];
}
}
};

var twoSum2 = function (numbers, target) {
var map = {};
for (var i = 0; i < numbers.length; i++) {
var n = numbers[i];
if (map[target - n] !== undefined) {
return [map[target - n], i];
} else {
map[n] = i;
}
}
};

console.log(twoSum([2, 11, 15, 7], 9));
console.log(twoSum2([2, 7, 11, 15], 9));
console.log(twoSum2([2, 7, 11, 15], 26));
console.log(twoSum2([2, 7, 11, 15], 26));

49 changes: 49 additions & 0 deletions 002-Add-Two-Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* https://leetcode.com/problems/add-two-numbers/description/
* Difficulty:Medium
*
* 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.
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
* Example
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* Explanation: 342 + 465 = 807.
*/

// Definition for singly-linked list.
function ListNode(val) {
this.val = val;
this.next = null;
}

/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function (l1, l2) {

var c = 0;
var ret = new ListNode(0);
var curr = ret;

while (l1 || l2) {
var a = l1 ? l1.val : 0;
var b = l2 ? l2.val : 0;
var sum = a + b + c;
c = Math.floor(sum / 10);
curr.next = new ListNode(sum % 10);
if (l1) {
l1 = l1.next;
}
if (l2) {
l2 = l2.next;
}
curr = curr.next;
}
if (c) {
curr.next = new ListNode(c);
}

return ret.next;
};
45 changes: 45 additions & 0 deletions 003-Longest-Substring-Without-Repeating-Characters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
* Difficulty:Medium
*
* Given a string, find the length of the longest substring without repeating characters.
* Examples:
* Given "abcabcbb", the answer is "abc", which the length is 3.
* Given "bbbbb", the answer is "b", with the length of 1.
* 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.
*
*/

/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {

var max = 0;
var i = 0;
var j = 0;
var n = s.length;
var map = {};

while (i < n && j < n) {
if (map[s[j]] === undefined) {
map[s[j]] = 1;
j++;
max = Math.max(max, j - i);
} else {
delete map[s[i]];
i++;
}

}

return max;
};

console.log(lengthOfLongestSubstring('c'), 1);
console.log(lengthOfLongestSubstring(''), 0);
console.log(lengthOfLongestSubstring('abcabcbb'), 3);
console.log(lengthOfLongestSubstring('bbbbb'), 1);
console.log(lengthOfLongestSubstring('pwwkew'), 3);
console.log(lengthOfLongestSubstring('xhhyccrcbdczkvzeeubynglxfdedshtpobqsdhufkzgwuhaabdzrlkosnuxibrxssnkxuhcggkecshdvkcmymdqbxolbfjtzyfw'), 14);
90 changes: 90 additions & 0 deletions 004-Median-of-Two-Sorted-Arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* https://leetcode.com/problems/median-of-two-sorted-arrays/description/
* Difficulty:Hard
*
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
*
* Example 1:
* nums1 = [1, 3]
* nums2 = [2]
* The median is 2.0
*
* Example 2:
* nums1 = [1, 2]
* nums2 = [3, 4]
* The median is (2 + 3)/2 = 2.5
* *
*/


function kth(arr1, s1, n1, arr2, s2, n2, k) {
// console.log(arr1, s1, n1, arr2, s2, n2, k);
// console.log('-----------');
if (k < 1 || k > n1 + n2) return -1;

if (n1 > n2) {
return kth(arr2, s2, n2, arr1, s1, n1, k);
}

if (n1 === 0) {
return arr2[s2 + k - 1];
}

if (k === 1) {
return arr1[s1] < arr2[s2] ? arr1[s1] : arr2[s2];
}

var newK = k >> 1;

if (n1 < newK) {
newK = n1;
}

if (arr1[s1 + newK - 1] < arr2[s2 + newK - 1]) {
return kth(arr1, s1 + newK, n1 - newK, arr2, s2, n2, k - newK);
} else {
return kth(arr1, s1, n1, arr2, s2 + newK, n2 - newK, k - newK);
}

}

// var arr1 = [2, 3, 6, 7, 9];
// var arr2 = [1, 4, 8, 10];
// console.log([...arr1, ...arr2].sort(function (a, b) {
// if (a > b) return 1;
// if (a < b) return -1;
// return 0;
// }));
//
// console.log('=======');
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 1), 1);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 2), 2);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 3), 3);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 4), 4);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 5), 6);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 6), 7);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 7), 8);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 8), 9);
// console.log(kth(arr1, 0, 5, arr2, 0, 4, 9), 10);

/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function (nums1, nums2) {

var n1 = nums1.length;
var n2 = nums2.length;

var mid = Math.floor((n1 + n2) / 2);
if ((n1 + n2) % 2 === 0) {
return (kth(nums1, 0, n1, nums2, 0, n2, mid) + kth(nums1, 0, n1, nums2, 0, n2, mid + 1)) / 2;
} else {
return kth(nums1, 0, n1, nums2, 0, n2, mid + 1);
}
};

console.log(findMedianSortedArrays([1, 3, 4], [2, 5]));
console.log(findMedianSortedArrays([1, 3, 4], [2, 5, 6]));
53 changes: 53 additions & 0 deletions 005-Longest-Palindromic-Substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* https://leetcode.com/problems/longest-palindromic-substring/description/
* Difficulty:Medium
*
* Given a string s, find the longest palindromic substring in s.
* You may assume that the maximum length of s is 1000.
*
* Example:
* Input: "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
*
* Example:
* Input: "cbbd"
* Output: "bb"
*/
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
var a = new Date();
var n = s.length;
var res = '';
var dp = [];
while (dp.push(new Array(n).fill(-1)) < n);
// console.log(dp);

for (var i = n - 1; i >= 0; i--) {
for (var j = i; j < n; j++) {
dp[i][j] = s[i] === s[j] && ((j - i < 3) || dp[i + 1][j - 1]);
if (dp[i][j] === undefined) {
console.log(i, j, s[i], s[j], dp[i + 1][j - 1])
}
if (dp[i][j]) {
var tmp = s.substring(i, j + 1);
if (tmp.length > res.length) res = tmp;
}

}
}
// console.log(dp);
console.log(new Date() - a);

return res;
};

// console.log(isPalindrome(s, 1, 3));
// console.log(longestPalindrome('babad'));
// console.log(longestPalindrome(''));
// console.log(longestPalindrome('a'));
// console.log(longestPalindrome('aabbbbbb'));
console.log(longestPalindrome("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
49 changes: 49 additions & 0 deletions 011-Container-With-Most-Water.js

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions 014-Longest-Common-Prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* https://leetcode.com/problems/longest-common-prefix/
* Difficulty:Easy
*
* Write a function to find the longest common prefix string amongst an array of strings.
*/

/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {

var m = strs.length;
if (!m) return '';

var min = Infinity;
var minIndex = -1;
for (var i = 0; i < strs.length; i++) {
if (strs[i].length < min) {
min = strs[i].length;
minIndex = i;
}
}

var s = strs[minIndex];

for (i = 0; i < s.length; i++) {
var ch = strs[0][i];
var same = true;
for (var j = 1; j < m; j++) {
if (strs[j][i] !== ch) {
same = false;
break;
}

}
if (!same) break;

}

return s.substr(0, i);

};

console.log(longestCommonPrefix([
'abdc',
'abc123',
'abc234'
]));
Loading

0 comments on commit 272cb1c

Please sign in to comment.