# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
res=ListNode(0)
tmp=res
while l1 != None and l2 != None:
if l1.val < l2.val:
tmp ...- read more
19. Remove Nth Node From End of List
read moreclass ListNode(object): def __init__(self, x): self.val = x self.next = None res = ListNode(0) res.next = head tmp = res # head 1-5, tmp0-5 for i in range(0, n): # double pointer, a.head from 1 to None is 5 steps, forward 2. This means the 1st pointer firstly complete ...
18. 4Sum
read moreclass Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ nums.sort() mylist = [] if len(nums) < 4: return [] else: for i in range(0,len(nums)-3): if i != 0 and nums[i] == nums[i-1]: continue for j in range ...
17. Letter Combinations of a Phone Number
read moreclass Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ map = {"2": ["a", "b", "c"], "3": ["d", "e", "f"], "4": ["g", "h", "i"], "5": ["j", "k", "l"], "6": ["m", "n", "o"], "7": ["p", "q", "r", "s"], "8": ["t", "u", "v"], "9": ["w", "x", "y", "z"]} if len ...
16. 3Sum Closest
read moreclass Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ nums = sorted(nums) ans = sum(nums[:3]) if len(nums) < 3: return 0 elif list(set(nums)) == [0]: return 0 else: for i in range(len(nums)): if nums[i] == nums[i - 1 ...
15. 3Sum
read moreclass Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ # # method 1 brute force # mylist=[] # if len(nums)<3: # return [] # else: # for i in range(len(nums)): # for j in range(len(nums[i + 1:])): # if (0-nums[i]-nums[i + 1:][j]) in nums[i ...
14. Longest Common Prefix
read moreclass Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if len(strs) <= 1: return strs[0] if len(strs) == 1 else "" end, minl = 0, min([len(s) for s in strs]) while end < minl: for i in range(1, len(strs)): if strs[i][end] != strs ...
13. Roman to Integer
read moreclass Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ values = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000] numerals = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'] sum = 0 s = s[::-1] while s != "": if s[:2 ...
12. Integer to Roman
read moreclass Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] list = '' for i in range(0, len(values)): while num ...
11. Container With Most Water
read moreclass Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ end = len(height) - 1 start = 0 max_volume = 0 while end > start: if height[end] >= height[start]: v_height = height[start] start += 1 else: v_height = height[end] end -= 1 if max_volume < (end - start + 1) * v_height: max_volume = (end - start + 1 ...