zoukankan      html  css  js  c++  java
  • 卷王

    ef fib(n):
        """ 递归:时间复杂度O(2**n),空间复杂度O(N),树的高度"""
        if n < 3:
            return 1
        else:
            return  fib(n - 1) + fib(n - 2)
    
    # fib(6)=fib(5)+fib(4)
    
    # 1,1,2,3,5,8
    def fib2(n):
        """ 循环:时间复杂度O(N),空间复杂度O(1)"""
        a,b =0,1
        for x in  range(n):
            a,b = b,a+b
        return b
    
    def fib_while(n):
        """ 循环:T=O(N),S=O(1)"""
        a,b=0,1
        i=1
        while i<=n :
             print(b,end=" ")
             i =i+1
             a,b =b,a+b
    
    
    # 二分法:
    s = list(range(1,10))
    
    def bin_search(arr,x):
        l,r = 0,len(arr)-1
        # 左边小于右边
    
        while l <=r :
            mid = (l + r )// 2
            if x < arr[mid]:
                r = mid -1
            elif x > arr[mid]:
                 l = mid +1
            else :
                return mid
        return  False
        # 左大于右边
    
    def  bin_search_recur(arr,x):
         k = len(arr)
         if k<1:
             return False
         mid = k//2
         if x< arr[mid]:
             return  bin_search_recur(arr[:mid-1],x)
    
         elif x> arr[mid]:
             return  bin_search_recur(arr[mid+1:],x)
         else :
             return True
    
    
    def insert_sort(arr):
        for  j in range(1,len(arr)):
            # j-1,j  =3,1
            while  arr[j] < arr[j-1]:
                    temp = arr[j - 1]
                    arr[j-1] = arr[j]
                    arr[j] =temp
                    j =j-1
        return  arr
    
    
    def bin_x(arr,x):
        mid =len(arr)// 2
        if x< arr[mid]:
            bin_x(arr[:mid-1],x)
        elif x > arr[mid]:
            bin_x(arr[mid+1:],x)
        else:
             return  mid
    
    def bin_whiles(arr,x):
        """
        :param arr:
        :param x:
        :return:  mid index 索引
        """
        l,r = 0 ,len(arr)-1
        while l<=r:
            mid = (l+r)// 2
            if  x< arr[mid] :
               r = mid-1
    
            elif x> arr[mid] :
               l =mid +1
            else :
              return  mid
    
    
    
    
    
    
    
    
    
    
    
    def  quick_sort(arr):
            """快速排序"""
            if len(arr) < 2:
                return arr
            # 选取基准,随便选哪个都可以,选中间的便于理解
            mid = arr[len(arr) // 2]
            # 定义基准值左右两个数列
            left, right = [], []
            # 从原始数组中移除基准值
            arr.remove(mid)
            for item in arr:
                # 大于基准值放右边
                if item >= mid:
                    right.append(item)
                else:
                    # 小于基准值放左边
                    left.append(item)
            # 使用迭代进行比较
            return quick_sort(left) + [mid] + quick_sort(right)
    
    
    
    def sr(arr):
        """ T= nlog(N),S= nlog(N)"""
        if len(arr)<2:
            return arr
        mid =arr[len(arr) // 2]
        arr.remove(mid)
        l,r = [],[]
        for k in arr:
            if k >= mid:
                r.append(k)
            else:
                l.append(k)
        return sr(l)+[mid]+sr(r)
    
    
    def hashT(arr,const=10):
        hashMap={}
        for x in range(len(arr)):
            y = const-arr[x]
            # 在hash表值,y 对应的索引不等于
            if   y in arr and hashMap.get(y) != arr[x]:
                print(arr[x],x,y,arr.index(y))
                hashMap[arr[x]]=y
        print(hashMap)
    
        # for x in range(len(arr)):
            # y = const-arr[x]
            # # 在hash表值,且在arr,不等于自己
            # if   y in hashMap.values() and  y !=x  and  y in arr:
            #     print(arr[x],y)
    
    
    
    
    
    
    if __name__ == '__main__':
    
        s= [5,1,6,3,7,1,4,5]
        print(insert_sort(s))
    

      

  • 相关阅读:
    最小的K个数
    数组中出现次数超过一半的数字
    合并排序
    一只小蜜蜂...
    从零开始学springboot笔记(三)-Spring boot之热部署之Springloaded(仅做了解,实际开发中不用它)
    从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)
    从零开始学springboot笔记(一)-Spring boot之Hello Word
    IDEA2018永久破解版下载与安装
    $.ajaxSettings.async = false;是什么意思?
    嘿嘿
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/15152347.html
Copyright © 2011-2022 走看看