算法之路(一)

1 篇文章 0 订阅

开发一个工程管理的项目,觉得自己算法知识比较薄弱,好吧,几乎为zero。看着别人写的代码都很厉害,算法也很经典。好吧,现在开始一点点补还来得及,首先从基础开始:

/**
* 求解最大子序列
* @author JulieZou
*
*/
public class MaxSubSequence {

/**
 * 欧几里得算法(计算最大公因数)
 * 
 * @param m
 * @param n
 * @return
 */
public static long gcd(long m, long n) {

    while (n != 0) {
        long res = m % n;
        m = n;
        n = res;
    }
    return m;
}

/**
 * 折半查找--查找前提是数据是有序的
 * 
 * @param arr
 * @return
 */
@SuppressWarnings("unused")
private static int binarySearch(int[] arr, int search) {

    int low = 0, high = arr.length - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] > search) {
            high = mid - 1;
        } else if (arr[mid] < search) {
            low = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}

/**
 * 子序列最简便的算法
 * 
 * @param arr
 * @return
 */
private static int maxSum2(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;

}

private static int maxSum(int[] arr) {

    int maxSum = 0;
    for (int i = 0; i < arr.length; i++) {
        int thisSum = 0;
        for (int j = 0; j < arr.length; j++) {
            thisSum += arr[j];
            if (thisSum > maxSum) {
                maxSum = thisSum;
            }
        }

    }
    return maxSum;
}

private static int maxSumRec(int[] arr, int left, int right) {

    if (left == right) {
        if (arr[left] > 0) {
            return arr[left];
        } else {
            return 0;
        }
    }

    int center = (left + right) / 2;
    int maxLeftSum = maxSumRec(arr, left, center);
    int maxRightSum = maxSumRec(arr, center + 1, right);

    int maxLeftBorderSum = 0, leftBorderSum = 0;

    for (int i = center; i >= left; i--) {

        leftBorderSum += arr[i];
        if (leftBorderSum > maxLeftBorderSum) {
            maxLeftBorderSum = leftBorderSum;
        }
    }

    int maxRightBorderSum = 0, rightBorderSum = 0;
    for (int i = center + 1; i <= right; i++) {
        rightBorderSum += arr[i];
        if (rightBorderSum > maxRightBorderSum) {
            maxRightBorderSum = rightBorderSum;
        }
    }
    return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);

}

private static int max3(int maxLeftSum, int maxRightSum, int i) {

    return (((maxLeftSum > maxRightSum) ? maxLeftSum : maxRightSum) > i) ? ((maxLeftSum > maxRightSum) ? maxLeftSum : maxRightSum) : i;
}

public static void main(String[] args) {

    int[] arr = { 1, 67, 89, 342, 32 };
    int maxSe = maxSumRec(arr, 0, arr.length - 1);
    System.out.println(maxSe);

    System.out.println(maxSum(arr));

    System.out.println(maxSum2(arr));
}

}

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* 自定义ArrayList
*
* @author JulieZou
*
* @param
*/
public class MyArrayList implements Iterable {

// 默认容量
private static final int DEFAULT_CAPACITY = 10;

private int size;

private AnyType[] items;


public MyArrayList() {

    doClear();
}

public void clear() {

    doClear();
}

/**
 * 清除数据
 */
public void doClear() {

    size = 0;
    ensureCapacity(DEFAULT_CAPACITY);
}

/**
 * 获取数组长度
 * 
 * @return
 */
public int size() {

    return size;
}

/**
 * 判断数组是否为空
 * 
 * @return
 */
public boolean isEmpty() {

    return size() == 0;

}

/**
 * 扩充容量
 */
public void trimToSize() {

    ensureCapacity(size());

}

/**
 * 扩充容量
 * 
 * @param newCapacity
 */
@SuppressWarnings("unchecked")
public void ensureCapacity(int newCapacity) {

    if (newCapacity < size)
        return;
    AnyType[] oldItems = items;
    items = (AnyType[]) new Object[newCapacity];
    for (int i = 0; i < size(); i++) {
        items[i] = oldItems[i];
    }
}

/**
 * 获取下标指定的元素
 * 
 * @param idx
 * @return
 */
public AnyType get(int idx) {

    if (idx < 0 || idx >= size()) {
        throw new ArrayIndexOutOfBoundsException();
    }

    return items[idx];
}

/**
 * 替换值
 * 
 * @param idx
 * @param newVal
 * @return
 */
public AnyType set(int idx, AnyType newVal) {

    if (idx < 0 || idx >= size()) {
        throw new ArrayIndexOutOfBoundsException();
    }
    AnyType oldVal = items[idx];
    items[idx] = newVal;
    return oldVal;
}

/**
 * 添加元素
 * 
 * @param x
 * @return
 */
public boolean add(AnyType x) {

    add(size(), x);
    return true;
}

/**
 * 添加元素
 * 
 * @param idx
 * @param x
 */
public void add(int idx, AnyType x) {

    if (items.length == size())
        ensureCapacity(size() * 2 + 1);
    for (int i = size; i > idx; i--) {
        items[i] = items[i - 1];
    }
    items[idx] = x;
    size++;
}

/**
 * 移除并返回指定位置的元素
 * 
 * @param idx
 * @return
 */
public AnyType remove(int idx) {

    AnyType removeItem = items[idx];
    for (int i = idx; i < size() - 1; i++) {
        items[i] = items[i + 1];
    }
    size--;
    return removeItem;
}

@Override
public Iterator<AnyType> iterator() {

    return new ArrayListIterator();
}


private class ArrayListIterator implements Iterator<AnyType> {

    private int current = 0;


    @Override
    public boolean hasNext() {

        return current < size();

    }

    @Override
    public AnyType next() {

        if (!hasNext())
            throw new NoSuchElementException();
        return items[current++];
    }

    public void remove() {

        MyArrayList.this.remove(--current);
    }

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值