Skip to content

Commit 7b4e65c

Browse files
author
loatheb
committed
update(leetcode-js): add JS solution in leetcode.com
0 parents  commit 7b4e65c

File tree

195 files changed

+8540
-0
lines changed

Some content is hidden

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

195 files changed

+8540
-0
lines changed

001-two-sum/two-sum.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
2+
//
3+
// You may assume that each input would have exactly one solution.
4+
//
5+
//
6+
// Example:
7+
//
8+
// Given nums = [2, 7, 11, 15], target = 9,
9+
//
10+
// Because nums[0] + nums[1] = 2 + 7 = 9,
11+
// return [0, 1].
12+
//
13+
//
14+
//
15+
//
16+
// UPDATE (2016/2/13):
17+
// The return format had been changed to zero-based indices. Please read the above updated description carefully.
18+
19+
20+
/**
21+
* @param {number[]} nums
22+
* @param {number} target
23+
* @return {number[]}
24+
*/
25+
var twoSum = function(nums, target) {
26+
const hmap = {}
27+
for (var idx=0; idx<nums.length; idx++) {
28+
var val = nums[idx]
29+
var delta = target - val
30+
var deltaIdx = hmap[delta]
31+
if (deltaIdx !== undefined && deltaIdx !== idx) {
32+
return [deltaIdx, idx]
33+
}
34+
hmap[val] = idx
35+
}
36+
return []
37+
};

