python---小算法解釋

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 nums


def reverse(array, a, b):
    while a < b:
        array[a], array[b] = array[b], array[a]
        a += 1
        b -= 1

a=[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 moves

if __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 res

def 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 res


if __name__ == "__main__":
    x = [-1,0,1,2,-1,-4]
    for i in range(len(x)-2) :
        print (i)
    print(three_sum(x))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章