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


    大战严蔚敏

    排序

    插入排序

    简单插入排序

    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
    
    }
    
  • 相关阅读:
    JS+CSS自动切换选项卡
    漂亮的透明css菜单 下拉效果
    HTML下的默认样式属性
    测测你是否近视!
    CDATA C#操作XML(无命名空间),添加/删除/编辑节点
    JS 键盘事件
    JS获取Cookie值
    解决windows7下不能以管理员身份安装msi的问题
    引用Interop.SQLDMO.dll后的注意事项。
    c#正则表达式用法大全
  • 原文地址:https://www.cnblogs.com/zhouyu0-0/p/14395419.html
Copyright © 2020-2023  润新知