class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
if head == None or head.next == None:
return head
dummy = ListNode(0);
dummy.next = head
p = dummy
while p.next and p.next.next:
tmp = p.next.next
p.next.next ...- read more
22. Generate Parentheses
read moreclass Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ mylist=[] if n == 0: return [] else: if n == 1: return ["()"] else: for item in self.generateParenthesis(n-1): for i in range(0,(n-1)*2): newp="("+item[:i]+")"+item[i:] if newp not in mylist: mylist ...
21. Merge Two Sorted Lists
read more# 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 ...
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 ...