算法题练习

找零

给定数组arr,arr中所有的值都是正数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim代表要找的钱数,求组成aim的最少货币数。(拓展,如果问有多少种不同的找零方法呢)

举例:

arr[5,2,3],aim=20。 4张5元可以组成20元,其他的找钱方案都要使用更多张的货币,所以返回4。
f [ i ] 为最少用多少张组成 i 数额的钱

f [ i ] = m i n ( f [ i m o n e y 1 ] + 1 , f [ i m o n e y 2 ] + 1 , )

class Solution:
    """
    @param money: a list of integer
    @param aim: a total amount of money amount
    @return: the fewest number of coins that you need to make up
    """
    def coinChange(self, money, aim):
        # write your code here
        nums=[float('inf')]* (aim+1)
        nums[0]=0
        for i in range(1,aim+1):
            for m in money:
                if i>=m:
                    nums[i]=min(nums[i],nums[i-m]+1)
        if nums[-1]==float('inf'):
            return -1
        return nums[-1]
拓展:组成target的组合数量,找零2
class Solution:
    """
    @param amount: a total amount of money amount
    @param coins: the denomination of each coin
    @return: the number of combinations that make up the amount
    """
    def change(self, amount, coins):
        # write your code here
        #f[i]多少种方式组成amount
        f=[0]*(amount+1)
        f[0]=1

        for coin in coins:
            for value in range(coin,amount+1):
                f[value]+=f[value-coin]
        return f[-1]

Number of 1 bit

def numBits(n):
    count=0
    while n>0:
        count+=n&1
        n=n>>1
    return count

# n& (n-1)按位与可以将最低位的1转为0
def numBits2(n):
    count=0
    while n!=0:
        count=count+1
        n=n&(n-1)
    return count

Hamming Distance

def HammingDis(x,y):
    res=x^y #异或
    return countingBits(res)

海量数据相似度计算之simhash和海明距离

回型打印数组 spiral Matrix

回形打印数组,例如给定数组:
2 6 9 1 7
3 11 14 4 6
0 5 8 10 3

按顺时针由外往内不重复输出
2 6 9 1 7 6 3 10 8 5 0 3 11 14 4
输入一个二维整形数组arr[m][n],回形输出一串数字

Easy to understand solution
中间步骤:
这里写图片描述

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if not matrix:
            return []
        if not matrix[0]:
            return []
        i,j,di,dj=0,0,0,1
        m=len(matrix); n=len(matrix[0])
        res=[]
        for v in range(m*n):
            res.append(matrix[i][j])
            matrix[i][j]=""
            if matrix[(i+di)%m][(j+dj)%n]=="":
                temp=di
                di=dj
                dj=-temp
            i+=di
            j+=dj

        return res      

Spiral Matrix 2

class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        mat=[[0]*n for _ in range(n)]
        i,j,di,dj=0,0,0,1
        for k in range(n*n):
            mat[i][j]=k+1
            if mat[(i+di)%n][(j+dj)%n]:
                print (i,j)
                print ((i+di)%n,(j+dj)%n)
                print ("\n")
                di,dj=dj,-di

            i=i+di
            j=j+dj

        return mat

打印回形数

width,height=list(map(int,input().strip().split()))

mat=[[-1]*(width) for i in range(height)]

v=0
i,j,di,dj=0,0,0,1
for index in range(width*height):
    mat[i][j]=v
    if mat[(i+di)%height][(j+dj)%width]!=-1:
        temp=di
        di=dj
        dj=-temp
        v=v+1
    i+=di
    j+=dj

for row in mat:
    print ("".join(list(map(str,row))))

Reverse Linked List

class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        p=head
        pre=None
        while p:
            temp=p.next
            p.next=pre
            pre=p
            p=temp
        return pre

Reverse Linked List 2

s = "Python syntax highlighting"
print s
s = "Python syntax highlighting"
print s

猜你喜欢

转载自blog.csdn.net/XindiOntheWay/article/details/81750493