8000 add Readme and solution file download by leetcode_generate · fancymax/leetcode@2982e4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2982e4a

Browse files
committed
add Readme and solution file download by leetcode_generate
1 parent 6baf285 commit 2982e4a

File tree

56 files changed

+3012
-1337
lines changed

Some content is hidden

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

56 files changed

+3012
-1337
lines changed

001-two-sum/two-sum.py

Lines changed: 35 additions & 0 deletions
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+
Lines changed: 59 additions & 0 deletions
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+
Lines changed: 33 additions & 0 deletions
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+
Lines changed: 40 additions & 0 deletions
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
Lines changed: 47 additions & 0 deletions
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]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
5+
#
6+
# P A H N
7+
# A P L S I I G
8+
# Y I R
9+
#
10+
#
11+
# And then read line by line: "PAHNAPLSIIGYIR"
12+
#
13+
#
14+
# Write the code that will take a string and make this conversion given a number of rows:
15+
#
16+
# string convert(string text, int nRows);
17+
#
18+
# convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
19+
20+
21+
class Solution(object):
22+
def convert(self, s, numRows):
23+
"""
24+
:type s: str
25+
:type numRows: int
26+
:rtype: str
27+
"""
28+
if not s or len(s) == 0 or numRows <= 0:
29+
return ""
30+
if numRows == 1:
31+
return s
32+
if len(s) % (numRows + numRows - 2):
33+
s = s + '#' * (numRows + numRows - 2 - (len(s) % (numRows + numRows - 2)))
34+
blocks = len(s)/(numRows + numRows - 2)
35+
res = ''
36+
for i in range(numRows):
37+
for j in range(blocks):
38+
if i == 0 or i == numRows-1:
39+
res += s[i + j*(numRows + numRows - 2)]
40+
else:
41+
res += s[i + j*(numRows + numRows - 2)]
42+
res += s[2*numRows-2-i + j*(numRows + numRows - 2)]
43+
return ''.join(res.split('#'))
44+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Reverse digits of an integer.
5+
#
6+
#
7+
# Example1: x = 123, return 321
8+
# Example2: x = -123, return -321
9+
#
10+
#
11+
# click to show spoilers.
12+
#
13+
# Have you thought about this?
14+
#
15+
# Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
16+
#
17+
# If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
18+
#
19+
# Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
20+
#
21+
# For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
22+
#
23+
#
24+
# Update (2014-11-10):
25+
# Test cases had been added to test the overflow behavior.
26+
27+
28+
class Solution(object):
29+
def reverse(self, x):
30+
"""
31+
:type x: int
32+
:rtype: int
33+
"""
34+
l = list(str(abs(x)))
35+
l.reverse()
36+
rst = int(''.join(l))
37+
if rst > 2147483647:
38+
return 0
39+
else:
40+
return rst if x>=0 else rst * (-1)
41+
42+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Implement atoi to convert a string to an integer.
5+
#
6+
# Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
7+
#
8+
#
9+
# Notes:
10+
# It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
11+
#
12+
#
13+
# Update (2015-02-10):
14+
# The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
15+
#
16+
#
17+
# spoilers alert... click to show requirements for atoi.
18+
#
19+
# Requirements for atoi:
20+
#
21+
# The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
22+
#
23+
# The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
24+
#
25+
# If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
26+
#
27+
# If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
28+
29+
30+
class Solution(object):
31+
def myAtoi(self, str):
32+
"""
33+
:type str: str
34+
:rtype: int
35+
"""
36+
import re
37+
p = re.compile(r'^[+-]?[0-9]+')
38+
m = re.match(p, str.strip())
39+
if m:
40+
ret = int(m.group())
41+
if ret < -0x80000000:
42+
return -0x80000000
43+
elif ret > 0x7fffffff:
44+
return 0x7fffffff
45+
return ret
46+
else:
47+
return 0
48+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Determine whether an integer is a palindrome. Do this without extra space.
5+
#
6+
# click to show spoilers.
7+
#
8+
# Some hints:
9+
#
10+
# Could negative integers be palindromes? (ie, -1)
11+
#
12+
# If you are thinking of converting the integer to string, note the restriction of using extra space.
13+
#
14+
# You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
15+
#
16+
# There is a more generic way of solving this problem.
17+
18+
19+
class Solution(object):
20+
def isPalindrome(self, x):
21+
"""
22+
:type x: int
23+
:rtype: bool
24+
"""
25+
if x < 0:
26+
return False
27+
x = abs(x)
28+
l = len(str(x))
29+
i = 1
30+
while i < l / 2 + 1:
31+
32+
head = (x / 10 ** (l-i)) % 10
33+
tail = (x % 10 ** i) if i == 1 else (x % 10 ** i) / (10 ** (i-1))
34+
if head != tail:
35+
return False
36+
i = i + 1
37+
38+
return True

0 commit comments

Comments
 (0)
0