算法设计

//连续子数组的最大和:O(n)复杂度的实现
//事实证明:巧妙使用临时变量可以省掉一层for循环,降低复杂度
public class T29 {
    public int FindGreatestSumOfSubArray(int[] array) {
        int max = array[0];
        int res = array[0];
        for (int i = 1; i < array.length; i++) {
            max = Math.max(max + array[i], array[i]);
            res = Math.max(res, max);
        }
        return res;
    }
}

//将数组中的元素颠倒
public class Test {
    public static void main(String[] args){
        int[] a = new int[]{
                (int)(Math.random()*1000),
                (int)(Math.random()*1000),
                (int)(Math.random()*1000),
                (int)(Math.random()*1000),
                (int)(Math.random()*1000)
        };
        System.out.println(Arrays.toString(a));
        swap(a);
        System.out.println(Arrays.toString(a));
    }
    
    public static void swap(int[] a){
        int len = a.length;
        for(int i=0;i<len/2;i++){
            int temp = a[i];
            a[i] = a[len-i-1];
            a[len-i-1] = temp;
        }
    }
}

//交换list中的两个元素
public static void swap(List<?> list, int i, int j) {
final List l = list;
    l.set(i, l.set(j, l.get(i)));
}

//数字金额 转 汉语金额   整体用的StringBuffer的插入法   存在问题,待完善
public class RenMingBi {
    
    public static final char[] data = new char[]{
        '零','壹','贰','叁','肆','伍','陆','柒','捌','玖'
    };
    
    public static final char[] units = new char[]{
        '元','拾','佰','仟','万','拾','佰','仟','亿'
    };
    
    public static String convert(int money){
        StringBuffer sbf = new StringBuffer();
        int unit = 0;
        while(money!=0){
            sbf.insert(0, units[unit++]);//从0号位置开始插入单位
            int number = money%10;
            sbf.insert(0, data[number]);//插入数字金额
            money /= 10;
        }
        return sbf.toString();
    }
    
    public static void main(String[] args){
        System.out.println(convert(135680123));
    }

}

//递归:第一个人10岁,第二个比第一个大2岁,依次递推,求第8个人年龄
public class Test {

    public static void main(String[] args){
        System.out.println(computeAge(8));
    }
    
    public static int computeAge(int n){
        if(1==n)
            return 10;
        return computeAge(n-1)+2;
    }
}

//有一个100万的数组,里边有两个是重复的,如何设计算法找到
public class ArrayDemo {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        /**
         * 初始化数组 start
         */
        int length = 10000000;//定义数组长度
        int arr[] = new int[length];//定义一个长度未length的数组
        for (int i = 0; i < length; i++) {
            arr[i] = i; //初始化数组
        }
        arr[1999999] = 1999996;//写入重复数据
        /**
         * 初始化数组 end
         */
        
        /***
         * 这里使用LinkedHashSet,不能用HashSet
         * LinkedHashSet 是基于链表实现的,是有序的
         * HashSet       是基于hash原理实现的,无序
         */
        Set set = new LinkedHashSet<>();
        //Set set = new HashSet();
        //循环将数组中的数据放入set中,因为set不可重复的特性,如果找到存在的数据,跳出循环,标记下标位置
        
        int i = 0;
        for (i = 0; i < length; i++) {
            if(!set.add(arr[i])){
                break;
            }
        }
        
        //利用已知重复数据arr[i],遍历set找到重复数据的索引
        int j = 0;
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            int temp = (int) iter.next();
            if(temp == arr[i]){
                break;
            }
            j++;
        }
        
        System.out.println("arr【" + i + "】 == arr【"+ j + "】 == 【" + arr[i] + "】==【"+ arr[j] + "】"); 
        long endTime = System.currentTimeMillis();
        System.out.println("遍历用时:" + (endTime - startTime) + "ms");
    }
}


二叉树实现

击石乃有火,不击元无烟!!
原文地址:https://www.cnblogs.com/rain2020/p/13097812.html