1.Two Sum
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].
class Solution:
def twoSum(self, nums, target):
for i in range(len(nums)-1):
for j in range(len(nums)):
if (nums[i]+nums[j])==target and j>i:
return [i,j]
暴力直接过
2.Add Two Numbers
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.
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is None:
return l2
elif l2 is None:
return l1
else:
carry = 0
ret = ListNode(0)
ret_Last = ret
while (l1 or l2):
sum = 0
if (l1):
sum = l1.val
l1 = l1.next
if (l2):
sum += l2.val
l2 = l2.next
sum += carry
ret_Last.next = ListNode(sum % 10)
ret_Last = ret_Last.next
carry = (sum >= 10)
if (carry):
ret_Last.next = ListNode(1)
ret_Last = ret.next
del ret
return ret_Last
3.Longest Substring Without Repeating Characters
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.
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
max = 0
curmax = 0
substr = "" # 当前的子字符串
i = 0
length = len(s)
while i < length:
ind = substr.find(s[i])
if ind == -1: # no containing item
substr += s[i]
curmax += 1
else:
if max < curmax:
max = curmax
i = i - len(substr) + ind # 回溯
substr = ""
curmax = 0
i += 1
if max < curmax:
max = curmax
return max
4.Median of Two Sorted Arrays
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)).
You may assume nums1 and nums2 cannot be both empty.
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
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
num = nums1+nums2
num = sorted(num)
l = len(num)
if l%2!=0:
return float(num[l//2])
else:
return float(num[l//2]+num[(l//2)-1])/2
5.Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
lens = len(s)
if lens < 2:
return s
maxlen = 0
maxl = 0
maxr = 0
i = 0
while i < lens:
if (lens - i) < maxlen // 2:
break
j = i
k = i
while (k < lens - 1) and (s[k + 1] == s[j]): # 寻找最大相同字符的子串作为核
k = k + 1
i = k + 1 # 跳过同一核中的其他i
while (j > 0) and (k < lens - 1) and (s[j - 1] == s[k + 1]):
j = j - 1
k = k + 1
if k - j + 1 > maxlen:
maxlen = k - j + 1
maxl = j
maxr = k
return s[maxl:maxr + 1]