001-two-sum/two-sum.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given an array of integers, return indices of the two numbers such that they add up to a specific target.
5+
#
6+
# You may assume that each input would have exactly one solution.
7+
#
8+
#
9+
# Example:
10+
#
11+
# Given nums = [2, 7, 11, 15], target = 9,
12+
#
13+
# Because nums[0] + nums[1] = 2 + 7 = 9,
14+
# return [0, 1].
15+
#
16+
#
17+
#
18+
#
19+
# UPDATE (2016/2/13):
20+
# The return format had been changed to zero-based indices. Please read the above updated description carefully.
21+
22+
23+
class Solution(object):
24+
def twoSum(self, nums, target):
25+
"""
26+
:type nums: List[int]
27+
:type target: int
28+
:rtype: List[int]
29+
"""
30+
d = {}
31+
for i, v in enumerate(nums):
32+
if v in d:
33+
return [d[v],i]
34+
d[target-v] = i
35+
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// You are given two linked lists representing two non-negative numbers. 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.
2+
//
3+
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
4+
// Output: 7 -> 0 -> 8
5+
6+
7+
/**
8+
* Definition for singly-linked list.
9+
* function ListNode(val) {
10+
* this.val = val;
11+
* this.next = null;
12+
* }
13+
*/
14+
/**
15+
* @param {ListNode} l1
16+
* @param {ListNode} l2
17+
* @return {ListNode}
18+
*/
19+
var addTwoNumbers = function(l1, l2) {
20+
if (!l1) return l2;
21+
if (!l2) return l1;
22+
var l3 = new ListNode(0), fakeHead = l3, carry = 0, sum = 0;
23+
while (l1 || l2 || carry) {
24+
sum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
25+
l3.next = new ListNode(sum % 10);
26+
carry = Math.floor(sum / 10);
27+
l3 = l3.next; l1 = l1 && l1.next; l2 = l2 && l2.next;
28+
}
29+
return fakeHead.next;
30+
};
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# You are given two linked lists representing two non-negative numbers. 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.
5+
#
6+
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
7+
# Output: 7 -> 0 -> 8
8+
9+
10+
# Definition for singly-linked list.
11+
12+
# class ListNode(object):
13+
# def __init__(self, x):
14+
# self.val = x
15+
# self.next = None
16+
17+
class Solution(object):
18+
def addTwoNumbers(self, l1, l2):
19+
"""
20+
:type l1: ListNode
21+
:type l2: ListNode
22+
:rtype: ListNode
23+
"""
24+
if(l1 is None and l2 is None):
25+
return None
26+
27+
head = ListNode(0)
28+
point = head
29+
carry = 0
30+
while l1 is not None and l2 is not None:
31+
s = carry + l1.val + l2.val
32+
point.next = ListNode(s % 10)
33+
carry = s / 10
34+
l1 = l1.next
35+
l2 = l2.next
36+
point = point.next
37+
38+
while l1 is not None:
39+
s = carry + l1.val
40+
point.next = ListNode(s % 10)
41+
carry = s / 10
42+
l1 = l1.next
43+
point = point.next
44+
45+
while l2 is not None:
46+
s = carry + l2.val
47+
point.next = ListNode(s % 10)
48+
carry = s / 10
49+
l2 = l2.next
50+
point = point.next
51+
52+
if carry != 0:
53+
point.next = ListNode(carry)
54+
55+
return head.next
56+
57+
58+
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Given a string, find the length of the longest substring without repeating characters.
2+
//
3+
// Examples:
4+
//
5+
// Given "abcabcbb", the answer is "abc", which the length is 3.
6+
//
7+
// Given "bbbbb", the answer is "b", with the length of 1.
8+
//
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+
* @param {string} s
14+
* @return {number}
15+
*/
16+
var lengthOfLongestSubstring = function(s) {
17+
var hashMap = {};
18+
var i = j = 0;
19+
var n = s.length;
20+
var len = 0;
21+
while (i < n && j < n) {
22+
if (!hashMap[s[i]]) {
23+
hashMap[s[i++]] = true;
24+
len = Math.max(len, i - j);
25+
} else {
26+
hashMap[s[j++]] = false;
27+
}
28+
}
29+
return len;
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given a string, find the length of the longest substring without repeating characters.
5+
#
6+
# Examples:
7+
#
8+
# Given "abcabcbb", the answer is "abc", which the length is 3.
9+
#
10+
# Given "bbbbb", the answer is "b", with the length of 1.
11+
#
12+
# 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.
13+
14+
15+
class Solution(object):
16+
def lengthOfLongestSubstring(self, s):
17+
"""
18+
:type s: str
19+
:rtype: int
20+
"""
21+
22+
longest, start, visited = 0, 0, [False for _ in range(256)]
23+
for ind, val in enumerate(s):
24+
if not visited[ord(val)]:
25+
visited[ord(val)] = True
26+
else:
27+
while val != s[start]:
28+
visited[ord(s[start])] = False
29+
start += 1
30+
start += 1
31+
longest = max(longest, ind - start + 1)
32+
return longest
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# There are two sorted arrays nums1 and nums2 of size m and n respectively.
5+
#
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+
#
10+
# nums1 = [1, 3]
11+
# nums2 = [2]
12+
#
13+
# The median is 2.0
14+
#
15+
#
16+
#
17+
# Example 2:
18+
#
19+
# nums1 = [1, 2]
20+
# nums2 = [3, 4]
21+
#
22+
# The median is (2 + 3)/2 = 2.5
23+
24+
25+
class Solution(object):
26+
def findMedianSortedArrays(self, nums1, nums2):
27+
"""
28+
:type nums1: List[int]
29+
:type nums2: List[int]
30+
:rtype: float
31+
"""
32+
nums = sorted(nums1 + nums2)
33+
t_len = len(nums)
34+
if t_len == 1:
35+
return nums[0]
36+
37+
if t_len % 2:
38+
return nums[t_len/2]
39+
else:
40+
return (nums[t_len/2] + nums[t_len/2 -1]) /2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
2+
//
3+
// Example:
4+
//
5+
// Input: "babad"
6+
//
7+
// Output: "bab"
8+
//
9+
// Note: "aba" is also a valid answer.
10+
//
11+
//
12+
//
13+
// Example:
14+
//
15+
// Input: "cbbd"
16+
//
17+
// Output: "bb"
18+
19+
20+
/**
21+
* @param {string} s
22+
* @return {string}
23+
*/
24+
var longestPalindrome = function(s) {
25+
var a = s.split(''),left, right,
26+
size = a.length,
27+
max = Number.MIN_VALUE,
28+
start = 0;
29+
30+
for(var i = 0; i < size; i = i + 0.5){
31+
left = Math.ceil(i - 1);
32+
right = Math.floor(i + 1);
33+
while(left >=0 && right < size) {
34+
if (a[left] === a[right]){
35+
left--;
36+
right++;
37+
} else { break;}
38+
}
39+
if (right - left - 1 > max){
40+
max = right - left - 1;
41+
start = left + 1;
42+
}
43+
}
44+
45+
return s.slice(start, start + max);
46+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
5+
#
6+
# Example:
7+
#
8+
# Input: "babad"
9+
#
10+
# Output: "bab"
11+
#
12+
# Note: "aba" is also a valid answer.
13+
#
14+
#
15+
#
16+
# Example:
17+
#
18+
# Input: "cbbd"
19+
#
20+
# Output: "bb"
21+
22+
23+
class Solution(object):
24+
def longestPalindrome(self, s):
25+
"""
26+
:type s: str
27+
:rtype: str
28+
"""
29+
longest, mid = "", (len(s) - 1) / 2
30+
i, j = mid, mid
31+
while i >= 0 and j < len(s):
32+
args = [(s, i, i), (s, i, i + 1), (s, j, j), (s, j, j + 1)]
33+
for arg in args:
34+
tmp = self.longestPalindromeByAxis(*arg)
35+
if len(tmp) > len(longest):
36+
longest = tmp
37+
if len(longest) >= i * 2:
38+
if len(longest) == 1:
39+
return s[0]
40+
return longest
41+
i, j = i - 1, j + 1
42+
return longest
43+
44+
def longestPalindromeByAxis(self, s, left, right):
45+
while left >= 0 and right < len(s) and s[left] == s[right]:
46+
left, right = left - 1, right + 1
47+
return s[left + 1: right]

0 commit comments

Comments
 (0)