java笔试题:数组查找,查找和为最大的子数组

package com.bxh.array;

import java.util.ArrayList;
import java.util.Arrays;

public class findMaxMin {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] arr={1,-2,4,8,-4,7,-1,-5};
//		findMaxMinS(arr);
//		int smax=secondMax1(arr);
//		System.out.println(smax);
		
		ArrayList list =new ArrayList();
		list=maxSubArray(arr);
		for (Integer integer : list) {
			System.out.print(integer+" ");
		}
		System.out.println("===========================");
		int sumM=maxSubArray2(arr);
		System.out.println("子数组最大和为:"+sumM);
	}

//动态规划方法求数组中最大子数组之和
	private static int maxSubArray2(int[] arr) {
		// TODO Auto-generated method stub
		int n=arr.length;
		int [] end=new int[n];
		int [] all=new int[n];
		end[n-1]=arr[n-1];
		all[n-1]=arr[n-1];
		end[0]=all[0]=arr[0];
		for(int i=1;in?m:n;
	}


	//求数组中最大和最大的子数组;
	private static ArrayList maxSubArray(int[] arr) {
		int n=arr.length;
		int sum=0,maxSum=0,i,j,k;
		ArrayList list =new ArrayList();
		for(i=0;imaxSum){
					list.clear();
					maxSum=sum;
					for(k=i;kmax){
				smax=max;
				max=arr[i];
			}else{
				if(arr[i]>smax){
					smax=arr[i];
				}
			}
		}
		return smax;
		
	}
	//求数组中第二大整数方法二:

	private static int secondMax1(int[] arr) {
		Arrays.sort(arr);
		return arr[arr.length-2];
	}

	//求数组中最大和最小的两个数
	private static void findMaxMinS(int[] arr) {
		int len=arr.length;
		int max=arr[0];
		int min=arr[0];
		for (int i = 0; i < len; i=i+2) {
			if(i+1==len){
				if(maxarr[i]){
					min=arr[i];
				}
			}
			if(i+1arr[i+1]){
				if(arr[i]>max){
					max=arr[i];
				}
				if(min>arr[i+1]){
					min=arr[i+1];
				}
			}
			if(i+1max){
					max=arr[i+1];
				}
				if(min>arr[i]){
					min=arr[i];
				}
			}
		}
		System.out.println(max+"   "+min);
	}
}

你可能感兴趣的:(Java面试题集)