• 过年七天乐,刷题也快乐!


    大战严蔚敏

    排序

    插入排序

    简单插入排序

    def simple_sort(self,arr):
            l =len(arr)
            for i in range(l-1):
                # 选择i+1到最小的放置到i上
                idx = i
                for j in range(i,l):
                    if arr[j]<arr[idx]: idx = j
                if idx != i:
                    t = arr[i]
                    arr[i] = arr[idx]
                    arr[idx] = t
    

    堆排序

    
    //从上往下
    void heap_adjust(vector<int> &arr,int start,int end){
        if (start ==end )return;
        //左子树
        for(int i =start,j = 2*(i+1)-1;j<=end;i=j,j=2*(i+1)-1){
            if(j+1<end&&arr[j]<arr[j+1])j++;
            if(arr[i]<arr[j])swap(arr,i,j);
        }
    }
    
    //从下往上
    void build_heap(vector<int> &arr){
        if(arr.size()==0)return;
        for(int i =arr.size()/2;i>=0;i--){
            heap_adjust(arr,i,arr.size()-1);
        }
    }
    
    //堆排序
    void heap_sort(vector<int> &arr){
        //建堆
        build_heap(arr);
        for(int i=arr.size()-1;i>=0;){
            swap(arr,0,i--);
            heap_adjust(arr,0,i);
            printVec(arr);
        }
    }
    
    
    //java中堆排序的写法 Timer.java
    /**
         * Establishes the heap invariant (described above) assuming the heap
         * satisfies the invariant except possibly for the leaf-node indexed by k
         * (which may have a nextExecutionTime less than its parent's).
         *
         * This method functions by "promoting" queue[k] up the hierarchy
         * (by swapping it with its parent) repeatedly until queue[k]'s
         * nextExecutionTime is greater than or equal to that of its parent.
         */
        private void fixUp(int k) {
            while (k > 1) {
                int j = k >> 1;
                if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
                    break;
                TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
                k = j;
            }
        }
    
        /**
         * Establishes the heap invariant (described above) in the subtree
         * rooted at k, which is assumed to satisfy the heap invariant except
         * possibly for node k itself (which may have a nextExecutionTime greater
         * than its children's).
         *
         * This method functions by "demoting" queue[k] down the hierarchy
         * (by swapping it with its smaller child) repeatedly until queue[k]'s
         * nextExecutionTime is less than or equal to those of its children.
         */
        private void fixDown(int k) {
            int j;
            while ((j = k << 1) <= size && j > 0) {
                if (j < size &&
                    queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
                    j++; // j indexes smallest kid
                if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
                    break;
                TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
                k = j;
            }
        }
    
        /**
         * Establishes the heap invariant (described above) in the entire tree,
         * assuming nothing about the order of the elements prior to the call.
         */
        void heapify() {
            for (int i = size/2; i >= 1; i--)
                fixDown(i);
        }
    
    

    kmp算法

    #include<iostream>
    using namespace std;
    
    const int N=100010,M=1000010;
    int n,m;
    int ne[N];
    char s[M],p[N];
    
    int main()
    {
        cin >> n >> p+1 >>m >>s+1;
        int i,j;
        //求next
        for(i=2,j=0;i<=n;i++)
        {
            while(j&&p[i]!=p[j+1])j = ne[j];
            if(p[i]==p[j+1])j++;
            ne[i]=j;
        }
        //匹配过程
        for(i=1,j=0;i<=m;i++)
        {
            while(j&&s[i]!=p[j+1])j=ne[j];
            if(s[i]==p[j+1])j++;
            if(j==n){
                printf("%d ",i-n);
                j=ne[j];
            }
        }
        return 0;
    }
    

    leetcode 567

    leetcode 4

    
    //两个数组的二分法
    func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
    	l := len(nums1) + len(nums2)
    	if l%2 == 1 {
    		midIndex := l / 2
    		return float64(getKthElement(nums1, nums2, midIndex+1))
    	} else {
    		midIdx1, midIdx2 := l/2-1, l/2
    		return float64(getKthElement(nums1, nums2, midIdx1+1)+getKthElement(nums1, nums2, midIdx2+1)) / 2.0
    	}
    	return 0
    
    }
    
    //得到第k个元素
    func getKthElement(nums1, nums2 []int, k int) int {
    	idx1, idx2 := 0, 0
    	for {
    		if idx1 == len(nums1) {
    			return nums2[idx2+k-1]
    		}
    		if idx2 == len(nums2) {
    			return nums1[idx1+k-1]
    		}
    		if k == 1 {
    			return min(nums1[idx1], nums2[idx2])
    		}
    		half := k / 2
    		newIdx1 := min(idx1+half, len(nums1)) - 1
    		newIdx2 := min(idx2+half, len(nums2)) - 1
    		p1, p2 := nums1[newIdx1], nums2[newIdx2]
    		if p1 <= p2 {
    			k -= (newIdx1 - idx1 + 1)
    			idx1 = newIdx1 + 1
    		} else {
    			k -= (newIdx2 - idx2 + 1)
    			idx2 = newIdx2 + 1
    		}
    	}
    	return 0
    }
    
    func min(x, y int) int {
    	if x < y {
    		return x
    	}
    	return y
    
    }
    
  • 相关阅读:
    【ESXI6.0】 ESXI6.0安装时无法安装网卡驱动的解决方法及将网卡驱动加载进ISO
    [转]Vs解决方案的目录结构设置和管理
    在控制台编译运行java程序详细指导
    详解Linux安装GCC方法
    MySQL server has gone away 问题的解决方法
    eclipse或Myeclipse中web项目没有run on server时怎么办?
    DTM/DEM/DSM/DOM/DLG
    linux常用命令
    为什么很多地方看到初始值是1970年8月1日
    mongoDB
  • 原文地址:https://www.cnblogs.com/zhouyu0-0/p/14395419.html
Copyright © 2020-2023  润新知