1. infix-prefix-postfix

    # -*- coding: utf-8 -*-
    from pythonds.basic.stack import Stack
    # 中缀转后缀
    def infixToPostfix(infixexpr):
        prec = {}
        prec["*"] = 3
        prec["/"] = 3
        prec["+"] = 2
        prec["-"] = 2
        prec["("] = 1
        opStack = Stack()
        postfixList = []
        tokenList = infixexpr.split()
    
        for token in tokenList:
            if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
                postfixList.append(token)
            elif token == '(':
                opStack.push(token)
            elif ...
    read more
  2. 7 Sort Methods

    # -*- coding: utf-8 -*-
    # 一、冒泡排序 BubbleSort,时间复杂度O(n2)
    # 冒泡排序的原理非常简单,它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。
    # 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
    # 对第0个到第n-1个数据做同样的工作。这时,最大的数就“浮”到了数组最后的位置上。
    # 针对所有的元素重复以上的步骤,除了最后一个。
    # 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
    def bubble_sort(arry):
        n = len(arry)
        for i in range(n):
            for j in range(1,n-i):    # 从第二个数到上次沉淀的数前的数,开始往前比较
                if arry[j-1] > arry[j]:
                    arry ...
    read more
  3. 50. Pow(x, n)

    class Solution(object):
        def myPow(self, x, n):
            """
            :type x: float
            :type n: int
            :rtype: float
            """
            flag = 0
            if n < 0:
                flag = 1
                n = abs(n)
            res = 1
            while n > 0:
                if n % 2 == 1:
                    res = res * x
                x = x * x
                n >>= 1
            if flag:
                return 1/res
            else:
                return ...
    read more
  4. 49. Group Anagrams

    class Solution(object):
        def groupAnagrams(self, strs):
            """
            :type strs: List[str]
            :rtype: List[List[str]]
            """
            map = {}
            for item in strs:
                order = ("".join(sorted(item))).lower()
                if order not in map:
                    map[order] = [item.lower()]
                else:
                    map[order] += [item.lower()]
    
            l = []
            for i in map.keys():
                l.append(map[i])
            return ...
    read more
  5. 48. Rotate Image

    # -*- coding: utf-8 -*-
    #首先沿逆对角线翻转一次,然后按x轴中线翻转一次。
    class Solution(object):
        def rotate(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: void Do not return anything, modify matrix in-place instead.
            """
            n=len(matrix)
            for i in range(n):
                for j in range(n-i-1):
                    matrix[i][j], matrix[n - 1 - j][n - 1 ...
    read more
  6. 47. Permutations II

    class Solution(object):
        def permute(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
    
            self.result = []
            nums.sort()
            for item in list(set(nums)):
                i = nums.index(item)
                length = len(nums)
                self.recursive_comb([nums[i]], nums[:i] + nums[i + 1:], length)
            return self.result
    
        def recursive_comb(self, temp_array, rest_array ...
    read more
  7. 46. Permutations

    class Solution(object):
        def permute(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
    
            self.result = []
            nums.sort()
            for i in range(len(nums)):
                length=len(nums)
                self.recursive_comb([nums[i]], nums[:i] + nums[i + 1:], length)
            return self.result
    
    
        def recursive_comb(self,temp_array, rest_array, target):
            if len(temp_array ...
    read more
  8. 43. Multiply Strings

    class Solution(object):
        def multiply(self, num1, num2):
            """
            :type num1: str
            :type num2: str
            :rtype: str
            """
            list1 = list(num1)
            list2 = list(num2)
    
            int1 = 0
            for i in list1:
                int1 = int1 * 10 + (ord(i)-ord("0"))
    
            int2 = 0
            for j in list2:
                int2 = int2 * 10 + (ord(j) - ord("0"))
    
            return str ...
    read more
  9. 40. Combination Sum II

    # -*- coding: utf-8 -*-
    class Solution(object):
        def combinationSum2(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
    
            self.result = []
            candidates.sort()
            for i in range(len(candidates)):
                self.recursive_comb([candidates[i]], candidates[i + 1:], target)
            return self.result
    
    
        def recursive_comb(self,temp_array, rest_array, target):
            if sum ...
    read more
  10. 39. Combination Sum

    # -*- coding: utf-8 -*-
    class Solution(object):
        def combinationSum(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
    
            self.result = []
            candidates.sort()
            for i in range(len(candidates)):
                self.recursive_comb([candidates[i]], candidates[i:], target)
            return self.result
    
    
        def recursive_comb(self, temp_array, rest_array, target):
            if sum(temp_array ...
    read more

« Page 13 / 23 »

blogroll

social