1. 24. Swap Nodes in Pairs

    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
  2. 22. Generate Parentheses

    class 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 ...
    read more
  3. 21. Merge Two Sorted Lists

    # 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
  4. 19. Remove Nth Node From End of List

    class 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 ...
    read more
  5. 18. 4Sum

    class 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 ...
    read more
  6. 17. Letter Combinations of a Phone Number

    class 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 ...
    read more
  7. 16. 3Sum Closest

    class 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 ...
    read more
  8. 15. 3Sum

    class 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 ...
    read more
  9. 14. Longest Common Prefix

    class 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 ...
    read more
  10. 13. Roman to Integer

    class 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 ...
    read more

« Page 14 / 22 »

blogroll

social