python---小算法解释

来源:互联网 发布:文员基本办公软件 编辑:程序博客网 时间:2024/06/08 03:35
def rotate(nums, k):    """    :type nums: List[int]    :type k: int    :rtype: void Do not return anything, modify nums in-place instead.    """    n = len(nums)    k = k % n    reverse(nums, 0, n - k - 1)  #1    print (nums)    reverse(nums, n - k, n - 1) #2    print (nums)    reverse(nums, 0, n - 1)#3    return numsdef reverse(array, a, b):    while a < b:        array[a], array[b] = array[b], array[a]        a += 1        b -= 1a=[1,2,3,4,5,6,7]print (rotate(a,4))
这段代码以前见过,让我费解的是 为什么这么颠倒最后就都移动了k位。
#1这一步移动了 (n-k)/2+1 #2这一步移动了 k/2+1 #3移动的不知道如何计算。



#
There is a parking lot with only one empty spot. Given the initial state
# of the parking lot and the final state. Each step we are only allowed to# move a car# out of its place and move it into the empty spot.# The goal is to find out the least movement needed to rearrange# the parking lot from the initial state to the final state.# Say the initial state is an array:# [1,2,3,0,4],# where 1,2,3,4 are different cars, and 0 is the empty spot.# And the final state is# [0,3,2,1,4].# We can swap 1 with 0 in the initial array to get [0,2,3,1,4] and so on.# Each step swap with 0 only.#Edited by cyberking-saga

def garage(beg, end):    i = 0    moves = 0    while beg != end:        if beg[i] != 0 and beg[i] != end[i]:            car = beg[i]            empty = beg.index(0)            final_pos = end.index(beg[i])            if empty != final_pos:                print ( str(beg[final_pos]), str(beg[empty]) +'   final emp')                beg[final_pos], beg[empty] = beg[empty], beg[final_pos]                print(beg)                empty = beg.index(0)                print (str(beg[beg.index(car)]), str(beg[empty]) +'   car  empty')                beg[beg.index(car)], beg[empty] = beg[empty], beg[beg.index(car)]                print(beg)                moves += 2            else:                print (str(beg[beg.index(car)]), str(beg[empty]) +'  car  empty  else')                beg[beg.index(car)], beg[empty] = beg[empty], beg[beg.index(car)]                print(beg)                moves += 1        i += 1        if i == len(beg):            i = 0    return movesif __name__ == "__main__":    initial = [1,2,3,0,4]    final = [0,3,2,1,4]    print("initial:", initial)    print("final:", final)    print(garage(initial, final))



def missing_ranges(nums, lo, hi):    res = [] # 存取缺少的    start = lo #start是最小数    for num in nums:        if num < start: #若小于最小数则不需要添加 继续下一步            continue        if num == start: #若相等,最小数加一            start += 1            continue        res.append(get_range(start, num-1))  #若不num>start则在最小数和现在的数之间有缺少值,添加到res中        start = num + 1    if start <= hi: #最后的检查,看是否有数组中最大值到设定的最大值之间的缺少值        res.append(get_range(start, hi))    return resdef get_range(n1, n2):    if n1 == n2:        return str(n1)    else:        return str(n1) + "->" + str(n2)nums = [3, 5, 10, 11, 12, 15, 19]print("original:", nums)print("missing range: ", missing_ranges(nums,0,20))


def three_sum(nums:"List[int]")->"List[int]":    res = []    nums.sort()    print (nums)    for i in range(len(nums)-2):        if i > 0 and nums[i] == nums[i-1]:   #就像-1 -1 这种情况,其实是一样的,不再计算。            continue        l, r = i+1, len(nums)-1   #l是现在要循环的后一个书,r表示最有一个数        while l < r:            s = nums[i] + nums[l] + nums[r]            if s > 0:                r -= 1            elif s < 0:                l += 1            else:                # found three sum                res.append((nums[i], nums[l], nums[r]))                # remove duplicates  由于不能确定是否中间还存在别的数加上现在I位置的数为0 所以需要这一步                while l < r and nums[l] == nums[l+1]:                    l+=1                while l < r and nums[r] == nums[r-1]:                    r -= 1                l += 1                r -= 1    return resif __name__ == "__main__":    x = [-1,0,1,2,-1,-4]    for i in range(len(x)-2) :        print (i)    print(three_sum(x))


0 0
原创粉丝点击