zoukankan      html  css  js  c++  java
  • 算法小记

    只要在常数时间内可以将问题的大小削减为其一部分($ frac{1}{2} (), 那么该算法就是()O(logN)$)

    1. 最大子序列和问题((O(NlogN)))
    public static int maxSubSum(int[] arr) {
    	int maxSum = 0, thisSum = 0;
    	for (int i = 0; i < arr.length; i++) {
    		thisSum += arr[i];
    		if (thisSum > maxSum) {
    			maxSum = thisSum;
    		} else if (thisSum < 0) {
    			thisSum = 0;
    		}
    	}
    	return maxSum;
    }
    
    1. 折半查找binary search((O(logN)))
    	public static int binarySearch(int[] arr, int x) {
    		int low = 0, high = arr.length - 1;
    		while (low <= high) {
    			int mid = (low + high)/2;
    			if (arr[mid] < x) {
    				low = ++mid;
    			} else if (arr[mid] > x) {
    				high = --mid;
    			} else {
    				return mid;
    			}
    		}
    		return -1;
    	}
    
    当你准备好了,机会来临的时候,你才能抓住
  • 相关阅读:
    PatentTips
    PatentTips
    PatentTips
    Xvisor ARM32 启动分析
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
  • 原文地址:https://www.cnblogs.com/studentytj/p/9249819.html
Copyright © 2011-2022 走看看